Question..

Blitz3D Forums/Blitz3D Programming/Question..

Mr. Slat(Posted 2010) [#1]
I wanted for my character to interact only with objects in front of him. Right now I've a function that calculates the distance between the character and the object, but I wanted the object that it's in front of him, not the one that's closer


Ross C(Posted 2010) [#2]
Your best looking into the tform point command, or calculating the angle of the entity in question, and comparing it against the angle the entity is facing. That should give you an idea.

Although, Tformvector might be your best bet...


Mr. Slat(Posted 2010) [#3]
Kinda hard when you don't know the math involved.... :P
kinda hoping for some pointers plzz :P


RGF(Posted 2010) [#4]
About the maths, this is an easy approach to the problem:

Function GetAngle(x1#,z1#,x2#,z2#)
angle# = ATan2(x#2-x#1,z#2-z#1)
Return angle#
End Function


Greetings


RGF(Posted 2010) [#5]
Sorry, should be completed with the distance:

Function GetDistance(x1#,z1#,x2#,z2#)
distance# = Sqr( (x1#-x2#)*(x1#-x2#) + (z1#-z2#)*(z1#-z2#) )
Return distance#
End Function


Stevie G(Posted 2010) [#6]
It'd be quicker using deltayaw.

The "For each Entity" is obviously pseudocode as I do not know how you store your entities but you get the idea. The Source would be the player entity.

function GetEntityClosestInFront( Source )

  local Found = 0
  local BestDY = 90  ;anything above 90deg is behind the player

  For each Entity
    Dy# = abs( Deltayaw( Source, Entity ) )
    if Dy < BestDY
      BestDY = Dy
      Found = Entity
    endif
  next

  return Found

end function  



Kryzon(Posted 2010) [#7]

Sorry, should be completed with the distance:

Function GetDistance(x1#,z1#,x2#,z2#)
distance# = Sqr( (x1#-x2#)*(x1#-x2#) + (z1#-z2#)*(z1#-z2#) )
Return distance#
End Function


Is that faster than using EntityDistance?

From a quick test I did, it's not.


RGF(Posted 2010) [#8]
Slower than EntityDistance. And yes, DeltaYaw will give you a direct and simplified method of getting the angle between two entities.

Just answering you about the BASIC MATHS involved in calculations. ATan2 is a useful, but unknown command, I thought it would be interesting to use it for example. The idea is the same, but explaining maths involved in DeltaYaw.

Greetings!!!!!!!!!