The Daily Click ::. Forums ::. Klik Coding Help ::. grenade arcs
 

Post Reply  Post Oekaki 
 

Posted By Message

Zi-Xiao



Registered
  29/07/2002
Points
  537

VIP Member
13th March, 2008 at 23:28:23 -

I've got a side scrolling game going. If the player is on a platform thats higher or lower than the AI, the AI will throw a grenade at the player. The code for the movement of my grenades look like this:

Always
Set Grenade Alterable Value X to Alterable Value X + Speed X
Set Grenade Alterable Value Y to Alterable Value Y - Speed Y
Set Grenade X position to Alterable Value X
Set Grenade Y position to Alterable Value Y
Set Alterable Value Y to Alterable Value Y - 1 <- gravity

Simple enough. So when the AI throws the grenade, I need to set it's Speed X and Speed Y to the appropriate values such that the grenade will actually hit the player. This technique is done in many games so I was wondering what technique one would use.

Zi-Xiao

 
n/a

Cecilectomy

noPE

Registered
  19/03/2005
Points
  305

Has Donated, Thank You!VIP MemberWeekly Picture Me This Winner!Cardboard BoxGhostbuster!Pokemon Ball!ComputerBox RedSanta HatSnowman
I am an April Fool
14th March, 2008 at 00:24:21 -

why not use quadratics?

 
n/a

Zi-Xiao



Registered
  29/07/2002
Points
  537

VIP Member
14th March, 2008 at 00:42:16 -

Cause they hurt my brain.

To be honest, they don't fit into the way the rest of the engine is structured. If I understand what you mean by quadratics, I'd have to either make x a function of y or vice versa, and that would run into problems with some other checks in the code.

Regardless, please elaborate on 'quadratics'. A solution in some shape or form would be nice.

 
n/a

Noyb



Registered
  31/05/2004
Points
  1117

VIP Member
14th March, 2008 at 02:45:00 -

I made an MMF example a little while back. It's not perfect, but it should be enough to get you started: http://www.create-games.com/forum_post.asp?id=176876&show=all

I think any inaccuracies might just be coming from not casting the integers as floats, so the divisions truncate any decimals, compounding the error. So multiply top and bottom of each fraction by 1.0 if you try that code. I don't have MMF1 working on this computer to test it out again, though.

Image Edited by the Author.

 
"Omg. Where did they get the idea to not use army guys? Are they taking drugs?" --Tim Schafer on originality in videogames

Cecilectomy

noPE

Registered
  19/03/2005
Points
  305

Has Donated, Thank You!VIP MemberWeekly Picture Me This Winner!Cardboard BoxGhostbuster!Pokemon Ball!ComputerBox RedSanta HatSnowman
I am an April Fool
14th March, 2008 at 05:07:21 -

quadratics are like this

quadratic equation is of the form y = ax^2 + bx + c

if you know a b and c you can find the x intercepts. quadratic formula is of the form x = (-b(+or-)sqrt(b^2 - 4ac))/2a

the equation is a parabolic function so if you know 3 points you can derive quadratic equation that goes through them. so you have your enemy which is point number 1 and your player which is point number 2. your third point is in between these two and has a greater y value than both of them. the greater the y value the higher the arc is going to be. id have to crack open an old math book to explain how to get the equation from the three points so i suggest you look it up. but youre right. it might not fit in with your engine.

theres another way to do an arc thats based on angle of projection using sin and cos, velocity, and gravity. that may be of more use to you. any way you look at it, an equation would better suit the engine than using an xspeed and yspeed and changing them as the grenade moves. vectors may be another way to do this as well.

any way you slice it, its gonna be a bit of math to determine how the grenade should move to strike its target.

good luck tho.

 
n/a

Joe.H

Evil Faker

Registered
  19/08/2002
Points
  3305
14th March, 2008 at 09:05:33 -

Image

V is the projection velocity
P is the angle of projection
X and Y are distances.

X speed at projection = V*cos(P)
Y speed = V*sin(P)

using various equations (calculate vertical and horizontal values independantly, time is usually the same in both)
v = u + at
v² = u² + 2as
s = ut + 0.5at²
s = 0.5(u+v)t

v is final velocity
u is initial velocity (or velocity at the point where your equation is calculated from)
s is distance
a is acceleration DOWNWARDS (i.e if your object is travelling upwards, and you are taking upwards velocity to be positive, acceleration would be negative)

horizontal acceleration is 0

velocities can be in pixels/second or pixels/frame, time must be in the corresponding value (i.e if v in pixels/second, time is in seconds)
distances are in pixels

vertical velocity at the midpoint (highest point acheived from the ground, usually half the time taken to complete path) is 0

Something like this may be simpler than using quadratic equations, as you can find variables quite easily.

Image Edited by the Author.

 
My signature is never too big!!!

Cecilectomy

noPE

Registered
  19/03/2005
Points
  305

Has Donated, Thank You!VIP MemberWeekly Picture Me This Winner!Cardboard BoxGhostbuster!Pokemon Ball!ComputerBox RedSanta HatSnowman
I am an April Fool
14th March, 2008 at 20:48:35 -

yup thats the one. lol.
i did this first year of highschool 5 1/2 years ago. its all evaporated from my mind

 
n/a

Klikmaster

Master of all things Klik

Registered
  08/07/2002
Points
  2599

Has Donated, Thank You!You've Been Circy'd!VIP MemberPS3 Owner
15th March, 2008 at 05:14:43 -

SUVAT Equasions! I remember them! Excellent stuff for projectiles

 
n/a

Zi-Xiao



Registered
  29/07/2002
Points
  537

VIP Member
15th March, 2008 at 12:39:05 -

I'm afraid all of this is way above my head. A quick mfa would be nice

 
n/a

Cecilectomy

noPE

Registered
  19/03/2005
Points
  305

Has Donated, Thank You!VIP MemberWeekly Picture Me This Winner!Cardboard BoxGhostbuster!Pokemon Ball!ComputerBox RedSanta HatSnowman
I am an April Fool
15th March, 2008 at 17:00:50 -

get used to having to know math. thats all game making is about. it varies from the mundanely simple to extremely complex.

 
n/a

Noyb



Registered
  31/05/2004
Points
  1117

VIP Member
15th March, 2008 at 21:03:44 -

Erm, did you not see my example in the thread I linked to?
http://realnoyb.googlepages.com/arc.cca

Edit: Updated to fix floating point precision bug. The grenades go relatively fast, so it's probably a good idea to make it so the grenades are moved in a loop.

Image Edited by the Author.

 
"Omg. Where did they get the idea to not use army guys? Are they taking drugs?" --Tim Schafer on originality in videogames

Zi-Xiao



Registered
  29/07/2002
Points
  537

VIP Member
16th March, 2008 at 21:25:29 -

Sorry Nyob, must have missed it some how. Its a great example and I found it to be amazingly accurate. I'll examine it in detail later, but from a quick look, it seems the arc is a tad high. Some tweaking will hopefully remedy that

 
n/a
   

Post Reply



 



Advertisement

Worth A Click