Facing the Direction your moving.

Blitz3D Forums/Blitz3D Beginners Area/Facing the Direction your moving.

What would you say is an Efficient way to turn an Entity to face the direction it is moving.

Regards,
ERic

You could use aligntovector(). Asumming you are using global translations then aligntovector entity, xspeed#, yspeed#, zspeed#, 3, 0.2 for instance.

I am using Atan2 (XVelocity, ZVelocity) Seems to work really well until I'm almost at a stand still.

DJ, Can you explain a little more?

Eric, if you're using atan2 it'll probably work fine if you just multiply up your velocities. ie atan2(xvel*1000,zvel*1000) This will reduce the problem, then you just have to say if abs(xvel)+abs(zvel)<threshhold then don't to the atan2 stuff

Yeah, I was playing with that, but when the Threshold is met it snaps to the new angle. This can't be that hard.

:(

This uses AlignToVector:

	Graphics3D 800,600,32
	SetBuffer BackBuffer()

	SeedRnd MilliSecs()

	fps_timer = CreateTimer(60)
	
	cam = CreateCamera()
	PositionEntity cam,0,0,-25
	
	light = CreateLight()
	RotateEntity light,90,0,0
	 
	Global player = CreateCone(16)
	RotateMesh player,90,0,0
	UpdateNormals player
	EntityColor player,255,0,255
	
	Const NUM_WAYPOINTS% = 10

	Type waypointT
		Field ent
	End Type

	For n = 1 To NUM_WAYPOINTS
		wp.waypointT = New waypointT
		wp\ent = CreateSphere()
		ScaleEntity wp\ent,.5,.5,.5
		PositionEntity wp\ent,Rnd(-10,10),Rnd(-10,10),Rnd(-10,10)
	Next

	Global vector_x#, vector_y#, vector_z#
	Global target_wp.waypointT
	Global speed# = .1, rate#
	Global move_x#, move_y#, move_z#

	set_waypoint_target()
	
	; Main loop
	While Not KeyHit(1)

		If EntityDistance(player,target_wp\ent) < speed Then set_waypoint_target()

		AlignToVector player,vector_x,vector_y,vector_z,3,rate
		TranslateEntity player,move_x,move_y,move_z

		RenderWorld

		WaitTimer(fps_timer)
		Flip(1)

	Wend

	End


Function set_waypoint_target()

	If target_wp <> Null Then EntityColor target_wp\ent,255,255,255
	
	If (target_wp = Null) Or (target_wp = Last waypointT)
		target_wp = First waypointT
	Else
		target_wp = After target_wp
	EndIf
	EntityColor target_wp\ent,255,0,0
	
	vector_x = EntityX(target_wp\ent,1) - EntityX(player,1)
	vector_y = EntityY(target_wp\ent,1) - EntityY(player,1)
	vector_z = EntityZ(target_wp\ent,1) - EntityZ(player,1)

	dist# = EntityDistance(player,target_wp\ent)
	move_x = (vector_x/dist) * speed
	move_y = (vector_y/dist) * speed
	move_z = (vector_z/dist) * speed

	rate = 1.0/((dist/4)/speed)
	
End Function


You could create a pivot that is parented to the object, but a bit in front, and just say "PointEntity object, pivot" or something.

I'm a bit confused here. Why not just use rotateentity(e,0,angle,0), and then move it using moveentity?

Well, I thought he wanted to gradually turn to point in the direction of movement. Maybe I misunderstood. :/

I think that was what he meant. Who knows ;)

Anyway, nice example. I tried to add some acceleration and deceleration. Probably better ways of doing it though. I don't really grasp the physics so what I have come up with seems to do the job, but it doesn't really. Although not obvious it accelerates out smoothly but it then decelerates and accelerates in fits and bursts to the target. Not very fuel efficient.

	Graphics3D 800,600,32,2
	SetBuffer BackBuffer()

	SeedRnd MilliSecs()

	fps_timer = CreateTimer(50)
	
	cam = CreateCamera()
	PositionEntity cam,0,0,-40
	
	light = CreateLight()
	RotateEntity light,90,0,0
	 
	Global player = CreateCone(16)
	RotateMesh player,90,0,0
	UpdateNormals player
	EntityColor player,255,0,255
	
	Const NUM_WAYPOINTS% = 15

	Type waypointT
		Field ent
	End Type

	For n = 1 To NUM_WAYPOINTS
		wp.waypointT = New waypointT
		wp\ent = CreateSphere()
		ScaleEntity wp\ent,.5,.5,.5
		PositionEntity wp\ent,Rnd(-20,20),Rnd(-20,20),Rnd(-10,20)
	Next

	Global vector_x#, vector_y#, vector_z#
	Global target_wp.waypointT
	Global accel# = .1, rate#, vel#
	Global move_x#, move_y#, move_z#

	set_waypoint_target()
	
	; Main loop
	While Not KeyHit(1)
	
		dist# = EntityDistance(player,target_wp\ent)

		If dist < vel Then set_waypoint_target() : Vel=0

		If dist>accel*(vel/accel)^2 Then vel=vel+accel Else vel=vel-accel

		mx# = move_x * vel
		my# = move_y * vel
		mz# = move_z * vel

		rate = vel/5

		AlignToVector player,vector_x,vector_y,vector_z,3,rate
		TranslateEntity player,mx,my,mz

		RenderWorld
		Text 0,0,"Dist "+dist+" Vel "+Vel+" Accel "+accel
		WaitTimer(fps_timer)
		Flip(1)

	Wend

	End


Function set_waypoint_target()

	If target_wp <> Null Then EntityColor target_wp\ent,255,255,255
	
	If (target_wp = Null) Or (target_wp = Last waypointT)
		target_wp = First waypointT
	Else
		target_wp = After target_wp
	EndIf
	EntityColor target_wp\ent,255,0,0
	
	vector_x = EntityX(target_wp\ent,1) - EntityX(player,1)
	vector_y = EntityY(target_wp\ent,1) - EntityY(player,1)
	vector_z = EntityZ(target_wp\ent,1) - EntityZ(player,1)

	dist# = EntityDistance(player,target_wp\ent)
	move_x = (vector_x/dist)
	move_y = (vector_y/dist)
	move_z = (vector_z/dist)
	
End Function


Thanks, for the Ideas, I am using Tokamak, I still have not given up on it yet.. Anyhow.. There are commands for VelocityX and VelocityZ.. I use these in the ATAN2 Function, And Set my RigidBody's Yaw to match The Resulting ATAN2 Function. Then I align the Entity to Match the RigidBody. I will look at this code and see if it makes sense. Thanks for the Help.

Regards,
Eric