Log In  

Hi all!

I have this collision function:

function are_colliding(entity_a,entity_b) -- are entities hitting each others boundary and not the same type?
 return entity_b.x < entity_a.x + entity_a.w and entity_a.x < entity_b.x + entity_b.w
 and entity_b.y < entity_a.y + entity_a.h and entity_a.y < entity_b.y + entity_b.h

It works almost fine but it is not pixel perfect.

I would like to use it in my games and make it as flexible as possible...
For example an issue I am facing is that in my pong game I can have a round ball or squared ball, depending on theme "classic" or "modern".
This moves the "x" and "y" coordinates of the object I take into account and ruins the collision (here's the game in case it helps problem comprehension - collision is still done there "manually"):

Do you have a suggestion on how to make it as general as possible?

Thanks for your time and patience :)

P#43790 2017-08-31 10:08 ( Edited 2017-09-04 19:37)

Suggest you add a 'type' property to your various entities.
With that you can add some nice OO behaviors:

  • function map:
 boxcircle=function(box,circle) ... end,
 boxbox=function(box,box) ... end,

function are_colliding(a,b)
 -- get resolution variant
 local coll_type=a.type+b.type
 return coll_fns[coll_type](a,b)
  • polymorphism (sort of)
 with_box=function(self,box) ... end,
 with_circle=function(self,circle) ... end
function are_colliding(a,b)
 return a["with_"+b.type](a,b)
P#43924 2017-09-04 15:37 ( Edited 2017-09-04 19:37)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-04-10 00:00 | 0.009s | 2097k | Q:14