360 Degree Formulas
Author: | RyGuyX
|
Submitted: | 5th December, 2003
|
Views: | 7098
| Rated: |
|
|
I wrote this in an email to a friend once, talking about how to do the coding for one of my top-view shoot-em-up's currently in production. I hope it's understandable enough, my friend understood it.
EDIT: MUST THE LETTERS X AND D NEXT TO EACH OTHER ALWAYS PRODUCE THIS ABOMINATION?: . I had to go back and change IST to X.Dist
--------------------
No matter how many times I code it, it always ends up trial and error... it all depends on how you want things to work... but basically, there are few foundation formulas.
Setting direction relative to angle.
Set direction = angle / ( 360 / 32 )
Getting the X distance of an object relative to another object
Set X.DIST = X(obj2) - X(obj1)
Getting the Y distance of an object relative to another object
*The Y axis in TGF and MMF is inverted. I'm sure you know this but may have never realized it's significance. Therefore, we have to invert it back. So we just do the opposite of what we did to get the X.dist. This is crucial for later values.
Set YDIST = Y(obj1) - Y(obj2)
Getting the total distance from an object to another object
Set TDIST = sqr ( [X.DIST] pow 2 + [YDIST] pow 2 )
Getting the angle of obj2 relative to obj1
*this one is tricky and involves a few steps, because it involves the total distance, in which a square root is taken, negating positives and negatives, leaving us without angles between 90 and 270 degrees.
Set valueA = asin( [YDIST] / [TDIST] ) * ( 180 / pi )
IF X(obj2) >= X(obj1) { set valueB = valueA }
IF X(obj2) < X(obj1) { set valueB = ( 180 - valueA )
ValueB is the angle
Now using these formulas, you can do alot of things. There are a few ways of doing Bullets, this is one way you can do it, although it has limitations. It's basically a rotation formula with ever-increasing radius values. By the way - do this all in fastloops to make the bullets go faster.
- Set valueR = valueR + 1
- if Flag 1 of bullet is off { set [original X position] to X(bullet) AND set [original Y position] to Y(bullet) AND set [angle] to ValueB(obj1) AND set Flag 1 of bullet ON }
- Set X(bullet) = cos( [angle] ) * valueR + [original X position]
- Set Y (bullet) = sin( [angle] ) * valueR + [original Y position]
This way is much better. The position is set to values (multiplied by 1000 for greater accuracy), then the X and Y Velocities are set in relation to the angle, and constatntly added to the position values, then the position is re-cacluated using the position values divided by 1000.
-create bullet at the gun
-if Flag 1 of bullet is off { set [xpos] to ( X(bullet) * 1000 ) AND set [ypos] to ( Y(bullet) * 1000 ) AND set [angle] to ValueB(obj1) AND set Flag 1 of bullet ON }
-add: sin ( [angle] ) * 1000 to [ypos]
-add: cos( [angle] ) * 1000 to [xpos]
-set X(bullet) to [xpos] / 1000
-set Y(bulet) to [ypos] / 1000
If you get it: good. If you don't: try to understand the basic concepts and explore it on your own.
Other than all of those formulas, the rest is just finding collisions in fastloops and such.
Happy Game Making!
|
|
RyGuyX
Registered 29/08/2002
Points 495
|