Game Development Community

Strategy for making a sprite follow a path, e.g a projectile arc

by Allistair · in Torque Game Builder · 02/23/2006 (3:47 pm) · 11 replies

Hi All,

I purchased the T2D package this week following finding out about the Torque engines via Marble Blast on XB360, coupled with always wanted to write a game but not really wanting to get into raw code. I am a Java developer but quite naive when it comes to games!

So I have an idea for something I'd like to try out as a start and I'm stuck already :) I think the docs need working more, but I'm not advanced enough to really say if that's my lack of experience or a general problem.

Essentially I have a sprite that I would like to follow a path, a bit like a projectile I suppose fired from a gun. Gravity is involved such that an arc path is formed which my sprite should move along. I am unsure how to go about this structually in T2D, what I should do myself and what T2D can offer me.

For example, I have bound a key W let's say to fire my sprite in an arc from a ground position. How do I define to T2D the arc it should follow? Should I do this in a custom algorithm and use a schedule function to update the X and Y position of my sprite over the duration of the movement or is there something I can hook into? Bit lost on this one :)

Another issue I noticed was that in a bound key, I setRotation on my sprite and setLinearVelocityX. When I hold the bound key down, the setLinearVelocityX appears to accumulate, or at least carry on, whereas the rotation happens once only. The setRotation is set to the getRotation + rotation increment, but I find I have to keep tapping the bound key to get an increment, whereas I want to hold the key down.

Sorry if these are way basic, I appreciate your time and help,

AC.

#1
02/23/2006 (3:58 pm)
If you're after a projectile motion, then you can give your sprite an impulse to set it off in the direction you want and apply gravity as a constant force. The physics in T2D should do the rest for you.

Have a look in the reference pdf file for the function "setImpulseForcePolar()" you can use that to apply a force to your objcet at a certain angle to get it in motion. Then have a read about "setConstantForce()" which you can use to apply a constant downwards force for gravity.

If however you're after following an arbitary path you create rather than just projectile motion, then you'll probably need a way to draw splines and have the sprites follow this path.
#2
02/23/2006 (4:01 pm)
Gary is right, for the specific request you stated, you simply need to assign forces that are appropriate for your sprite (ballistic projectile style here).

Quote:If however you're after following an arbitary path you create rather than just projectile motion, then you'll probably need a way to draw splines and have the sprites follow this path

FYI, this is coming...pathing objects!
#3
02/23/2006 (4:02 pm)
Hi,

Wow thanks for the ultra fast reply. The motion I need is not spline based (thank goodness) - it's pretty much an arc based on sideways movement, gravity and constant "impulse" as it's called. I will go away and take a look at those features, thanks again :)
#4
02/23/2006 (4:03 pm)
That sounds really useful ... I suppose something like Zuma on XB360 uses a spline for the balls to roll along .. anything that simplifies splines would be pretty useful ;)
#5
02/23/2006 (4:07 pm)
That sounds really useful ... I suppose something like Zuma on XB360 uses a spline for the balls to roll along .. anything that simplifies splines would be pretty useful ;)
#6
02/24/2006 (3:48 am)
For your other problem try using "setAutoRotation" instead of "setRotation". This will make the sprite continue to turn. You will just have to create a function that sets the auto rotation to 0 when the key is released (otherwise it will never stop ;).
#7
02/24/2006 (3:54 am)
Thanks Tom :) The other suggestions about the impulse and constant force worked by the way and I am very happy. I am sure I may come back with other questions :)
#8
02/24/2006 (4:02 am)
Actually though, whilst we're on the topic ... if I want an accumulation of say speed or rate based on the user holding a key down, how would I do this if the key hit is registered only once?
#9
02/24/2006 (4:30 am)
In the function that increases the players speed schedule it to repeat itself again. eg:

function keyPressed()
{
    $keyPressed = 1;
    increaseSpeed();
}

function keyReleased()
{
    $keyPressed = 0;
}

function increaseSpeed()
{
    if ($keyPressed)
    {
        %speed = $player.getLinearVelocityX();
        %speed++;
        $player.setLinearVelocityX(%speed);
        schedule(100,0,"increaseSpeed");
    }
}

Bind the "keyPressed()" and "keyReleased()" to which ever key you need and the speed of $player should increase by 10 units a second (in the X plane anyway) while the key is held.
#10
02/24/2006 (4:36 am)
Ok I see, thanks a lot, will give it a go :)
#11
02/24/2006 (6:10 am)
That's great, works really well, very happy.