Collision detection issue

Hello everyone,

I am facing an issue with the detection of collision.My both objects are dynamic and what I want to do is just print a statement when the collision happens.

Receiving help from you all is of great fortune to me.

Below is the code:

local rec=display.newRect(200,200,80,80) tab={-40,-40,-40,80,40,80,40,-40} rec.name="rect" physics.addBody( rec, "dynamic",{shape=tab}) rec:setLinearVelocity( 0, -330 ) local ball=display.newCircle( 200, 100, 30 ) ball.name="ball" physics.addBody( ball, "dynamic") function onCollison(self,event) if event.phase=="ended" then if event.other.name=="ball" then print(true) end end end rec.collision=onCollision rec:addEventListener( "collision", rec )

Every suggestion is welcome.

Thanks and regards,

Swanand Thakur.

Hi mate,

Got your message and I also read this post earlier, but apart from checking for syntax errors, I’m afraid I have little to offer.

Regarding syntax it looks okay but since your condition is not triggering, I’d start by inserting som print statements in the various logic places to find out which line is in fact failing.

I haven’t used physics in corona yet though so besides that i’m not sure  :slight_smile:

anaqim

I agree with @anaqim.  I would print the event table and make sure you’re getting what you expect.

Also, without seeing the rest of your code, you have your onCollision function global. If you have another module that gets loaded with a global onCollision function, they will stomp on each other. No reason to not put a local in front of the function definition to keep it scoped properly.

An easy way to print a table is to use an API from the JSON library. This means at the top you need to do:

local json = require("json")

Then as the first line inside the onCollision function do:

    print( json.prettify( event ) )

and then look in the Corona Console log and see what’s being dumped for the event table.

Rob

Hello Sir @Rob Miracle,

In the above code the only thing I have not mentioned is the physics initialization part otherwise the above written is the full code.

I have also modified my function to local and also used that json about which you said in your post. I got nothing print in the console.

Regards,

Swanand Thakur.

does it look correct in hybrid?  rec’s shape should be clockwise

Sir @davebollinger,

I didn’t understand regarding that clockwise thing.

Regards,

Swanand Thakur.

  1. You don’t need a shape.  You seem to be trying to define a rectangular shape that matches the dimensions of the rectangle.  Just use the default body.  Tip: The default body is the same size as the rectangle.

  2. Your shape is in the wrong order (unless I’m reading it wrong)

    IN-CORRECT (why 80 by the way?): – This looks counter-clockwise to me. local shape = { -40, -40, -40, 80, 40, 80, 40, -40 } – ‘CLOCKWISE (vertex) WINDING’ - look up this term on Google CORRECT: local shape = { -40, -40, – UPPER LEFT 40, -40, – UPPER RIGHT 40, 40, – LOWER RIGHT -40, 40 } – LOWER LEFT

About vertex winding:

https://cmichel.io/understanding-front-faces-winding-order-and-normals

Tip: Don’t get hung up on the numbers he has assigned the vertices.  Pay attention to the order of traversal.  The order you traverse the vertices (or in the case of collision shapes order you define them) determines the winding (clockwise or counter-clockwise).

Finally I would reduce your code to this:

local rect = display.newRect( 200, 200, 80, 80) rect.name = "rect" physics.addBody( rect, "dynamic" ) rect:setLinearVelocity( 0, -330 ) function rect.collision( self, event ) if( event.phase == "ended" ) then if( event.other.name == "ball" ) then print("YO! HIT A BALL!") end end end rect:addEventListener( "collision" ) local ball = display.newCircle( 200, 100, 30 ) ball.name = "ball" physics.addBody( ball, "dynamic", { radius = 30 } )

Hello Sir @roaminggamer,

Purposely was trying to have collision boundaries more than that of an object body, but from above discussion it seems this can’t be done right??

Thanks for that vertices part.

Thanks and Regards,

Swanand Thakur.

You can make the body any size you like.

I simply wondered what you wanted to do and though perhaps you’d made a math error.

Note: You did have your winding in the wrong order.  So, whatever you choose to do with regards to custom physics bodies, be sure to follow the rules as per the docs.

https://docs.coronalabs.com/api/library/physics/addBody.html

https://docs.coronalabs.com/api/library/physics/addBody.html#bodytype-optional

https://docs.coronalabs.com/api/type/Body/bodyType.html

https://docs.coronalabs.com/guide/index.html#physics

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html

AND BE SURE TO READ THIS (pay attention to IMPORTANT notes):

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html#polygonal-bodies

Custom body vertices must be specified in clock-wise order and the body must be convex.  If you do not know what these terms mean, be sure to read the docs and research online. 

Tip: Non-convex bodies may seem to work, but will fail in certain cases.

Hello Sir @roaminggamer,

What I wanted to ask is if I have a rectangle

local rect = display.newRect( 200, 200, 80, 80)

then can I define the body collision outlines as

local shape = {

-40,-40,    ------upper left

40,-40,     ------upper right

40,80,      ------bottom right

-40,80      ------bottom left

}

Would the things work?? Would the collision be detected?

Regards,

Swanand Thakur. 

Yes, and you can observe the body by enabling debug draw mode:

https://docs.coronalabs.com/api/library/physics/setDrawMode.html

Thank you so much to all of you who have given their inputs in this above discussion.It has really helped heavily.

The problem is finally solved in all means.

Special thanks to Sir @roaminggamer, for his invaluable time and sharing precious knowledge. The above discussion was really fruitful, it taught me many useful and helpful concepts, which will definitely help me in prolong journey.

Thanks and Regards,

Swanand Thakur.

Hi mate,

Got your message and I also read this post earlier, but apart from checking for syntax errors, I’m afraid I have little to offer.

Regarding syntax it looks okay but since your condition is not triggering, I’d start by inserting som print statements in the various logic places to find out which line is in fact failing.

I haven’t used physics in corona yet though so besides that i’m not sure  :slight_smile:

anaqim

I agree with @anaqim.  I would print the event table and make sure you’re getting what you expect.

Also, without seeing the rest of your code, you have your onCollision function global. If you have another module that gets loaded with a global onCollision function, they will stomp on each other. No reason to not put a local in front of the function definition to keep it scoped properly.

An easy way to print a table is to use an API from the JSON library. This means at the top you need to do:

local json = require("json")

Then as the first line inside the onCollision function do:

    print( json.prettify( event ) )

and then look in the Corona Console log and see what’s being dumped for the event table.

Rob

Hello Sir @Rob Miracle,

In the above code the only thing I have not mentioned is the physics initialization part otherwise the above written is the full code.

I have also modified my function to local and also used that json about which you said in your post. I got nothing print in the console.

Regards,

Swanand Thakur.

does it look correct in hybrid?  rec’s shape should be clockwise

Sir @davebollinger,

I didn’t understand regarding that clockwise thing.

Regards,

Swanand Thakur.

  1. You don’t need a shape.  You seem to be trying to define a rectangular shape that matches the dimensions of the rectangle.  Just use the default body.  Tip: The default body is the same size as the rectangle.

  2. Your shape is in the wrong order (unless I’m reading it wrong)

    IN-CORRECT (why 80 by the way?): – This looks counter-clockwise to me. local shape = { -40, -40, -40, 80, 40, 80, 40, -40 } – ‘CLOCKWISE (vertex) WINDING’ - look up this term on Google CORRECT: local shape = { -40, -40, – UPPER LEFT 40, -40, – UPPER RIGHT 40, 40, – LOWER RIGHT -40, 40 } – LOWER LEFT