Skip to main content
gamedev.net
🔒 Locked

"Throwing" in 3D

Started by Scarfy Sep 20, 2005 at 8:28 AM 6 replies 1k views
Original Post
Scarfy
Scarfy
Probably a bad title but I'll explain what I need to achieve here, The game I'm making (tile based turn based strategy) allows the player to have a unit throw an item from one square to another (the game is implemented in OpenGL just to be clear, it's not a 2D game). My problem is that I cannot work out the calculation I would need for the velocity of the item in order to make it land in the correct place. Calculating the normal for the direction the item must go is easy, however I need to take in to account gravity. Could anyone point me at some code samples or give me some hints as to what I need to do to achieve this? Thanks
Parallel RealitiesProject: Starfighter
Endurion
Endurion
I wouldn't go so far as to actually implement a true physics calculation. Half a sine should do the trick for a realistic enough looking curve.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Trap
Trap
The general form of this problem is called "boundary value problem". The most primitive method of solving these problems is called the "shooting method". You try some values, look where you land and pick better start values till you hit close enough to your target.

What kind of physics do you want to consider? If you only want gravity it is easy:
1. reduce it to a 2d problem
2. pick some value for vx and solve for vy.
x_target=x0+vx*t
y_target=y0+vy*t-1/2*g*t*t
Scarfy
Scarfy
The sine method sounds good.

Yes, I only want to take in to account gravity. Enough so that the item has time to traverse the required distance before it lands.
Parallel RealitiesProject: Starfighter
Trap
Trap
A sine looks quite similar to the correct curve, but the correct one is almost as easy. I would use the correct one posted above.
TDragon
TDragon
Let's do a little physics.

Given starting position x0 and y0, and starting velocity v0x and v0y, we can calculate the final position by solving an constant-a equation (where we used the simplified model of gravity as the only accelerative force) in the y direction for time, then solving a constant-v equation in the x direction for x.
yf = y0 + v0yt + 0.5at2
xf = x0 + v0xt

a is the gravitational acceleration constant for your game (in real life, -9.8 m/s2)

So if you know the final position you want, xf, just pick an arbitrary value for v0 (representative of the unit's throwing power) and use v02 = v0x2 + v0y2

I'll leave all the dirty algebra up to you :D (let me know if you don't understand this)

Hope that helps,
Twilight Dragon
vallentin
vallentin
x=t*vx+x0
y=y0+vy0*t-g*t2/2
where t should be computed as t-t0

x0 horizontal initial position
vx horizontal speed
y0 vertical initial position
vy0 ascendent initial speed
g=9.8*m/s2 (convert in whatever)
t starts at the moment of throwing

EDIT:smbd.beat me to it
Scarfy
Scarfy
Thanks, guys. I'll look in to your suggestions.

Cheers.
Parallel RealitiesProject: Starfighter

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.