Move entity relative to camera with mouse help.

Blitz3D Forums/Blitz3D Programming/Move entity relative to camera with mouse help.

Vertigo(Posted 2012) [#1]
Hello. I am using atan2 with the mouse x and y compared to screen center to get an angle. Then using tformnormal to get coords relative to the camera's orientation. I must not be doing something right as I cannot seem to get the results I want. I am trying to have an entity deltayaw to the angle of the mouse click. For instance, regardless of how the camera is rotating/pointing, if I click to the far right side of the screen I want the entity to face that direction relative to the camera.

Can anyone help?


Ross C(Posted 2012) [#2]
I think tforming co-ords for moving the entity from camera space into world space is the way to go. TFormVector?


Vertigo(Posted 2012) [#3]
TFormVector returns the magnitude as well, I just need the direction. TFormNormal should work fine for this. However converting the mouse position as an angle to rotate the player entity relative to camera rotation is not working right. It seems to only be apply a 180 degree range. And yes I converted atan2's output to degrees.


Ross C(Posted 2012) [#4]
Do you have any code we/I could test?


Matty(Posted 2012) [#5]
Use aligntovector...

from memory the syntax is

aligntovector entity,vector x,vector y,vector z,axis to align,rate

simply pass the entity, the tformedx/y/z and the axis (in this case most likely 3 (z)), and the rate (1.0 most likely)....

very useful command.


Vertigo(Posted 2012) [#6]
Here is what I have:
(pseudo code)

TFormNormal(mousex-ScreenCenterX, 0, mousey-ScreenCenterY, Camera_Entity, 0 )
angle# = atan2(-TFormedX(), -TFormedZ()) * 180 / PI
RotateEntity player, 0, angle, 0, true

And this works as long as the camera rotation is 0,0,0 looking at the player. If I were to say, move the camera to the right side of the player having a -90 yaw value then the whole thing gets offset.

I tried playing around with aligntovector, but where does the angle from the mouse click come into play? I am not wanting to do any camerapicks or anything.

Thanks for your help guys.


Matty(Posted 2012) [#7]
Try this

Graphics3D 512,512,0,2
camera=CreateCamera()
cone = CreateCone()
MoveEntity camera,0,0,-10
SetBuffer BackBuffer()

dirn#=1.0
Repeat
a# = a# + 0.001 * dirn
b# = b# + 0.0005 * dirn
frame = (frame + 1) Mod 360
If frame = 0 Then dirn = dirn * -1.0

TurnEntity camera,a,b,1


;point cone in the right direction

TFormNormal MouseX()-256,256-MouseY(),1,camera,0
AlignToVector cone,TFormedX(),TFormedY(),TFormedZ(),2,1
;TFormNormal 0,0,1,camera,0
;AlignToVector cone,TFormedX(),TFormedY(),TFormedZ(),3,1

RenderWorld
Flip


Until KeyDown(1)
End


Last edited 2012


Stevie G(Posted 2012) [#8]
I am not wanting to do any camerapicks or anything.


The solution using a camerapick on an invisible plane would be trivial, why do you not want to use this? Simply position a pivot at the picked location and do you're deltayaw based on this target.

If we are talking about anything other than a topdown view then your angle is never going to be accurate as perspective is not taken into consideration.

For top down view this works, relative to the pivot, which the camera is attached:

Graphics3D 640,480,16,1

Const HGW = GraphicsWidth()*.5
Const HGH = GraphicsHeight()*.5

Global Camera = CreateCamera() : PositionEntity Camera, 0,50,0 : RotateEntity Camera, 90,0,0
Global Cone = CreateCone() : RotateMesh Cone, 90,0,0 : ScaleMesh Cone, 1,1,2
Global Pivot = CreatePivot()
EntityParent Camera, Pivot

Global Dx#, Dz#, Yaw#

While Not KeyHit(1)

	TurnEntity Pivot, 0,.5, 0

	Dx# = MouseX() - HGW
	Dz# = HGH - MouseY()

	TFormPoint Dx, 0, Dz, Pivot, 0

	Yaw# = VectorYaw( TFormedX(), 0, TFormedZ() )
	RotateEntity Cone, 0, Yaw, 0

	RenderWorld()
	
	Text MouseX(), MouseY(), "*",1,1
	
	Text 0,0,Yaw
	
	Flip
	
Wend