Help with Rotation

Blitz3D Forums/Blitz3D Beginners Area/Help with Rotation

Black Hydra(Posted 2004) [#1]
I've been having trouble with rotation.

In my code I need to turn a set of X and Y angles into
a vector. Getting the YRotation to represent the vector
has been pretty easy. Getting the XRotation to translate into
a YValue has been difficult.

At first I tried this expression hoping that I could divide my
rotation 'sphere' into a circle of a smaller proportional size
where I could then gather X and Y values with this code:

Function AngToVect.Vector(YAngle#, XAngle#)
Y# = sin(XAngle#)
Radius# = 1.0-Abs(Cos(XAngle#))
X# = cos(YAngle#)*Radius#
Z# = sin(YAngle#)*Radius#
NewVect.Vector = New Vector
NewVect\X# = X#
NewVect\Y# = Y#
NewVect\Z# = Z#
Return NewVect
End Function

Unfortunately that didn't work at all.

So then I gave up on math and tried using the Move function with pivot
entities to try to gain coordinate values like:

Function AngToVect.Vector(yAngle#, xAngle#)
MovePivot = CreatePivot()
PositionEntity(MovePivot, 0, 0, 0)
RotateEntity(MovePivot, xAngle#, yAngle#, 0)
MoveEntity(MovePivot, 0, 0, 1)
;Turn the pivot results into the vector
NewVect.Vector = New Vector
NewVect\X# = EntityX(MovePivot)
NewVect\Y# = EntityY(MovePivot)
NewVect\Z# = EntityZ(MovePivot)
FreeEntity MovePivot
Return NewVect
End Function

Nope, that doesn't work either...

So now, here I am asking for some help. Can anyone tell me how I can
convert a set of angles into a normalized 3D vector?

I'd like to do it with math preferably, but getting it working is more important
than whether I take longer and use pivot entities or not...

I'd like to make my first week using Blitz a good one. :)


jhocking(Posted 2004) [#2]
I don't know what that radius thing was all about. Just get the cosine and sine of the angle (no multiplying by any radius) and use those values for the vector.

EDIT: Oh wait, I think I just figured out what you're talking about. Ignore my sass.


Ross C(Posted 2004) [#3]
What you could do is this. Have a pivot parented to your player or whatever your using. Move this one blitz unit ahead of it. Something like: (press left mouse button to move. Arrow keys to turn left,right,up and down)

Graphics3D 800,600
SetBuffer BackBuffer()

light = CreateLight()

Global cam = CreateCamera()
PositionEntity cam,0,0,-10

Global cube = CreateCube()
Global campivot = CreatePivot(cube)
PositionEntity campivot,0,2,-7

Global pivot = CreatePivot()
EntityParent pivot,cube

PositionEntity pivot,0,0,1


Dim backstuff(20)
For loop = 0 To 20
	backstuff(loop) = CreateCube()
	PositionEntity backstuff(loop),Rnd(-20,20),Rnd(-20,20),Rnd(-20,20)
Next




While Not KeyHit(1)

	If KeyDown(203) Then RotateEntity cube,EntityPitch(cube),EntityYaw(cube)+1,EntityRoll(cube)
	If KeyDown(205) Then RotateEntity cube,EntityPitch(cube),EntityYaw(cube)-1,EntityRoll(cube)
	If KeyDown(200) Then RotateEntity cube,EntityPitch(cube)+1,EntityYaw(cube),EntityRoll(cube)
	If KeyDown(208) Then RotateEntity cube,EntityPitch(cube)-1,EntityYaw(cube),EntityRoll(cube)	

	If MouseDown(1) Then MoveEntity cube,0,0,0.1

	updatecamera()
	UpdateWorld
	RenderWorld
	Text 0,0,"x vector="+(EntityX(pivot,True)-EntityX(cube,True))+"  y vector="+(EntityY(pivot,True)-EntityY(cube,True))+"  z vector="+(EntityZ(pivot,True)-EntityZ(cube,True))
	Flip
Wend
End

Function updatecamera()

	PointEntity cam,campivot
	If EntityDistance#(cam,campivot) > 0.5 Then
		MoveEntity cam,0,0,EntityDistance#(cam,campivot)/20
	End If
	PointEntity cam,cube
	
End Function


Basically, you have a pivot parented to the cube, exactly one unit ahead of the cube. You subtract the pivots GLOBAL X Y and Z from the cube's GLOBAL X Y and Z. The rest of that code up there is just so you can see it in action. Hope that helps :o)


Black Hydra(Posted 2004) [#4]
Yeah this is for a 3D vector, 2D is easy.

The radius was meant so that x and z values would be taken from a 'cross-section' circle of a larger sphere.

However, I must have done something wrong there as that doesn't work.

Edit:
Wow you posted just a few seconds before me!

I'll try parenting a pivot to my entity and see how it goes.
I was hoping for some formula's, but as long as it works I'm grateful.

Thanks.


Ross C(Posted 2004) [#5]
Well, if you run that code above, it gives the x y and z vectors :o)