Help newb understand code

Hello, just starting to study game development and I came across this piece of code. I’ve been looking through the docs and trying to completely understand what’s going on, but having a bit of a hard time grasping it fully. I know this piece of code detects whether or not two objects have collided, but if someone can explain it to me in plain English that would be awesome.

[code]
function HitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)

end
[/code] [import]uid: 132483 topic_id: 34246 reply_id: 334246[/import]

Ok, i think i got it. Right,left,up,down are booleans, and the function returns a boolean. In order to return true (a positive hit) both parts of the return statement have to be true, right? But doesn’t that mean that the obj has to be hit on more than one side? [import]uid: 132483 topic_id: 34246 reply_id: 136171[/import]

It has hit on at least one side. [import]uid: 199310 topic_id: 34246 reply_id: 136176[/import]

Ah, I see. The AND operator returns true if either statement is true. My next question is is there a better way to test object collision? Can’t this be done with the Physics library? [import]uid: 132483 topic_id: 34246 reply_id: 136208[/import]

Yepp, use physics - it is more accurate then inventing the wheel again :wink:

J [import]uid: 81188 topic_id: 34246 reply_id: 136216[/import]

I disagree. There are many things where you need to know if two objects are overlapping that you don’t need gravity, impulses, force, drag and bounce calculations getting in your way. If you are not using physics for physics and only to detect if two things hit each other, there are much more efficient way’s to manage the detection.

This bounding box method is one of of simplest methods out there. You could modify that function to return in addition to true/false which side of the box got the collision.
[import]uid: 199310 topic_id: 34246 reply_id: 136235[/import]

@Rob: You are right but it depends on how fast our scripts are get to run/compile comparing to native physic engine inside Corona. I understand your point that if you do not use most features of physic engine, do not add it just for sake of finding collision points but in Unity’s case it was not true and PhysX implementation was so fast that using it in such cases was more optimal than finding collision points via script level code since PhysX is coded with C++ at native core of Unity and runs natively. [import]uid: 206803 topic_id: 34246 reply_id: 136265[/import]

Ok, i think i got it. Right,left,up,down are booleans, and the function returns a boolean. In order to return true (a positive hit) both parts of the return statement have to be true, right? But doesn’t that mean that the obj has to be hit on more than one side? [import]uid: 132483 topic_id: 34246 reply_id: 136171[/import]

It has hit on at least one side. [import]uid: 199310 topic_id: 34246 reply_id: 136176[/import]

Ah, I see. The AND operator returns true if either statement is true. My next question is is there a better way to test object collision? Can’t this be done with the Physics library? [import]uid: 132483 topic_id: 34246 reply_id: 136208[/import]

Yepp, use physics - it is more accurate then inventing the wheel again :wink:

J [import]uid: 81188 topic_id: 34246 reply_id: 136216[/import]

I disagree. There are many things where you need to know if two objects are overlapping that you don’t need gravity, impulses, force, drag and bounce calculations getting in your way. If you are not using physics for physics and only to detect if two things hit each other, there are much more efficient way’s to manage the detection.

This bounding box method is one of of simplest methods out there. You could modify that function to return in addition to true/false which side of the box got the collision.
[import]uid: 199310 topic_id: 34246 reply_id: 136235[/import]

@Rob: You are right but it depends on how fast our scripts are get to run/compile comparing to native physic engine inside Corona. I understand your point that if you do not use most features of physic engine, do not add it just for sake of finding collision points but in Unity’s case it was not true and PhysX implementation was so fast that using it in such cases was more optimal than finding collision points via script level code since PhysX is coded with C++ at native core of Unity and runs natively. [import]uid: 206803 topic_id: 34246 reply_id: 136265[/import]