Entity near? how?
Blitz3D Forums/Blitz3D Beginners Area/Entity near? how?
| ||
I need to say that if there is ANY entity near to another then do something.. There is some command do this? It' s for an ai, i want that ai check every time if there are other ai or building or player near it.. How can i do it? Aza |
| ||
depends how you define "near". There is the entitydistance command, so, this is possible:Function EntityNear(Entity1, Entity2, Distance#= 2) ;"Distance#=2" mean that the default distance to check ; for is 2, so if you wanted 2 to be your default, and be ; able to change it, do this If EntityDistance(Entity1, Entity2) > Distance# then ;If your entity distances are further than your designated "near" variable, then do nothing EndIf If EntityDistance(Entity1, Entity2) =< Distance# then Return True EndIf End Function This function would be used in code like: If EntityNear(Zombie, Building) = True Then ;such and such Endif ;OR If EntityNear(Zombie, Building, 10) = True Then ;If the zombie is 10 units or less from the building then such and such Endif FURTHERMORE If you need the distance in just X and Z (Y messed me up for a few things, say, a building's pivot is halfway up it's height, the entitydistance will be thrown off), use the 2d distance formular like so: Function distxz(entity1, entity2) distx# = EntityX(entity1) - EntityX(entity2) distz# = EntityZ(entity1) - EntityZ(entity2) distxz# = Sqr(distx^2 + distz^2) Return distxz# End Function Hope I helped you! |
| ||
Thanks, i know this command but i search something a little different, i need to use this function for many and many entity, if every time i'll check entity by entity i think the program will go slow.. What do you think about it? Aza |
| ||
EntityDistance is plenty fast but this function is ineffecient and pointless ...Function EntityNear(Entity1, Entity2, Distance#= 2) ;"Distance#=2" mean that the default distance to check ; for is 2, so if you wanted 2 to be your default, and be ; able to change it, do this If EntityDistance(Entity1, Entity2) > Distance# then ;If your entity distances are further than your designated "near" variable, then do nothing EndIf If EntityDistance(Entity1, Entity2) =< Distance# then Return True EndIf End Function Just use .... if entitydistance( entity1, entity2 ) < Range ;do something endif If you have issues due to the center of the entity being higher on the y axis than you like .. parent a pivot to it at it's base and do distance checks against that. How many entities are you checking against each other? Stevie |
| ||
I don't think you need to know the actual distance away entities are because you can simply do a comparision against a squared distance range, obviating the need to use Sqr each time (which EntityDistance uses internally, too). |
| ||
I agree with big10p (even if he uses words like "obviating" and is a prize monkey). Although EntityDistance will be plenty fast. |
| ||
lol. Charming! :D |
| ||
I would also agree with big10p. If you're worried about the speed of all these comparisons, compare the squared totals. They're perfectly comparable as they are. |
| ||
You could use the tformpoint command, that way you can get distance, wether they are in front or behind the entity and at which side they are of the entity. You can also make sure the entity isn't too far away on the y axis. |
| ||
I am sorry, what you mean for squared? |
| ||
Just use entitydistance if your not sure what squared numbers are. CPU should be able to do 10,000's of these every loop with very little slowdown. But, then again, 10,000's of objects on screen, wouldn't be a great idea :oP |