Smart turning

Blitz3D Forums/Blitz3D Beginners Area/Smart turning

Ben(t)(Posted 2007) [#1]
I am making a turret for my a.i. test and I was wondering if there was a easy way to make the turret turn to face the player without pointentity,turret,player
right now I have

if entitydistance(turret,player) =< 25 and entitypick(turret,25)=player then
pointentity turret,player
fire#=fire#+1
else
turnentity turret,0,1,0
endif


I am trying to do a Z and X axis check but I'm a little confused
can someone help me?


Naughty Alien(Posted 2007) [#2]
Try this...

Function Point_Entity(entity,Target)
xt#=entityx(Target)
yt#=entityy(Target)
zt#=entityz(Target)
xdiff# = EntityX(entity)-xt#
ydiff# = EntityY(entity)-yt#
zdiff# = EntityZ(entity)-zt#
dist#=Sqr#((xdiff#*xdiff#)+(zdiff#*zdiff#))
pitch# = ATan2(ydiff#,dist#)
yaw# = ATan2(xdiff#,-zdiff#)
RotateEntity entity,pitch#,yaw#,0
End Function

..its quite useful so by disabling let say pitch# /RotateEntity entity,0,yaw#,0/ you will get only Y axis rotation towards Target or do combination you want..


Rob Farley(Posted 2007) [#3]
Graphics3D 1024,768,32,2

camera = CreateCamera()
light = CreateLight()
TurnEntity light,45,45,0

MoveEntity camera,0,30,-30
TurnEntity camera,45,0,0

cone = CreateCone(6)
RotateMesh cone,90,0,0

cube = CreateCube()

pointer = CreatePivot()

PositionEntity cube,0,0,10
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2

Repeat

	mx# = MouseXSpeed() / 10.0
	my# = -MouseYSpeed() / 10.0
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	
	TranslateEntity cube,mx,0,my
	
	PointEntity pointer,cube
	TurnEntity cone,0,AngleDifference(EntityYaw(cone),EntityYaw(pointer))/20,0
	
	RenderWorld
	Flip

Until KeyHit(1)

Function AngleDifference#(angle1#,angle2#) 
	Return ((angle2 - angle1) Mod 360 + 540) Mod 360 - 180 
End Function



Stevie G(Posted 2007) [#4]
Instead of pointentity you could use ...

DY# = Deltayaw( Turret, PLayer )
Turnentity Turret , 0 , DY , 0


or ..

Dx# = entityx( Player ) - entityx( Turret )
Dz# = entityz( Player ) - entityz( Turret )
Aligntovector Turret , Dx, 0 , Dz , 3, 1


Stevie


Ben(t)(Posted 2007) [#5]
I kind of like double question marks approach it gives the turret a very timesplitters feel where you can avoid it but if it actually gets to pointing at you then your dead


Ben(t)(Posted 2007) [#6]
is there a way for the turret to face the character without having the character point at the turret?


Stevie G(Posted 2007) [#7]
As mentioned above. Remember to use the global flag for turnentity ...

DY# = Deltayaw( Turret, PLayer )
Turnentity Turret , 0 , DY * .05 , 0 , 1

Stevie


Ben(t)(Posted 2007) [#8]
I need some more help on turning!
I found that your second method works really well stevie G
but my ai units won't look up!
I tried this

dx#=EntityX(player)-EntityX(a\entity)
dy#=EntityY(player)-EntityY(a\entity)
dz#=EntityZ(player)-EntityZ(a\entity)
AlignToVector a\entity,dx,0,dz,3,.25
AlignToVector a\entity,0,dy,0,1,.25

but it still doesn't look up!


Stevie G(Posted 2007) [#9]
dx#=EntityX(player)-EntityX(a\entity, 1)
dy#=EntityY(player)-EntityY(a\entity, 1)
dz#=EntityZ(player)-EntityZ(a\entity, 1)
AlignToVector a\entity,dx,dy,dz,3,.25


The above should work, assuming a\entity is the turret parented to the enemy.

Stevie


Ben(t)(Posted 2007) [#10]
what If I want a moving enemy to do the same sort of thing would that work?


Stevie G(Posted 2007) [#11]
No reason why not, why don't you try it.


Ben(t)(Posted 2007) [#12]
I just tested it out and it works perfectly!
thank you Stevie