hi everybody!
im coding a zelda-like game, and for the most part the engine aint
that hard, but I cant seem to figure out how to do angles. let me
expound.
in zelda (as many know) when you attack something, it sorta flies back in the opposite direction you attacked it from. my ? is how do you calculate angles using just x y coords? any help would be appreciated!
Well, this should be simple. In Zelda, if you attack something, it moves in the same direction Zelda is, right? Since there are only 8 directions you can go in Zelda, this shouldn't be much of a challenge. If you can move Zelda, you can now move this baddie.
heh man am i glad it wasnt that easy
just checked it out. you can actually hit em in any angle.
...
...HELP lol
okay then:
where angles is in radians (degrees * pi / 180):
Code:
x = x + cos(angle) * velocity
y = y + sin(angle) * velocity
angle should be the player's angle if the thing is getting thrown back from the player.
like toonski said, in zelda there are only 8 directions. when you hit an enemy, they fly back in the opposite direction they are moving. so if they are moving down, when you hit them they move up.
hey, if you want help in making it in any way, i'd be glad to help. i've always wanted to make a zelda clone.
heh i dont think you guys understand me
jofers: thats a good method to use... once you know the angles
thats what my ? was, to be able to take playerx, playery, enemyx, enemyy, and figure out which angle they are hit at...
barok: dude! no!
you have full 360 degree angles to hit the enemy at. its not just u,ur,r,dr,d,dl,l,ul.... try it.
thanks for th offer
we pretty much have it covered, unless you know how to do bionic animation
if you have playerX, playerY, enemyX, enemyY just follow the good ol' formula:
Code:
tangent = (playerY-enemyY)/(playerX-enemyX)
angle = ATN(targent)
ATN is ArcTaNgent, i.e. which angle has the specified tangent.
Hehe, ATN always gets the smallest angle, and will screw up calculations if you don't get angle checks up and running when using ATN.
Code:
E.g.:
/
/|
/ |
/ | y
/a |
/----|
x
Get a by: a = ATN(y / x)
BUT!:
\
|\
| \
|y \
| \
| x \a
------X-----
Here get a by a = 180 - ATN(y / x)
thanks a lot guys!!!!
i just have a couple of ?'s though.... ;(
first, when i do like PlayX - EnemX do I have to make sure the biggest is the one being subtracted from? or does it not matter?
and secondly. neo, i dont understand. how do you know when to subtract 180? God i shouldve taken maths
it's easier to just post some code, and since i'm a lazy sob, rel wrote it
Code:
FUNCTION Rel.Angle% (X1, Y1, X2, Y2)
'By RelSoft of Auraflow
'Parameters:
'x1,y1= the starting coord
'x2,y2=the target coord
'Returns=the Angle in degrees between the two coords
'useable with SIN and COS so this is useful for calculating the
'vectors between 2 points.
'This is the Fast BASIC version
'Rad=Deg*PI/180
'Rad*180=Deg*PI
'Rad*180/PI=Deg
'Deg=Rad*180/PI
IF X1 = X2 THEN
IF Y1 < Y2 THEN
Rel.Angle% = 90
EXIT FUNCTION
ELSEIF Y1 > Y2 THEN
Rel.Angle% = 270
EXIT FUNCTION
ELSE
Rel.Angle% = 0
EXIT FUNCTION
END IF
ELSE
DeltaX = X2 - X1
DeltaY = Y2 - Y1
AngleTemp% = ATN(DeltaY / DeltaX) * 180 / 3.141593
AngleTemp% = AngleTemp% + 90
IF X1 > X2 THEN
AngleTemp% = AngleTemp% + 180
END IF
END IF
AngleTemp% = AngleTemp% - 90
IF AngleTemp% < 0 THEN AngleTemp% = AngleTemp% + 360
Rel.Angle% = AngleTemp%
END FUNCTION
...and version 2:
Code:
FUNCTION Rel.Angle2% (X1, Y1, X2, Y2)
'By RelSoft of Auraflow
'Parameters:
'x1,y1= the starting coord
'x2,y2=the target coord
'Returns=the Angle in degrees between the two coords
'useable with SIN and COS so this is useful for calculating the
'vectors between 2 points.
'This is the Readable Version :*)
'Some convertions to help me :*)
'Rad=Deg*PI/180
'Rad*180=Deg*PI
'Rad*180/PI=Deg
'Deg=Rad*180/PI
CONST PI! = 3.141593
CONST Convert! = 180 / PI
DeltaX = X2 - X1
DeltaY = Y2 - Y1
IF DeltaX = 0 THEN
IF DeltaY < 0 THEN
Rel.Angle2% = 270
EXIT FUNCTION
ELSEIF DeltaY > 0 THEN
Rel.Angle2% = 90
EXIT FUNCTION
ELSE
Rel.Angle2% = 0
EXIT FUNCTION
END IF
ELSE
Slope! = DeltaY / DeltaX
Angle! = ATN(Slope!)
AngleTemp% = Angle! * Convert!
AngleTemp% = AngleTemp% + 90 'Error fix
IF X1 > X2 THEN
AngleTemp% = AngleTemp% + 180
END IF
END IF
'Fix the error in the fixer. Riiiight.
AngleTemp% = AngleTemp% - 90
IF AngleTemp% < 0 THEN AngleTemp% = AngleTemp% + 360
Rel.Angle2% = AngleTemp%
END FUNCTION