Before I begin, I will let you know I have read extensively box2d manual, I utilize every API in Corona for physics and I have looked at every post regarding this issue, including “resolutions to most common physics issues”
If you are lazy, I created video at bottom, and also what I need
I’ve read the following
general information about bodies
http://developer.anscamobile.com/content/game-edition-physics-bodies#physics.addBody
more detailed information
http://developer.anscamobile.com/content/game-edition-box2d-physics-engine
note for box2d users
corona sdk collision detection
http://developer.anscamobile.com/content/game-edition-collision-detection
The problem I face is I think A common one, but seems to be one of those issues I have not seen one person solve completely, or at least to a point of where its acceptable.
HEre is the scenario, I’m not providing code at this point, as the issue is pretty easy to describe.
The problem is this:
Lets say I have a long rectangle, which is “static”.
Now, I spawn a ball on top and let it fall onto the object. The ball will bounce off the rectangle and will not even get anywhere close to going partially through the object.
Now, if I have that same rectangle in place and it’s welded to a couple of anchor points and the rectangle is “dynamic” and the rectangle is NOT moving AND I let the ball fall onto the rectangle the ball will partially penetrate the rectangle.
What gets me is how Box2d treats a dynamic object that isn’t moving… it’s just ODD.
That’s scenario 1, now we have scenario 2:
Same rectangle, but now it’s a draggable. I could use it to drag the rectangle around to bounce the ball around have fun etc.
The ball would sometimes penetrate the rectangle. Sometimes the ball would just go through it, and it was like the rectangle wasn’t even considered in the first place. It’s like sometimes the box2d engine forgets what it’s doing and just allows the ball to pass through.
So I said ok, I’ll go to battle on this thing! Here is what I tried
physics.setPositionIterations(128)
physics.setVelocityIterations(128)
physics.setDrawMode( “normal”)
physics.setScale(60 )
body.angularDamping
body.linearDamping
body.angularVelocity
body:setLinearVelocity
body.isBullet = true
body.isBodyActive
body.isAwake
body.isSleepingAllowed
body:getLinearVelocity( )
body:resetMassData( )
I’ve played with those numbers, and on iterations I’ve done 0 through 5000! It seems above 128 to 200 the effectiveness fades with a sort of heavy performance hit.
The scale, I’ve been up and down from 0 to a 1000, it doesn’t seem to matter. I’ve tried combinations of things. This has been going on for at least 2 months.
So then, iNSERTCODE on twitter reached out to me and pointed me to this:
http://www.gamedev.net/blog/1350/entry-2253939-intelligent-2d-collision-and-pixel-perfect-precision/
This is a pretty interesting read. The problem I have is this is I think for cocos2d or something, which me being a coding noob, I can’t decipher it. I get the logic, but the code usually tells more than what’s being said. I was wondering, anyone want to take a stab at this and see if this would be applicable for Corona SDK and translate it to Corona SDK.
I think this would be helpful to everyone who has this problem, right now It’s the #1 thing preventing me from releasing. Character going through walls and off screen is a big big bug, which I can’t release it that way. So basically, I’m spinning my wheels trying to figure this out.
If someone really wants sample code, I could whip something up. But since this is more of an advanced topic…ok I changed my mind lol. Here is some sample code
local physics = require "physics"
--physics.setGravity(0, 80) -- set this down at the bottom!!
physics.setPositionIterations(128)
physics.setVelocityIterations(128)
physics.setDrawMode( "hybrid")
physics.setScale(30)
physics.start(true)
--shorcut variables to call the content width and height
local \_W = display.contentWidth
local \_H = display.contentHeight
local ball = display.newCircle (\_W/2 -30, \_H/2-300, 11)
physics.addBody( ball, { density=.01, friction=.50, bounce=0.01, radius=11} )
ball.linearDamping = 0
ball.isBullet = true
ball:setFillColor (30,144,255)
ball.alpha = 1
local Lrect = display.newRect (3,20, 20, \_H -20)
physics.addBody( Lrect, "static")
Lrect.alpha = 0
local Rrect = display.newRect (747,20, 20, \_H-18)
physics.addBody( Rrect, "static")
Rrect.alpha = 0
local Trect = display.newRect (\_W/2 -385,0, \_W+10, 20)
physics.addBody( Trect, "static")
Trect.alpha = 0
local Brect = display.newRect (\_W/2 -400,1005, \_W, 20)
physics.addBody( Brect, "static")
Brect.alpha = 0
local platform = display.newRect (\_W/2,\_H/2,200,20)
physics.addBody( platform, "static")
platform.isFixedRotation = true
platform.alpha = 1
local ground = display.newRect (0, \_H/2+200,800,20)
ground.isFixedRotation = true
physics.addBody(ground, "static")
local function dragBody( event )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
if "began" == phase then
stage:setFocus( body, event.id )
body.isFocus = true
--Change body to dynamic while we are moving the maze around
event.target.bodyType = "dynamic"
-- Create a temporary touch joint and store it in the object for later reference
body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )
elseif body.isFocus then
if "moved" == phase then
-- Update the joint to track the touch
body.tempJoint:setTarget( event.x, event.y )
elseif "ended" == phase or "cancelled" == phase then
stage:setFocus( body, nil )
body.isFocus = false
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
--After the event ends, change body back to static.
--event.target.bodyType = "static"
-- Remove the joint when the touch ends
body.tempJoint:removeSelf()
end
end
-- Stop further propagation of touch event
return true
end
platform:addEventListener( "touch", dragBody )
Oh and if that is too much for you, here I created a video of it as well
http://www.youtube.com/watch?v=KwmocWSUojQ&feature=youtube_gdata_player
In the video , I make a change on the iterations and go to 512, almost instantly I get the same issue. Lol
[import]uid: 61600 topic_id: 19839 reply_id: 319839[/import]