turning one object towards another one

Blitz3D Forums/Blitz3D Programming/turning one object towards another one

ryan scott(Posted 2004) [#1]
i wan to give some intelligence to an enemy, making him turn towards the player a little bit each frame.

i am not sure how to make him turn a little towards him.

i found this, but it does a rotate, not a turn, and i'm not sure how to make it turn just part of the way, like 1% or whatever degree i specify. ATAN is a little beyond me.

Can anyone clue me in?

Function Point_Entity(entity,x#,y#,z#)
xdiff# = EntityX(entity)-x#
ydiff# = EntityY(entity)-y#
zdiff# = EntityZ(entity)-z#
dist#=Sqr#((xdiff#*xdiff#)+(zdiff#*zdiff#))
pitch# = ATan2(ydiff#,dist#)
yaw# = ATan2(xdiff#,-zdiff#)
RotateEntity entity,pitch#,yaw#,0
End Function

; really just want to turn, and a variable amt.


jhocking(Posted 2004) [#2]
Try using AlignToVector. You can set a tweening/turning rate parameter to do a smooth turn. If the entity turning is called "avatar" and the entity being faced is "target" the code is:
AlignToVector avatar,EntityX(avatar)-EntityX(target),0,EntityZ(avatar)-EntityZ(target),1,0.2

Call this every frame as part of the enemy's movement code.


ryan scott(Posted 2004) [#3]
hmm..

they are driving around him in a circle. i don't really know why.

movement code is just 'move him forward a little (its different for each enemy so fast and slow were both tested)' then 'aligntovector' just like you have it there. also tried instead of .2, 1 but its exactly the same result, they circle the player. same direction every time, never getting closer to the player. if i move the player, they will actually veer away to avoid him and keep that constant circle around him.

i am not sure why this wouldn't work, it seems to make sense.

changing the last parameter has no effect on the tightness of the circle.


ryan scott(Posted 2004) [#4]
fixed it

e\model is the enemy
car is my player.


AlignToVector e\model,EntityX(car,1)-EntityX(e\model,1),0,EntityZ(car,1)-EntityZ(e\model,1),3,.2

i don't know why this works. shouldn't 3 be 'roll'?


Matty(Posted 2004) [#5]
The 3 signifies that you are aligning the object's Z axis to the vector you specify. Think of the front of your car having a long arrow pointing out of it forwards representing the z axis. When you call align to vector with "3" as the axis parameter the "arrow" is rotated so that it faces in the same direction as the vector you are aligning it to. Very useful function.


jhocking(Posted 2004) [#6]
Yeah, what he said. I forgot to mention, that you set the axis to align when calling AlignToVector. You want that axis to be the forward direction of the object.

Another caveat is that AlignToVector is sensitive to the framerate. You can compensate for this by dividing the last parameter by the framerate, or multiplying by the time between frames. Doing this obviously you'll need also need to change the number but once that's done it'll all be good.