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
end

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"):
https://www.lexaloffle.com/bbs/?pid=43786#p43786

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:
coll_fns={
 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)
end
  • polymorphism (sort of)
a={
 ...,
 with_box=function(self,box) ... end,
 with_circle=function(self,circle) ... end
}
function are_colliding(a,b)
 return a["with_"+b.type](a,b)
end
P#43924 2017-09-04 15:37 ( Edited 2017-09-04 19:37)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-03-28 09:54 | 0.009s | 4194k | Q:11