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 )
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
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.
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.
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
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).
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.
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.
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.
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
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.
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.
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