Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / i never was good at physics....

Author
Message
matt rochon
21
Years of Service
User Offline
Joined: 15th Mar 2003
Location: Canada
Posted: 25th Sep 2004 07:33
ok so ive locked down the physics formulae for my game here



now this works in theory, i have a target on the map that corresponds to the location im firing at, now the problem im having is i cant figure out how to make the bullet shoot along an arc that will land at this position...

anyone wanna gimme a crash course on game physics?
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 25th Sep 2004 11:13 Edited at: 25th Sep 2004 11:14
Good Physics question!

If you know the range between you and the target, and the angle to the horizontal you are projecting, then it's a case of setting the correct velocity. This is given by the equation sqrt((g*range)/(sin(2*angle))), where g is the acceleration due to gravity. (Let me know if you want to know how this equation is derived.)

Then you calculate the vertical and horizontal components of the initial velocity using, as you said, vx#=v*cos(angle) and vy#=v*sin(angle).

Finally, move the bullet in an arc by using

y#=y#+vy# `change y position by y velocity amount
vy#=vy#-g# `change y velocity by gravity amount (gives acceleration)
x#=x#+vx# `change x position by x velocity amount

You should end up with something like this .... use the arrow keys to move the target, and spacebar to fire at the target. I set the angle at 60 degrees in the code:



Hope that helps.


matt rochon
21
Years of Service
User Offline
Joined: 15th Mar 2003
Location: Canada
Posted: 25th Sep 2004 14:37
fantastic man
works great

1 last question, where does time come into play, i dont want this to happen too fast, could you post the whole set of equations?
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 25th Sep 2004 23:17
Well, the easy answer to controlling the speed is to adjust the value of gravity. Making it smaller will make things happen slower. (If you wanted more accurate time control, you'd need to add a timer function into the loop - but that get's a bit more complicated.)

Quote: "could you post the whole set of equations? "

Well, you have most of them already ......

Initial vertical velocity: vy#=v#*sin(angle#)
Initial horizontal velocity: vx#=v#*cos(angle#)
Time of flight: tof#=2*vy#/g#
Range: range#=vx#*tof#
Required initial velocity to hit target: v#=sqrt((g#*range#)/(sin(2*angle#)))
Max height: height#=((vy#)^2)/(2*g#)


Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 25th Sep 2004 23:25
Well, the easy answer to controlling the speed is to adjust the value of gravity. Making it smaller will make things happen slower. (If you wanted more accurate time control, you\'d need to add a timer function into the loop - but that get\'s a bit more complicated.)

Quote: "could you post the whole set of equations? "

Well, you have most of them already ......

Initial vertical velocity: vy#=v#*sin(angle#)
Initial horizontal velocity: vx#=v#*cos(angle#)
Time of flight: tof#=2*vy#/g#
Range: range#=vx#*tof#
Required initial velocity to hit target: v#=sqrt((g#*range#)/(sin(2*angle#)))
Max height: height#=((vy#)^2)/(2*g#)

If you wanted to get a little more involved with your projectiles, and start adding variables such as lift, air resistance, headwind, crosswind, bounce etc. then you can check out this code I wrote a while back - you can change the trajectory using the arrow keys, and all the other variables in the code:




matt rochon
21
Years of Service
User Offline
Joined: 15th Mar 2003
Location: Canada
Posted: 2nd Oct 2004 12:28
ok i got the formulae down and i understand how it works, the problem i have is i cant seem to implement it

i have an object that is facing a certain direction (y angle)
it has a certain view angle(horizontal angle)

now i can calculate where i want the shot to go, but i cant figure out how to make the shot change x and y speed to make sure it gets there, as well i have gravity set to 9.8 (im really not very good at physics)

what i need to know is how to calculate the change each cycle

i know the initial velocity i just need a way to calcualet the change at any time, i also have the time since shooting
matt rochon
21
Years of Service
User Offline
Joined: 15th Mar 2003
Location: Canada
Posted: 2nd Oct 2004 12:29
if there is a function of the curve the shot will take i know i can use the derivative to find the horizontal and vertical change at any time, but i dont know the function for the line
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 3rd Oct 2004 00:20
Quote: "i cant figure out how to make the shot change x and y speed "


It's all in the code snippet above....

vy#=vy#-g# is what changes the vertical speed.

vy# is the vertical speed at any point in time. -g# is the change in speed, (which is the vertical acceleration.) So long as you don't want to get involved in using a timer loop, adjust the value of g# to make the ball move at the desired speed. 9.8 will definitely be too big if you want it reasonably slow.

As for the change in horizontal speed of a projectile (neglecting air resistance/wind), well, there isn't any. Horizontally, a projectile moves with constant speed, because the only force acting is gravity, and that's vertical.

Quote: "if there is a function of the curve the shot will take i know i can use the derivative to find the horizontal and vertical change at any time, but i dont know the function for the line "


Take my word for it, you really don't want to, nor is there any need to get involved in 2 dimensional parabolic curve equations and their derivitives. That will get needlessly messy. Always keep vectors as seperate, perpendicular directions, as above. That enables you to consider the horizontal and vertical motions seperately - makes life a whole lot easier.


matt rochon
21
Years of Service
User Offline
Joined: 15th Mar 2003
Location: Canada
Posted: 3rd Oct 2004 08:58
hmm the problem is im using 9.8 for all of my other equations... is there a way i could generate the amount of gravity resistance on the fly?
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 3rd Oct 2004 22:06
So long as your projectile code isn't too closely linked with your other equations, just use a seperate variable for gravity for your projectiles - like gp#. 9.8, by the way is the 'real world' value for the acceleration due to gravity in metres per second squared. Unless you're trying to do accurate physics modelling (which I presume you're not since you want a slow motion effect), real world values don't have much meaning within your game world.


matt rochon
21
Years of Service
User Offline
Joined: 15th Mar 2003
Location: Canada
Posted: 4th Oct 2004 01:04
i know its the real gravity acceleration
umm i dont necessarily want slow motion effect but i want to be able to see the projectile travelling

ill give it a go with this and let you know what i got

Login to post a reply

Server time is: 2024-09-23 02:29:14
Your offset time is: 2024-09-23 02:29:14