Physics problem when using the ultimate config.lua file

Hello.

I have a problem when using physics objects together with the ultimate config.lua file.

The problem is that when I use the ultimate config file and apply a linear impulse to an object, the object travels different distances for different devices. I tested it with Corona simulator.

Attached to this post there is a sample project I made to demonstrate this problem. When open, click on the circle and watch the Simulator output and see how far it has traveled. Then change the device type and repeat the process and see that the distance traveled is not the same.

When I used the default config file when a new project is created this was not an issue. The distance traveled was constant across multiple devices.

In the attached project the config.lua file is the ultimate one, and for test purposes the config_default.lua file is the default one. In case any of you might want to test this out.

What I figured so far is that when using the ultimate config file the screen resolution is not the same and this affects the calculated mass of the object and therefore the impulse has different effects on the object.

I think that by using a dynamic value for the density or the impulse (or both) of the object I might make it behave properly, but I have no idea how.

Any help or hint is appreciated. Thank you.

For convenience I will paste the code in the attachment here too:

local physics = require("physics") physics.start() local appWidth = display.contentWidth local appHeight = display.contentHeight local appUnitX = appWidth/20 local playDt local runtime = 0 local objRadius = appUnitX local impulseX = 100 local impulseY = -100 local objDensity = 5 local function launchTestObj(event)              physics.addBody(event.target, {density=objDensity, friction=0.0, bounce=0.0 } )     event.target:applyLinearImpulse( impulseX , impulseY, event.target.x, event.target.y )     -- prints some info     print("width:" .. appWidth .. " height:" .. appHeight .. " density:" .. objDensity)     print("mass:" .. event.target.mass .. " impulseX:" .. impulseX .. " impulseY:" .. impulseY) end local testObj = display.newCircle(0, 0, objRadius) testObj.anchorX = 0.5; testObj.anchorY = 0.5 testObj.x = 0;testObj.y = appHeight testObj.stopPhysics = false -- to remove physics object only once testObj:addEventListener( "tap", launchTestObj ) local function getDeltaTime()     local temp = system.getTimer()  --Get current game time in ms     if runtime == 0 then runtime = temp-33.33 end -- force the dt to be 1 for the first frame     local dt = (temp-runtime) / (1000/30)  --60fps or 30fps as base     runtime = temp  --Store game time     return dt end local function frameUpdate()     --Delta Time value     playDt = getDeltaTime()     if testObj.y \> appHeight and testObj.stopPhysics == false then         testObj.stopPhysics = true         physics.removeBody( testObj )         print("distance (measured in appUnitX): " .. (testObj.x/appUnitX))     end end Runtime:addEventListener( "enterFrame", frameUpdate )  

I got the config file from this tutorial:

http://coronalabs.com/blog/2012/12/04/the-ultimate-config-lua-file/

Hi @g.robert9,

Physics is based on the “content area” in Corona, like many other aspects. Physical objects will behave the same within this content area on different devices. Meaning, if you apply forces to an object from some point (a tap location) inside the content area, it will behave the same. However, if you are on a “tall” screen or a “wider” screen, and you tap outside of the content area, you’ll get more force applied.

One way to work around this is to use the “zoomEven” scale mode instead of “letterbox”. This makes the content area fill the entire screen in whatever direction it can, with any extra content flowing off the screen (thus, the user can’t register a tap there).

If you haven’t read this guide, please do so to fully understand the content area and scale choices:

http://docs.coronalabs.com/guide/basics/configSettings/index.html

Hope this helps,

Brent

Thank you for your response Brent.

I have tried zoomEven just now and it does not solve the problem.

The ball still travels different distances for different screen sizes. By different distances I mean by percentage. For example (not actual numbers) if on Ipad Retina it travels 75% of the screen, on Droid it travels 50% for the same impulse.

I don’t know if this helps but when I apply the linear impulse I always apply it to the center of the object.

On the attached project one can test this out easily.

And thank you for the link too.

Hi @g.robert9,

I tested your project and the behavior. In this case, it may be better to retain a dedicated content area size instead of using the “variable size” in the Ultimate Config, primarily to maintain consistency for the physics actions. If you need to align certain UI elements to different edges of the screen on all devices, there are alternative methods.

Take care,

Brent

Thank you Brent.

I will keep only one content area size and I will search for a solution (as you said) to align some elements to the edges.

In case you have a link about this alternate aligning would be great, if not I will try to calculate this somehow.

Personally, I prefer the following simple method for edge alignment. Using “letterbox” scale setting, I add this one line to the top of my main.lua file:

[lua]

local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY)

[/lua]

These values should then represent the “letterbox bar width” no matter which device you run the app on. So, if I want to push something against the left edge, I put its left edge at “-ox”. If I want to push it against the right edge, I put its right edge at the width of the content area and then add “ox” to it. Same basic idea for the vertical space, just use “-oy” and “oy” accordingly.

Hope this helps,

Brent

Hi @g.robert9,

Physics is based on the “content area” in Corona, like many other aspects. Physical objects will behave the same within this content area on different devices. Meaning, if you apply forces to an object from some point (a tap location) inside the content area, it will behave the same. However, if you are on a “tall” screen or a “wider” screen, and you tap outside of the content area, you’ll get more force applied.

One way to work around this is to use the “zoomEven” scale mode instead of “letterbox”. This makes the content area fill the entire screen in whatever direction it can, with any extra content flowing off the screen (thus, the user can’t register a tap there).

If you haven’t read this guide, please do so to fully understand the content area and scale choices:

http://docs.coronalabs.com/guide/basics/configSettings/index.html

Hope this helps,

Brent

Thank you for your response Brent.

I have tried zoomEven just now and it does not solve the problem.

The ball still travels different distances for different screen sizes. By different distances I mean by percentage. For example (not actual numbers) if on Ipad Retina it travels 75% of the screen, on Droid it travels 50% for the same impulse.

I don’t know if this helps but when I apply the linear impulse I always apply it to the center of the object.

On the attached project one can test this out easily.

And thank you for the link too.

Hi @g.robert9,

I tested your project and the behavior. In this case, it may be better to retain a dedicated content area size instead of using the “variable size” in the Ultimate Config, primarily to maintain consistency for the physics actions. If you need to align certain UI elements to different edges of the screen on all devices, there are alternative methods.

Take care,

Brent

Thank you Brent.

I will keep only one content area size and I will search for a solution (as you said) to align some elements to the edges.

In case you have a link about this alternate aligning would be great, if not I will try to calculate this somehow.

Personally, I prefer the following simple method for edge alignment. Using “letterbox” scale setting, I add this one line to the top of my main.lua file:

[lua]

local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY)

[/lua]

These values should then represent the “letterbox bar width” no matter which device you run the app on. So, if I want to push something against the left edge, I put its left edge at “-ox”. If I want to push it against the right edge, I put its right edge at the width of the content area and then add “ox” to it. Same basic idea for the vertical space, just use “-oy” and “oy” accordingly.

Hope this helps,

Brent