Physics body not working as I expected/needed

Hi there everyone, I have a big issue with testing simple collision on corona Physics engine. I used the standard circle body options and set the physics draw to hybrid to see the body.

so heres what ive got.

http://picpaste.com/weirdBody-eXdBx5wL.png

above pic, the collision of the smiley is sort of working, because the outline of the circle is colliding with the outline of the rect, i mean it should work like this yeah ?

so… below is a different story.

http://picpaste.com/weirdBody2-7GjZIcqA.png

this time, if i move the smiley to the Right side of the rect, the collision is not working up until the (xy line or the greenred line, i dont really know what it is) because it seems like it is controlling the collision. Where the middle extends out to the green and red line, the collision is happening. Its like the collision is only at a half-a-semi-circle at the bottom right.

Is this natural for physics engine corona? how to solve this. what i need is a full fledged collision according to the Physics body shape. Not according to the greenred line area. Or how do i go about making this work?

Help :frowning: been testing for days. My main.lua below

local physics = require "physics" physics.start() physics.setGravity(0,0) local StickLib = require("lib\_analog\_stick") local screenW = display.contentWidth local screenH = display.contentHeight physics.setDrawMode( "hybrid" ) -- SETUP GRAPHICS local heroCircInfo = require("HeroCircle") local heroCircSheet = graphics.newImageSheet("HeroCircle.png", heroCircInfo:getSheet()) local background = display.newImage("background.jpg") background:scale(2,2) background.x = 600 background.y = 400 local happy = display.newImage("happy.png") happy.x = 500 happy.y = 400 happy:scale(2,2) local heroCircInfo = require("HeroCircle") local heroCircSheet = graphics.newImageSheet("HeroCircle.png", heroCircInfo:getSheet()) local angry = display.newImage(heroCircSheet, 1) angry.x = 880 angry.y = 560 angry:scale(2,2) angry = angry local angry2 = display.newImage(heroCircSheet, 1) angry2.x = 600 angry2.y = 520 angry2 = angry2 ----- create the analog joystick MyStick = StickLib.NewStick( { x = screenW\*.1, y = screenH\*.85, thumbSize = 16, borderSize = 32, snapBackSpeed = 0.2, R = 0, G = 255, B = 255 } ) ---------------------- happy.physicsOptions = { density = 1, radius = 50, isSensor = false } angry.physicsOptions = { density = 30, friction = 1 } angry2.physicsOptions = { density = 30, friction = 1 } physics.addBody(happy, "dynamic",happy.physicsOptions) physics.addBody(angry, "static",angry.physicsOptions) physics.addBody(angry2, "static",angry2.physicsOptions) local function main( event ) -- MOVE THE Character (obj,maxSpeed,rotate) MyStick:move(happy, 7.0, false) end Runtime:addEventListener("enterFrame", main) function onCollision(event) print("collide!") end Runtime:addEventListener("collision", onCollision) -------------------------------------------------------------------------------- -- Build Camera -------------------------------------------------------------------------------- --- these below is just a camera follow wont affect physics local require = require local perspective = require("perspective") local camera = perspective.createView() camera:add(happy, 1) camera:add(angry, 2) camera:add(angry2, 2) camera:add(background, 3) MyStick:toFront() camera:setParallax(1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3) camera.damping = 10 -- A bit more fluid tracking camera:setFocus(happy) -- Set the focus to the player camera:track() -- Begin auto-tracking

Dynamic physics objects can’t be moved manually unless you use a touch joint or temporary change them to static objects. The collision engine has no idea that the object has been moved if you just change the x,y value. (I’m assuming that’s what MyStick:move is doing.)

Hi Tom, it seems that even without the movement, just simply loading the image and add physics body, I do not have the same outcome as the this below. This one below has no greenred line inside the physics body shape.

http://picpaste.com/correctPhysics-DVvlrsor.png

