Entity turning code with max rate...

Blitz3D Forums/Blitz3D Programming/Entity turning code with max rate...

OrcSlayer(Posted 2007) [#1]
Anyone have (or could create) some simple code to turn an entity towards another (like pointentity) while having a cap on the maximum turn rate (in degrees, just like when using turnentity)?

It would be nice if such a function could also return true if the entity is pointing directly at the target entity, would make the code very useful for a whole range of requirements.

Doesn't sound to hard, but that's just not my area of expertise...


GfK(Posted 2007) [#2]
You can use DeltaPitch() and DeltaYaw() to determine if an entity needs to turn up/down or left/right. You can also use If DeltaYaw(Entity1,Entity2) < 1 to determine if an entity is pointing directly at an other.
MaxTurnSpeed# = 5
turnSpeed# = 0
turnTargetSpeed# = 0

If DeltaYaw(Entity1,Entity2) < 0
  turnTargetSpeed = 0-MaxTurnSpeed
Endif
If DeltaYaw(Entity1,Entity2) > 0
  turnTargetSpeed = MaxTurnSpeed
Endif
If DeltaYaw(Entity1,Entity2) < 0.1
  turnTargetSpeed = 0
Endif

If turnSpeed > turnTargetSpeed
  turnSpeed = turnSpeed - 0.1
endif
If turnSpeed < turnTargetSpeed
  turnSpeed = turnSpeed + 0.1
endif
if abs(turnSpeed - turnTargetSpeed) < 0.1
  turnSpeed = turnTargetSpeed
endif

TurnEntity Entity,0,turnSpeed,0


I haven't coded in Blitz3d for a couple of years now (typed the above straight into reply box so I can't guarantee it'll work straight off), but this should point you in the right direction (no pun intended).


OrcSlayer(Posted 2007) [#3]
Hmmm, I cleaned up the code a bit and tried to use it as a function, but the result is that the entity turns at an agonizingly slow rate, or doesn't turn at all in some cases (not sure why). Probably something simple...here's what I did.

Edit: Code removed, see next post.


OrcSlayer(Posted 2007) [#4]
I just realised I didn't even know what DeltaYaw DID until just now, since my documentation is out of date...was wondering why I couldn't read it in program.

Now that I know how it works, I should be able to fix this myself...it's nowhere near as complex as I was worried it would be.

Thanks for pointing me in the right direction!

Edit: Here's a simple function I made out of Gfk's example, which should work right out of the box. If anyone needs it, use as you like.

Function LimitedTurn(e1,e2,MaxTurnSpeed#=5)
	TurnSpeed# = 0
	FinalYaw# = DeltaYaw(e1,e2)
	If FinalYaw < 0 Then
		If FinalYaw > -MaxTurnSpeed Then
			TurnSpeed = FinalYaw
		Else
			TurnSpeed = -MaxTurnSpeed
		EndIf
	ElseIf FinalYaw > 0 Then
		If FinalYaw < MaxTurnSpeed Then
			TurnSpeed = FinalYaw
		Else
			TurnSpeed = MaxTurnSpeed
		EndIf
	EndIf
	TurnEntity(e1,0,TurnSpeed,0)
End Function



Stevie G(Posted 2007) [#5]
Here's a simpler function ...

Function LimitedTurn(e1,e2,MaxTurnSpeed#=5)
	Turn# = DeltaYaw(e1,e2)
	If Abs( Turn ) > MaxTurnSpeed Turn = Sgn( Turn ) * MaxTurnSpeed
	TurnEntity(e1,0,Turn,0)
End Function



slenkar(Posted 2007) [#6]
Function LimitedTurn(e1,e2,MaxTurnSpeed#=5)
	Turn# = DeltaYaw(e1,e2)
	If Abs( Turn ) > MaxTurnSpeed
 Turn = Sgn( Turn ) * MaxTurnSpeed
	TurnEntity(e1,0,Turn,0)
else
pointentity e1,e2
return 1
endif
End Function



that returns if it has finished turning


OrcSlayer(Posted 2007) [#7]
I found another good way to check if the entity is facing the target is another deltayaw check (well, it never quite seems to return 0, but if the absolute value of the result is less than 1 it works pretty well too. Depends on how accurate it needs to be, I suppose.


GfK(Posted 2007) [#8]
I found another good way to check if the entity is facing the target is another deltayaw check (well, it never quite seems to return 0, but if the absolute value of the result is less than 1 it works pretty well too.
I told you that two days ago. :p

You can also use If DeltaYaw(Entity1,Entity2) < 1 to determine if an entity is pointing directly at an other.



Stevie G(Posted 2007) [#9]
@ Gfk, there's no ABS() in your condition so technically you didn't ;)


GfK(Posted 2007) [#10]
I haven't coded in Blitz3d for a couple of years now (typed the above straight into reply box so I can't guarantee it'll work straight off)

:p