B3D's Entity Rotation

Blitz3D Forums/Blitz3D Programming/B3D's Entity Rotation

It is annoying the heck out of me how the rotation does not go from 0 to 359. My program is trying to get the entity's yaw and it bothers the heck out of me how the yaw can be 180 or -180.

Anyone have some functions to return rotation values from 0 to 359?

This seems to work...
Graphics3D 640, 480, 0, 2

cam = CreateCamera()
MoveEntity cam, 0, 0, -5

c = CreateCube()

Repeat
	cr =cr+1
	RotateEntity c, 0, cr, 0
	
	RenderWorld
	Text 0, 20, EntityYaw(c)
	Text 0, 0, TEntityYaw(c)
	Flip
Until KeyHit(1)
End

Function TEntityYaw#(entity)
	ey# = EntityYaw(entity)
	If ey>=-180 And ey <=0
		ey = (360+ey)
		DebugLog ey
		Return ey
	ElseIf ey >= 0 And ey <=180
		Return ey
	EndIf
End Function


use:

if ey<0
ey = ey+360
return ey
else
return ey
endif

ey will always be>=-180 :)

Got it before you did... ja! :p

How about this one ..

Ey = Ey + 360.0 * ( EntityYaw( Entity ) < 0 )

;)

B3D's way of doing it is actually pretty nice, since you can use the yaw directly in a calculation for AI purposes. it saves alot of confusion; Turntonorm=-Entityyaw(mahmama)/10.0 slowly turns the entity back to 0.
It's useful to avoid alot of AI coding and because of this allows faster AI code. (Thats how I use it ;) )

Agree with Cyg. It's far more logical and useful in 3d when you get used to it.

Stevie

It's just when you want to use it to turn the entity in relation to the camera ie mario64 that the -180 rotation system gets tricky.

@ sinu ...

Completely disagree. Try this short example ... A + S to rotate the camera, cursors to move. No need to worry about the rotation system ;)


Graphics3D 640,480,16,1

Global LIGHT = CreateLight()
Global PLANE = CreatePlane() 
EntityColor PLANE, 200,100,100
texture = CreateTexture( 32, 32 )
SetBuffer TextureBuffer( texture )
For y = 0 To 1
	For x = 0 To 1
		Color 100, 150+25*( ( x+y) Mod 2 ) , 100
		Rect x*16,y*16,16,16,1
	Next
Next
SetBuffer BackBuffer()
ScaleTexture texture, 50,50
EntityTexture PLANE, texture
FreeTexture texture

Global CUBE = CreateCube() : FitMesh CUBE, -2,0,-2,4,10,4
Global CAMERApivot = CreatePivot()
Global CAMERA = CreateCamera( CAMERApivot )
PositionEntity CAMERA, 0,50,-50
PointEntity CAMERA , CAMERApivot

While Not KeyDown(1)

	PositionEntity CAMERApivot , EntityX( cube ) , 0, EntityZ( cube )
	TurnEntity CAMERApivot, 0 , KeyDown(31) - KeyDown(30) , 0
	Mx# = KeyDown(205) - KeyDown(203)
	Mz# = KeyDown(200) - KeyDown(208 )
	TFormVector Mx, 0, Mz , CAMERApivot , 0
	TranslateEntity CUBE, TFormedX(), 0, TFormedZ()

	RenderWorld()
	Flip

Wend


Stevie

@Stevie G

Am i missing something, all that demo shows is how to translate the entity correctly when turning a camera around it, not actually turning the entity to face the right direction too?

Sorry mate .. is this what you mean?

Graphics3D 640,480,16,1

Global LIGHT = CreateLight()
Global PLANE = CreatePlane() 
EntityColor PLANE, 200,100,100
texture = CreateTexture( 32, 32 )
SetBuffer TextureBuffer( texture )
For y = 0 To 1
	For x = 0 To 1
		Color 100, 150+25*( ( x+y) Mod 2 ) , 100
		Rect x*16,y*16,16,16,1
	Next
Next
SetBuffer BackBuffer()
ScaleTexture texture, 50,50
EntityTexture PLANE, texture
FreeTexture texture

Global CUBE = CreateCube() : FitMesh CUBE, -2,0,-2,4,10,4 
Tmp = CreateCube() : FitMesh Tmp, -1,7,2,2,2,2 : AddMesh Tmp, CUBE : FreeEntity Tmp
Global TARGET = CreatePivot()

Global CAMERApivot = CreatePivot()
Global CAMERA = CreateCamera( CAMERApivot )
PositionEntity CAMERA, 0,50,-50
PointEntity CAMERA , CAMERApivot

While Not KeyDown(1)

	PositionEntity CAMERApivot , EntityX( CUBE ) , 0, EntityZ( CUBE )
	TurnEntity CAMERApivot, 0 , KeyDown(31) - KeyDown(30) , 0
	
	;move relative to camera
	Mx# = KeyDown(205) - KeyDown(203)
	Mz# = KeyDown(200) - KeyDown(208 )

	If Mx <> 0 Or Mz <> 0
		;move relative to camera
		TFormVector Mx, 0, Mz , CAMERApivot , 0
		TranslateEntity CUBE, TFormedX(), 0, TFormedZ()
		;turn relative to movement
		PositionEntity TARGET, EntityX( CUBE ) + TFormedX() , 0, EntityZ( CUBE ) + TFormedZ()
		TurnEntity CUBE, 0 , DeltaYaw( CUBE, TARGET ) * .25, 0
	EndIf
	
	RenderWorld()
	Flip

Wend


Stevie

Yep, i only discovered the coolness of the Delta commands recently and didn't think of using in this way, cheers :)