#Need help with calculating trajectory for a grenade in a game

72 messages · Page 1 of 1 (latest)

young solstice
#

i'm a game developer who's not that good at math and i need to calculate the trajectory of a grenade in various different ways:

first, having an origin, end point and a force, i will have to calculate the angle at which the projectile will have to go starting from the origin to reach the end point, and it's okay if it can't reach it fully, but it will have to respect the force.

after that, I have a "beam" which is actually a bezier curve, and I have to modify what I can (attachments and curve sizes) to match the trajectory of the part which is re-calculated. Check the image for more information on how it works.

normal arrowBOT
dull shuttle
#

Is the force like a certain speed?

#

The way projectiles work is they will always have the same horizontal speed, while their vertical speed accelerates due to gravity

#

So using trigonometry, the horizontal speed is ucosA, while the vertical speed is usinA, where u is the starting speed of the object and A is the angle

quaint parrotBOT
#

ImOakley

dull shuttle
#

s is vertical height and a is acceleration (on earth it would be -9.8m/s^2)

#

Whatever end point youre using is another s variable away from the origin (call it d)

#

The horizontal speed is ucosA so d=utcosA

#

You would then find where the times are equal and use that to find the angle you need

young solstice
young solstice
young solstice
dull shuttle
dull shuttle
dull shuttle
young solstice
dull shuttle
#

Does the end point land at any points of higher elevation?

#

The way trig works here is that when the grenade has its initial speed, that speed can be defined into the x direction and y direction (and z direction)

#

Like if you were moving in a diagonal line in a coordinate grid you would be changing y position at a constant and changing x coordinate at another constant

#

So the rates that those coordinates change can be found using a right triangle, where the hypotenuse is the absolute speed

#

Thats why the vertical speed is usinA and horizontal is ucosA

#

gravity only affects the vertical

dull shuttle
young solstice
dull shuttle
#

yeah

young solstice
#

well that's variable

#

depends on where the user points the grenade

#

could be higher, lower

dull shuttle
#

ok so basically $h=u\sin(A)t+\frac{1}{2}(-9.8)t^2$

quaint parrotBOT
#

ImOakley

dull shuttle
#

h is the landing altitude, t is time, A is angle, u is launch speed

#

so $-4.9t^2+u\sin(A)t-h=0$

quaint parrotBOT
#

ImOakley

dull shuttle
#

using quadratic formula $t=\frac{-u\sin{A}+\sqrt{u^2\sin^2A-4(-4.9)(-h)}}{2(-4.9)}$

quaint parrotBOT
#

ImOakley

dull shuttle
#

not plus or minus because it should be the higher of the two values for t

#

this value should be equal to $\frac{s}{u\cos{A}}$ since the horizontal distance is $s=u\sin{(A)}t$

quaint parrotBOT
#

ImOakley

dull shuttle
#

you would use this to find a value for A

#

its a lot less complicated if the end altitude is 0

#

because then $-4.9t^2+u\sin(A)t=0$

quaint parrotBOT
#

ImOakley

dull shuttle
#

so either $t=0$ or $-4.9t+u\sin{A}=0$

quaint parrotBOT
#

ImOakley

dull shuttle
#

obviously the latter would be correct since t cant be zero in this context

#

so just $t=\frac{u\sin{A}}{4.9}$

quaint parrotBOT
#

ImOakley

dull shuttle
#

then you would have $\frac{s}{u\cos{A}}=\frac{u\sin{A}}{4.9}$

quaint parrotBOT
#

ImOakley

dull shuttle
#

$s=\frac{u^2\sin{A}\cos{A}}{4.9}$

quaint parrotBOT
#

ImOakley

dull shuttle
#

theres a trig equation you dont need to worry about where $2\sin{A}\cos{A}=\sin{2A}$

quaint parrotBOT
#

ImOakley

dull shuttle
#

so $s=\frac{u^2\sin{2A}}{9.8}$

quaint parrotBOT
#

ImOakley

dull shuttle
#

$\sin{2A}=\frac{9.8s}{u^2}$

quaint parrotBOT
#

ImOakley

dull shuttle
#

$2A=\sin^{-1}{(\frac{9.8s}{u^2})}$

quaint parrotBOT
#

ImOakley

dull shuttle
#

so theres the equation you need for the angle

#

divided by 2

#

but only if its on ground level

quaint parrotBOT
#

ImOakley

dull shuttle
#

ok so including different altitude its this freaking mess: $$A=\tan^{-1}{\frac{u^2s-u^2\sqrt{s^2-\frac{96.04s^4+19.6s^2u^2h}{u^4}}}{9.8s^2}}$$

quaint parrotBOT
#

ImOakley

dull shuttle
#

simplified to this: $$A=\tan^{-1}{(\frac{u^2-\sqrt{u^4-96.04s^2-19.6u^2h}}{9.8s})}$$

quaint parrotBOT
#

ImOakley

young solstice
#

holy

#

okay but, if i need 3 coordinates for the angle, do i just replace u with each coordinate of the launch speed?

dull shuttle
#

like whats the input

young solstice
#

origin position (XYZ Vector) and launch direction (XYZ Unit Vector)