forums

TNT Basic Forums > Programming Q&A
calculating X and Y velocity for projectiles
< Last Thread     Next Thread >
Author
Thread        Post A Reply

09-17-2010 00:22

Posted by:
Yakkers

Find more posts by Yakkers

I believe this is really just math that I can't piece together how to do, but say I want to give the ability to shoot a projectile at any angle, like directly where the mouse is clicked or directly at another sprite.

If my projectiles are handled with both an X and Y position array and an X and Y velocity array, how would I go about calculating what to set the X and Y velocities to, to fire in a set direction at a contant speed?

09-20-2010 15:31

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Hi Mate...

Take a look in the Directory for a thing called Breshenham's mid point line algorithm. It should do exactly what you want, and I wrote it myself, so I know it works... ;)

Any problems drop me a line...

Happy Hunting!

Danny (nod the mod)

09-20-2010 17:25

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

P.S.

You could also do it the old fashioned way, but this involves multiplication and division at run time, which is slower, but the math behind it is far simpler...

Click here to view the 'line equation'

Happy, er.. sloping?

Danny (nod the mod)

09-21-2010 17:25

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

P.P.S...

You should probably look on page 2, for the original version (v0.1) as this is more to do with projectiles, the later one (on page 1) uses sprite animation and could cloud the issue...

'Till next time...

Danny (nod the mod)

09-25-2010 20:44

Posted by:
Yakkers

Find more posts by Yakkers

I could use the bresenham, but I'd really rather just find a way to calculate the X and Y per frame so I can just calculate it once per projectile and just let it fly using those values until it hits something.

Also I don't really see how I'd apply the basic slope formulas to this, especially with the Y intercept and all.

It just really seems like there should be a simple formula to take the length of a triangle's hypotenuse and the coords of its endpoints and use that to find the length of the other two sides, I'm pretty sure I've done things exactly like that in math classes before.

09-27-2010 21:05

Posted by:
Yakkers

Find more posts by Yakkers

I gots it!

What I did is use the distance formula to find the distance between the bullet's starting location and target. Then I divided the speed It should go by that distance to get a scale factor, which I then divided the X and Y distances between the two points by.

It uses a lot of floats so it's probably not very efficient, but It's a start until maybe someday I feel like fully understanding and implementing the bresenham.

09-29-2010 11:32

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Way to Go!

That's the simplest way (mathematically) to sort it out, but you are spot on with the float issue... especially when doing multiplication and division. Remember also that the screen uses int, not float, for the coordinates, so there is the issue of implied conversion. You don't have to specifically do this as TNT does it for you (hence the implied bit), but it still happens all the same.

It's not a biggy at the moment as you're only prototyping, but it might be an idea to factor the calculations out to separate routines that you can modify later without effecting the rest of the code, especially if you want to tweek performance in the optimisation stages...

Keep us posted as to your progress!

Danny (nod the mod)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.