Code archives/Algorithms/Distance between 2 entities in 3D space for collision!
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This is a very fast 'sort of' collision function! The function checks if the distance between the two entities provided is smaller or equal to the distance-parameter you provided. You can use this function as if the 2 entities have collided with each other while they actually didn't. You can also use it to determine when to execute some sort of action like "making an enemy walk towards you" when you are at a certain distance from the second entity. Hopefully this is useful to you! Bye, Steve Overmars. | |||||
; distance(entity1, entity2, distance#) ; ; Parameters: ; ; entity1 - entity1 handle ; entity2 - entity2 handle ; distance# - The distance between entity1 and entity2 ; ; Description: ; ; Returns True if the distance between entity1 and entity2 is smaller or equals the distance-parameter provided. ; Function distance(entity1,entity2,distance#) If Sqr#((EntityX#(entity1) - EntityX#(entity2))^2 + (EntityY#(entity1) - EntityY#(entity2))^2 + (EntityZ#(entity1) - EntityZ#(entity2))^2)<= distance# Return True Else Return False EndIf End Function |
Comments
| ||
Faster test: (avoid sqr())(EntityX#(entity1) - EntityX#(entity2))^2 + (EntityY#(entity1) - EntityY#(entity2))^2 + (EntityZ#(entity1) - EntityZ#(entity2))^2<= (distance#)^2 |
| ||
What is wrong with using the built-in EntityDistance() function? It is faster for one thing. On another note you can speed up both of the above snippets by not using ^2 but multiplying instead: (EntityX(entity1) - EntityX(entity2))*(EntityX(entity1) - EntityX(entity2)) etc Or even better: dx# = EntityX(entity1) - EntityX(entity2) then... dx*dx |
| ||
^2 is a slow operation. |
| ||
"Beaker" is the daddy. |
Code Archives Forum