(Taken from https://www.youtube.com/watch?v=No8eZrfroy4&index=6&list=PLtNErhYMkHnHuIZ885KpdZTkAsiBEiJaR     At 9:38 onwards)

mine below here has greenred line inside the body. which somehow seems to cause it to overlap inside the other body and only collide when it hits the middle point of that greenredline.

http://picpaste.com/weirdBody2-7GjZIcqA.png

Is the greenredline something special ? or is there a way to get rid of it.

Hi @zaviermars,

Here’s what I can elaborate on, judging from your code and screenshots.

#1) It appears that perhaps your circle object has gone “asleep”. To clarify what Tom says, you can technically move any physics object by simply setting/changing its x/y position – and that object will still register collisions as you move it around – but the risk is that by simply moving it that way, you risk the physics engine putting it to sleep, and thus it will never sense collisions. This is because, if you move it by just setting x/y, you haven’t done any actual physical actions upon it, so it remains asleep (it has no idea that you want to manipulate it physically, so Box2D won’t wake it up from sleep). Now, the easiest solution to this is just prevent it from sleeping entirely (object.isSleepingAllowed) OR wake it up by using object.isAwake when you start moving it around.

#2) However, I think the real problem is not #1 above, but rather that you’re trying to scale physical objects independently of the overall world. You cannot scale bodies individually, but rather, you can only scale the entire “world” in the same time manner. This is outlined here.

Take care,

Brent

Thanks for the explanation Brent, I think I will try to maintain properly scaling/moving of the groups and test it out. Ultimately thank you for the help!

Hi Brent, I managed to solve the collision issue. May I ask one last question?

Regarding the dynamic content scaling for the resolutions, as per normal I have the normal png and @2x png for my imageSheets. So I am building my game on the screen of iphone 5 as my “base” content area. If i have my physics body traced using Physics Editor https://www.codeandweb.com/physicseditor ,  on my normal size png, example 150x150, it will definitely be placed nicely on my iphone 5 screen simulator. But what if i switch simulator to a device that uses @2x png, will the physics body be able to enlarge itself dynamically with the dynamic content scaling Feature of corona ? This seems tricky to me in terms of which image should i use to create my physical body shape.

Really appreciate your help, on the other hand, Corona is really a very nice and easy to learn with lua language.

Hi @zaviermars,

Physics should work fine with content scaling, because as you suspect, the physics engine scales dynamically with the content area. The only “warning” I should mention is that you shouldn’t be using a content area scale setting like “zoomStretch” (well, I tell everybody to avoid that mode, since it’s not ideal for anything, and we only basically include it for legacy purposes).

Take care,

Brent

Dynamic physics objects can’t be moved manually unless you use a touch joint or temporary change them to static objects. The collision engine has no idea that the object has been moved if you just change the x,y value. (I’m assuming that’s what MyStick:move is doing.)

Hi Tom, it seems that even without the movement, just simply loading the image and add physics body, I do not have the same outcome as the this below. This one below has no greenred line inside the physics body shape.

http://picpaste.com/correctPhysics-DVvlrsor.png

(Taken from https://www.youtube.com/watch?v=No8eZrfroy4&index=6&list=PLtNErhYMkHnHuIZ885KpdZTkAsiBEiJaR     At 9:38 onwards)

mine below here has greenred line inside the body. which somehow seems to cause it to overlap inside the other body and only collide when it hits the middle point of that greenredline.

http://picpaste.com/weirdBody2-7GjZIcqA.png

Is the greenredline something special ? or is there a way to get rid of it.

Hi @zaviermars,

Here’s what I can elaborate on, judging from your code and screenshots.

#1) It appears that perhaps your circle object has gone “asleep”. To clarify what Tom says, you can technically move any physics object by simply setting/changing its x/y position – and that object will still register collisions as you move it around – but the risk is that by simply moving it that way, you risk the physics engine putting it to sleep, and thus it will never sense collisions. This is because, if you move it by just setting x/y, you haven’t done any actual physical actions upon it, so it remains asleep (it has no idea that you want to manipulate it physically, so Box2D won’t wake it up from sleep). Now, the easiest solution to this is just prevent it from sleeping entirely (object.isSleepingAllowed) OR wake it up by using object.isAwake when you start moving it around.

#2) However, I think the real problem is not #1 above, but rather that you’re trying to scale physical objects independently of the overall world. You cannot scale bodies individually, but rather, you can only scale the entire “world” in the same time manner. This is outlined here.

Take care,

Brent

Thanks for the explanation Brent, I think I will try to maintain properly scaling/moving of the groups and test it out. Ultimately thank you for the help!

Hi Brent, I managed to solve the collision issue. May I ask one last question?

Regarding the dynamic content scaling for the resolutions, as per normal I have the normal png and @2x png for my imageSheets. So I am building my game on the screen of iphone 5 as my “base” content area. If i have my physics body traced using Physics Editor https://www.codeandweb.com/physicseditor ,  on my normal size png, example 150x150, it will definitely be placed nicely on my iphone 5 screen simulator. But what if i switch simulator to a device that uses @2x png, will the physics body be able to enlarge itself dynamically with the dynamic content scaling Feature of corona ? This seems tricky to me in terms of which image should i use to create my physical body shape.

Really appreciate your help, on the other hand, Corona is really a very nice and easy to learn with lua language.

Hi @zaviermars,

Physics should work fine with content scaling, because as you suspect, the physics engine scales dynamically with the content area. The only “warning” I should mention is that you shouldn’t be using a content area scale setting like “zoomStretch” (well, I tell everybody to avoid that mode, since it’s not ideal for anything, and we only basically include it for legacy purposes).

Take care,

Brent