Question on this Tutorial: Non-Physics Collision Detection

Hi Rob:

I found your tutorial about collision. And I see many functions but I don’t know how to use them.

I tried something about the isSensor = true, about physics or non-physics I have read many post and I still don’t really get it to work

Do you have a sample code as easy as possible to copy and past in a STORYBOARD, because most of the code I found are in one single file, so when I try to have this on storyboard, the runtime keeps working, the physics will not stop, some of the objects will not move after the second time, the physics works only the first time, and so on, I have several post on this an no solution yet

Thank for everything Rob, as always I really appreciate your time.

As an exampl, I see this

--rectangle-based collision detection local function hasCollided( obj1, obj2 ) &nbsp;&nbsp; if ( obj1 == nil ) then&nbsp; --make sure the first object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp; end &nbsp;&nbsp; if ( obj2 == nil ) then&nbsp; --make sure the other object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp; end &nbsp;&nbsp; local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin &nbsp;&nbsp; local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax &nbsp;&nbsp; local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin &nbsp;&nbsp; local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax &nbsp;&nbsp; return (left or right) and (up or down) end

But I don’t have any idea waht to do with it or how to make ti work in storyboard at all

Thanks Rob

The function above simply checks to see if two rectangles are overlapping.  Nothing more, nothing less.  It returns true if they overlap, false if it does not.

With physics, the system moves things for you and it lets you know when things collide.  The non-physics version assumes that you are moving items yourself.  If you’re dragging with a touch handler, then you can call the has_collided() function in your touch handler.  If you’re moving things with transitions or some other non-physics means, then you can use a Runtime “enterFrame” handler that will check your objects every frame to see if they have collided.

Typically you will have a table of objects that you’re interested in checking and you use a for loop to loop over the list of objects and see if your main object is touching one of the objects in the table.

Thanks Rob…

I finally got it to work.

One little note…

Let me tell you how much I admire you, and all the developers that really knows

a lot of programming, it’s not easy, it takes a lot of time, and intelligence, and

creativity, and I wish I knew more…

But I am learning, little by little

Thanks for everything

Thanks.  I do have to admit, learning to program came easy for me.  But also keep in mind that I’ve been doing this for over 30 years.  With any thing, the more you do it, the better you get at it.  The more you learn from your mistakes and recognize patterns that will help you avoid those mistakes in the first place. 

You’re doing quite well considering you started at zero.

Hi Rob…

Still working with this collision thing…

I have this code

local function onLocalCollision( self, event ) &nbsp;&nbsp; &nbsp;if ( event.phase == "began" ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("start") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(btnHome, {time = 200, x=60, y=100, yScale=1}) &nbsp;&nbsp; &nbsp;elseif ( event.phase == "ended" ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("finish") &nbsp;&nbsp; &nbsp;end end myCircle.collision = onLocalCollision myCircle:addEventListener( "collision", myCircle )

And it works one time only…

A circle(ball) falls down

hits the rect, a “floor” at the bottom

and moves the button

because the circle has bounce=2, when bounces and touch the “floor” again, the button will not move

I thought i – local function onLocalCollision( self, event )

was a function and you call it everytime it has a collision…

and the other problem, is that I have a redCircle, that is moving on the screen

So I want to detect when the circle hits the redCircle,

not when the circle hits the floor

How do I do that?

Thanks

The function above simply checks to see if two rectangles are overlapping.  Nothing more, nothing less.  It returns true if they overlap, false if it does not.

With physics, the system moves things for you and it lets you know when things collide.  The non-physics version assumes that you are moving items yourself.  If you’re dragging with a touch handler, then you can call the has_collided() function in your touch handler.  If you’re moving things with transitions or some other non-physics means, then you can use a Runtime “enterFrame” handler that will check your objects every frame to see if they have collided.

Typically you will have a table of objects that you’re interested in checking and you use a for loop to loop over the list of objects and see if your main object is touching one of the objects in the table.

Thanks Rob…

I finally got it to work.

One little note…

Let me tell you how much I admire you, and all the developers that really knows

a lot of programming, it’s not easy, it takes a lot of time, and intelligence, and

creativity, and I wish I knew more…

But I am learning, little by little

Thanks for everything

Thanks.  I do have to admit, learning to program came easy for me.  But also keep in mind that I’ve been doing this for over 30 years.  With any thing, the more you do it, the better you get at it.  The more you learn from your mistakes and recognize patterns that will help you avoid those mistakes in the first place. 

You’re doing quite well considering you started at zero.

Hi Rob…

Still working with this collision thing…

I have this code

local function onLocalCollision( self, event ) &nbsp;&nbsp; &nbsp;if ( event.phase == "began" ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("start") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(btnHome, {time = 200, x=60, y=100, yScale=1}) &nbsp;&nbsp; &nbsp;elseif ( event.phase == "ended" ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("finish") &nbsp;&nbsp; &nbsp;end end myCircle.collision = onLocalCollision myCircle:addEventListener( "collision", myCircle )

And it works one time only…

A circle(ball) falls down

hits the rect, a “floor” at the bottom

and moves the button

because the circle has bounce=2, when bounces and touch the “floor” again, the button will not move

I thought i – local function onLocalCollision( self, event )

was a function and you call it everytime it has a collision…

and the other problem, is that I have a redCircle, that is moving on the screen

So I want to detect when the circle hits the redCircle,

not when the circle hits the floor

How do I do that?

Thanks