It looks like you're new here. If you want to get involved, click one of these buttons!
-- Get distance between x points self.dx = self:getX() - self.scene.hero:getX(); -- get distance between y points self.dy = self:getY() - self.scene.hero:getY(); self.distance = math.sqrt((self.dx*self.dx)+(self.dy*self.dy)) |
Comments
so if you want to check if an object is within say 25 pixels instead of checking if self.distance <= 25, just set self.distance = ((self.dx * self.dx) + (self.dy * self.dy)) and then check if self.distance <= (25*25).
If you've got a lot of checks to do the savings will start to mount up.
Also - there used to be a quick hack in the old 8/16bit days where you could "guestimate" distance to +/-10% by calculating dx and dy as above and then simply using the max of dx or dy plus half the min of dx or dy, using a single bit shift right the "half" practically came for free.
Likes: talis, plamen
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
That works perfectly. Thank you.
For anyone interested, here's my example:
(self.maxLength is the max length in this example)
Likes: techdojo, talis