About EntityDistance()

Blitz3D Forums/Blitz3D Programming/About EntityDistance()

Mortiis(Posted 2009) [#1]
Is there a possible way to set an entity radius in X and Z axis' of an
object so that EntityDistance() command won't calculate the distance
taking the center of the Entity as the target?

I have a couple of AI enemies in my game and I'm updating them each loop, facing them to the player's entity and moving them towards the player. I want to check that if the enemy is near the player then it gets into the attack stance and starts the hitting animation etc.

By the way, the player object is a tank which is not shaped like a box but more like a rectangle so the enemies coming from left and right sides, they start attacking at a more distant position then the ones attacking from the back and the front.


Warner(Posted 2009) [#2]
I suppose you could use a pointdistancetoplane or pointdistancetoline routine from the code archive. Calculate the distance for each side of the square/cube.


Yasha(Posted 2009) [#3]
Off the top of my head, but it might work...

You could put a pivot at each corner of the tank, and if the distance to any one pivot is less than the desired attack distance... attack!

Obviously this would only work if the tank itself is smaller than the attack distance, and you're operating primarily in a 2D plane (easy enough to add four more and make it 3D, though).


RifRaf(Posted 2009) [#4]
You dont need to over complicate things, this would work as well
you could modify this depending on angle of ent1, and ent2 and adjust the entrad values accordingly if you wanted different range if its a "side attack".. but for general spherical radius check, it will work as is.
Global Dist_RadPiv#=CreatePivot()
Function EntityDistance_Rad#(Ent1,Ent1Rad#,Ent2,Ent2rad#)
 PositionEntity Dist_RadPiv,EntityX(ent1),EntityY(ent1),EntityZ(ent1)
 PointEntity Dist_RadPiv,Ent2
 MoveEntity Dist_RadPiv,0,0,Ent1rad+Ent2rad
 Return EntityDistance (Dist_RadPiv,Ent2)
End Function



Stevie G(Posted 2009) [#5]
tformpoint 0,0,0,Enemy, Player

if abs( tformedx() ) < Side_Attack_Dist
   ;do side attack
endif

if abs( tformedz() ) < FrontRear_Attack_Dist
   ;do front or rear attack
endif



Mortiis(Posted 2009) [#6]
Stevie, your code makes the AI get into the attack status at random distances, but I couldn't figure out why?

I create a new instance of AI type every 3 seconds, and move the each AI to the player and check if they are close enough to attack, with the code you provided.


Warner(Posted 2009) [#7]
It could be that the player's scaling is taken in account as well.


GIB3D(Posted 2009) [#8]
Function FUNC_3DDistance#(x1#,z1#,x2#,z2#)
	Return Sqr((x1-x2)^2+(z1-z2)^2)
End Function


or

Function FUNC_3DDistance#(entity1,entity2)
	Local x1# = EntityX(entity1,1)
	Local z1# = EntityZ(entity1,1)
	Local x2# = EntityX(entity2,1)
	Local z2# = EntityZ(entity2,1)
	
	Return Sqr((x1-x2)^2+(z1-z2)^2)
End Function