require"box2d"local scale = b2.getScale()local world = b2.World.new(0, 10, true)local body = world:createBody({type= b2.DYNAMIC_BODY, position ={x =160, y =0}})local shape = b2.CircleShape.new(0, 0, 30)
body:createFixture({shape = shape, density =1})localfunction onEnterFrame()
world:step(1/60, 8, 3)end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)localfunction applyLinearImpulse()local x, y = body:getPosition()
body:applyLinearImpulse(0, -20, x/scale, y/scale)end
stage:addEventListener(Event.MOUSE_DOWN, applyLinearImpulse)local debugDraw = b2.DebugDraw.new()
world:setDebugDraw(debugDraw)
stage:addChild(debugDraw)
In my tests, it works on PC, iOS and Android.
Note: We realized that applyLinearImpulse and applyForce functions don't consider physics scale. It's a bug and now fixed. To overcome this issue, I get the physics scale and apply it to applyLinearImpulse.
hi, I tried your code, and It works on both pc and phone device. finally, I got the answer. here is the code example reproduce the issue the code work well on pc but not on my nexus s.
require "box2d"
local scale = b2.getScale()
local world = b2.World.new(0, 10, true)
--add ground local ground = world:createBody({})
ground.name = "ground"
local shape = b2.EdgeShape.new(0, application:getLogicalHeight(), application:getLogicalWidth(), application:getLogicalHeight()) ground:createFixture({shape = shape, density = 0})
local body = world:createBody({type = b2.DYNAMIC_BODY, fixedRotation = true, position = {x = 160, y = application:getLogicalHeight()}})
--[[ local shape = b2.CircleShape.new(0, 0, 30) body:createFixture({shape = shape, density = 1})
local shape = b2.PolygonShape.new() shape:setAsBox(20, 20, 0, 0, 0) body:createFixture{shape = shape, density = 1}
shape = b2.PolygonShape.new() shape:setAsBox(20, 10, 0, 20, 0) ------notice here, the fourth param should less than 20, or it will be hold by the ground body:createFixture{shape = shape, density = 1}
local function onEnterFrame() world:step(1/60, 8, 3) end
You're right. For this case box2d at desktop and mobile behaves differently. Currently I cannot predict the main reason. It's an interesting case and I'll investigate more.
Comments
Can you try this small example:
Note: We realized that applyLinearImpulse and applyForce functions don't consider physics scale. It's a bug and now fixed. To overcome this issue, I get the physics scale and apply it to applyLinearImpulse.
I tried your code, and It works on both pc and phone device.
finally, I got the answer.
here is the code example reproduce the issue
the code work well on pc but not on my nexus s.
require "box2d"
local scale = b2.getScale()
local world = b2.World.new(0, 10, true)
--add ground
local ground = world:createBody({})
ground.name = "ground"
local shape = b2.EdgeShape.new(0, application:getLogicalHeight(), application:getLogicalWidth(), application:getLogicalHeight())
ground:createFixture({shape = shape, density = 0})
local body = world:createBody({type = b2.DYNAMIC_BODY, fixedRotation = true, position = {x = 160, y = application:getLogicalHeight()}})
--[[
local shape = b2.CircleShape.new(0, 0, 30)
body:createFixture({shape = shape, density = 1})
shape = b2.CircleShape.new(30, 0, 60)
body:createFixture({shape = shape, density = 1})
]]
local shape = b2.PolygonShape.new()
shape:setAsBox(20, 20, 0, 0, 0)
body:createFixture{shape = shape, density = 1}
shape = b2.PolygonShape.new()
shape:setAsBox(20, 10, 0, 20, 0) ------notice here, the fourth param should less than 20, or it will be hold by the ground
body:createFixture{shape = shape, density = 1}
local function onEnterFrame()
world:step(1/60, 8, 3)
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
local function applyLinearImpulse()
local x, y = body:getPosition()
print(scale)
body:applyLinearImpulse(0, -20, x/scale, y/scale)
end
stage:addEventListener(Event.MOUSE_DOWN, applyLinearImpulse)
local debugDraw = b2.DebugDraw.new()
world:setDebugDraw(debugDraw)
stage:addChild(debugDraw)
https://sites.google.com/site/xraystudiogame
https://sites.google.com/site/xraystudiogame