Game Development Community

Forward Acceleration/Velocity

by Brad Fairley · in Torque Game Builder · 07/31/2009 (11:10 pm) · 5 replies

Hello,
So I have my mouse draggable behavior set up and have been using constant force on mouseUP. However,I am curious about how I might apply the velocity to the forward motion of whichever way the mouse is moving after mouse up. The goal essentially is to take this object, and throw it into another on the same plane. I have gotten it to work with the constant force but it feels a bit lacking. Please anyone out there who might be able to help me, I am not asking for source code, just asking for a direction. Wouldn't be opposed to a bit of an example tho. ;)

Thank you,

#1
08/02/2009 (1:01 am)
hummm... thats an interesting question.

out of the top of my head, i would say, save the original position of the object (when grabbed) and the position of the same object when released, then, with that vector, calculate the time it took the player to get from point A to point B, and that would give you the acceleration.

then, use the setLinearVelocityPolar() or something like that to move the object in the desired direction and speed.
#2
08/03/2009 (1:47 am)
Right, ok so I have been working on trying to just capture the x and y into certain values with getWord() but I have been having so much trouble trying to figure out how to place them. So much so that I have been erroring out each time it compiles and tries to run TGB Game. Now after they are captured I want to take that point and set it as the first point in the section then onMouseUp() I need to capture another set of coordinates. Then set a speed over distance between the points and apply that to my object. If anyone has any ideas or any guidance on how this might actually work please post. I have been at this all day for a few days and if I wasn't bald already I would be pulling my hair out by now.

Thank you!
#3
08/03/2009 (9:40 am)
... ok...
1st thing 1st.

this will get the coordinates of the object when you leftclick on it, and save it to 2 global vars (posX and posY).
function class::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
    if (%clicks == 1)
    {
       $posX = getWord(%worldPosition, 0);
       $posY = getWord(%worldPosition, 1);
    }
    
}

change the onMouseDown, to onMouseUp, to get the final coordinates.

now, to calculate the speed, you need to save the time when you pushed the left click button (this, you can do it on onMouseDown), and the time when you release the button (in onMouseUp).

to calculate the speed, you can use a hack, like the one i explained in the other post... calculate the delta of the initial and final time, and positions... im a lil sleeepy now, but i think that should work.
#4
08/04/2009 (11:12 pm)
So now my only question is how would I apply the speed into the coordinates so that it would keep the object moving fluidly in the same direction that the mouse was just doing. As to say how would I say ok take the last coordinates given on mouse up and then apply the speed here in this direction.

Thanks so much again!!!!
#5
08/06/2009 (9:01 am)
when you have your speed calculated, and the angle, just use
setLinearVelocityPolar(%angle, %speed)

just remember, TGB does not have the same coordinates than a cartesian system... in CS 90 degrees is UP, but in TGB 90 degrees is RIGHT... so, keep that in mind... it will save you countless hours of debuggin.