Angles. and rotation..

BlitzMax Forums/BlitzMax Beginners Area/Angles. and rotation..

Paul "Taiphoz"(Posted 2011) [#1]
I'm about to start a small test project, little bit of code to see if a basic game concept will work out, and once thing that I will need to do a lot is turn one sprite to face a given position on the screen, but last time I worked with angles I recall max was different to blitz3D.

Does anyone have any bullet proof way of handling this ? not touched it in ages id rather not fumble around and fall into old habbits its the perfect chance for me to learn a better way of doing it.


Midimaster(Posted 2011) [#2]
are you talking about 2D or 3D? What do you mean with "face"?

BlitzMax has only 2D drawing possibilities. So how you make a sprite "face" a given position?

I think we need more information about the visual situation you are expecting...


GfK(Posted 2011) [#3]
Atan2() using the coordinates of the player and enemy.

angle = atan2(ey-py, ex-px)


Paul "Taiphoz"(Posted 2011) [#4]
Gfk yeah thats what I would do but when it comes to taking that value and making it point my sprite in the right direction I always get all screwed up I work it out in the end but its always a pain in the ass.

I think its cos it yields -180,180 response as I recall, and I always instinctively try something like pointenemy(180) and expect it to face down the screen when in fact it ends up facing left or something.

I recall writing a function that takes that daft angle system and converts it to a 0/360 system but I cant find it, guess im gona have to do it again.


Paul "Taiphoz"(Posted 2011) [#5]
Oh sorry yeah 2D at the moment while I proof the concept out, but then with everything working I plan to port it all to 3D, in reality very little code will need to change from 2D to 3D so doing it in 2D first as it does not require a ton of time spend on art assets.

which of course means I have more time on code, with my limited time for blitz, having more time on it is a good thing.

Last edited 2011


GfK(Posted 2011) [#6]
Gfk yeah thats what I would do but when it comes to taking that value and making it point my sprite in the right direction I always get all screwed up I work it out in the end but its always a pain in the ass.
SetRotation(Angle)
DrawImage imgPlayer,px,py

But you are confusing me with the to-ing and fro-ing between 2d and 3d buzzwords. I really don't see the point in writing something in 2d, then rewriting it all in 3d afterwards. I also don't understand how you think that changing it from 2d to 3d will be a quick process as surely all of your art assets would need to be replaced with meshes.


Jesse(Posted 2011) [#7]
the main thing you have to remember is that if your original image is not facing to the right at it's unrotated state then you have to do some adjustment to the setRotation angle before display.
if your image natural angle is facing up then just before display you need to do something like this:
setRotation(angle+90)

to avoid this kind of problems and if your images are not facing to the right use a paint program and turn and/or flip all of your images to face to the right.
then you can do as GFK prescribed.


Paul "Taiphoz"(Posted 2011) [#8]
So when I draw a new sprite I should face it right, I always have mine facing up dont know why just habbit I guess.


Jesse(Posted 2011) [#9]

So when I draw a new sprite I should face it right, I always have mine facing up dont know why just habbit I guess.


just about everybody face them up.

Don't worry about the negative number in the atan2. You still will get the correct results.

Last edited 2011


Midimaster(Posted 2011) [#10]
here is a code sample to demonstrate Atan2 without the "180° problems".

Graphics 400,300
	x#=100
	y#=100
	add#=.5
	xadd#=add
	yadd#=0
	kiX=200
	kiy=150

Repeat
	x=x+xadd
	y=y+yadd
	If xadd>0 And x>300
		xadd=0
		yadd=add
	ElseIf yAdd=add And y>200
		xadd=-add
		yadd=0
	ElseIf XAdd=-add And x<100
		xadd=0
		yadd=-add
	ElseIf yAdd=-add And y<100
		xadd=add
		yadd=0
	
	EndIf
	Cls
	SetColor 0,155,0
	DrawRect 98,98,210,110
	SetColor 255,0,0
	DrawOval kiX,kiY,5,5
	SetColor 0,0,255
	DrawOval x,y,5,5
	winkel%= ATan2(x-kiX,y-kiY)
	SetColor 255,255,255
	DrawText winkel,10,10
	DrawText "=" + ((winkel+360) Mod 360) + " Grad",10,30
	Flip 1
	
Until KeyHit(key_Escape)




There is also a article in the german board about "facing two objects in 2D without screwing problems" maybe it can help you:

http://www.blitzforum.de/forum/viewtopic.php?p=392344#392344


sample:
WorldWinkel = Atan2 ( GesuchterX - VerfolgerX ,  GesuchterY - VerfolgerY )
WorldWinkel = (WorldWinkel +360° ) mod 360°
ZielWinkel = WorldWinkel - SichtRichtung
If ZielRichtung < - 180°
    ZielRichtung = ZielRichtung + 360°
Endif
If ZielRichtung > + 180°
    ZielRichtung = ZielRichtung - 360°
Endif


you have to keep in mind that Atan2 results the WorldAngle (WorldWinkel). If your hunter already runs in a certain direction (SichtRichtung), you have to keep in mind this to calculate the required turn of the hunter (ZielRichtung). Only now you can decide how to turn...