About Store Forum Documentation Contact



Post Reply 
grab and move an object with mouse
Author Message
yvanvds Offline
Member

Post: #1
grab and move an object with mouse
Hello.

Tonight i am trying to grab an item and move it around with my mouse. The method i came up with works for a bit, but has some side effects. It works on a smooth terrain, but when my mouse goes over a hillside, for example the item moves very fast, bounces against the hill an goes out of sight with a huge leap. Quite spectacular, but not really what i had in mind.

Since i guess i'm not the first to fancy this way of moving objects, i hope some of you might have some better suggestions. Thanks in advance!

My code below:

Code:
// try to grab an item
if(Ms.b(0)) {
    if(!grab.is()) {
        // if we're not grabbing something, see if there
        // is an object below the cursor
        Vec pos, dir;
        ScreenToPosDir(Ms.pos, pos, dir);
        PhysHit selector;
        if (Physics.ray(pos, dir*D.viewRange(), &selector)) {
            if (grabbedItem = CAST(Item, selector.obj)) {
                grab.create(grabbedItem->actor, Vec(0,0,0),1);
            }
        }
    }
    // if something is grabbed, move it
    if (grab.is()) {
        Vec pos, dir;
        ScreenToPosDir(Ms.pos, pos, dir);
        PhysHit selector;
        if (Physics.ray(pos, dir*D.viewRange(), &selector)) {
            grab.pos(selector.plane.pos);
        }
    }
} else {
    if(grab.is()) grab.del();
}
07-26-2010 09:59 PM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #2
RE: grab and move an object with mouse
grab.create(grabbedItem->actor, Vec(0,0,0),0);
07-27-2010 10:44 AM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Member

Post: #3
RE: grab and move an object with mouse
Seems logical, but it doesn't really make a difference. My objects are as jumpy as before.

Another solution might be to use
Code:
if (grab.is()) grab.pos(grab.pos() + Ms.dir_d.x0y());

to adjust the item position. But then i'm strugling with the rotation of the camera or the item. Guess I should have taken that math class in secondary school :-)
07-27-2010 11:19 AM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Member

Post: #4
RE: grab and move an object with mouse
Working further on the last one, i got movement allright on the x-axis. Strange thing is, it is still wrong on the z-axis. If someone with a more mathematical background would like to take a look at it, it would help a lot!

My best try so far:

Code:
if (grab.is()) {
    Vec dir = Ms.dir_ds.x0y();
    Vec result = (0, 0, 0);
    result.x = (dir.x * Cos(angle.x)) + (dir.z * Sin(angle.x));
    result.z = (dir.x * Sin(angle.x)) + (dir.z * Cos(angle.x));
    grab.pos(grab.pos() + result*Time.d()*50);
}

This is called from within my player update function, so angle is the direction my character is looking. One full round being equal to 2*PI.
07-27-2010 04:57 PM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Member

Post: #5
RE: grab and move an object with mouse
Well i got it in the end. Who needs math classes anyway :-)

Just in case someone else is looking for the same answer, here's the correct code. It is used inside the player update function, so the 'angle' variable is the direction the camera looks.

Code:
// try to grab an item
if(Ms.b(0)) {
    if(!grab.is()) {
        // if we're not grabbing something, see if there
        // is an object below the cursor
        Vec pos, dir;
        ScreenToPosDir(Ms.pos, pos, dir);
        PhysHit selector;
        if (Physics.ray(pos, dir*D.viewRange(), &selector)) {
            if (grabbedItem = CAST(Item, selector.obj)) {
                grab.create(grabbedItem->actor, Vec(0,0,0),2);
            }
        }
    }
    // if something is grabbed, move it
    if (grab.is()) {
        Vec result = (0, 0, 0);
        result.x = Ms.dir_ds.x * Cos(angle.x) - Ms.dir_ds.y * Sin(angle.x);
        result.z = Ms.dir_ds.x * Sin(angle.x) + Ms.dir_ds.y * Cos(angle.x);
        grab.pos(grab.pos() + result*Time.d()*50);
    }
} else {
    if(grab.is()) grab.del();
}
07-27-2010 07:10 PM
Find all posts by this user Quote this message in a reply
miro@3dea Offline
Member

Post: #6
RE: grab and move an object with mouse
This posts requires a revive!
As I forgot most of elementary Math (grin) this is THE most helpful post I could only wish for, so first of all one big thanks to yvanvds!!!!
To move an item in y direction just multiply item matrix.y with Ms.d().y.
In my case move vector looks like:
Code:
//result is calculated in yvanvds example..
Vec3 move = Kb.b(EE::KB_LCTRL) ?
            itemMtx.x * result.x + itemMtx.y * Ms.d().y :
            itemMtx.x * result.x + itemMtx.z * result.x;
item.pos(item.pos() + move);
Other related post that I was trying to use was Moving objects... but as it says it's only partially solved, so I hope Harry or whomever else will give a try to this approach..
(This post was last modified: 10-29-2013 10:22 AM by miro@3dea.)
10-28-2013 11:39 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #7
RE: grab and move an object with mouse
Quote:Vec2 dir = Ms.pos() - (Ms.pos() - Ms.d());
Isn't that just "Vec2 dir=Ms.d()" ? wink
10-28-2013 08:36 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #8
RE: grab and move an object with mouse
(10-28-2013 09:17 PM)aceio76 Wrote:  Actually, based on that equation, isn't it "Vec2 dir= -Ms.d()" ? smile
I don't think so wink there's a double minus for Ms.d()
But yeah, math can be a pain sometimes
10-28-2013 09:22 PM
Find all posts by this user Quote this message in a reply
miro@3dea Offline
Member

Post: #9
RE: grab and move an object with mouse
yep, it's too obvious now grin..
now, it's still not exact yet, what I mean is when you move a mouse straight right (Ms.d().y == 0), moved object leans a little bit down (like --z).
Any thoughts what to use to correct this, to be just like in editor?
(This post was last modified: 10-29-2013 10:36 AM by miro@3dea.)
10-29-2013 08:39 AM
Find all posts by this user Quote this message in a reply
miro@3dea Offline
Member

Post: #10
RE: grab and move an object with mouse
finally it works as expected.. thing is to rotate moving object's matrix to face south (angle PI) and then to apply the rest of calculations to corrected matrix..
here is the code:
Code:
Vec2 delta = Ms.d() * -10.F;
Matrix mtx = item.matrix();
mtx.rotateY(AngleDelta(mtx.angles().y, PI));

Float spinx = delta.x * Cos(Cam.angle.x) - delta.y * Sin(Cam.angle.x);
Float spinz = delta.x * Sin(Cam.angle.x) + delta.y * Cos(Cam.angle.x);

Vec mov = mtx.x * spinx + mtx.z * spinz; // you can adjust y by -delta.y

item.pos(item.pos() + mov);
11-04-2013 12:00 PM
Find all posts by this user Quote this message in a reply
Post Reply