Physics Scaling between multiple resolutions

Hi,

I’m trying to solve a particular issue of supporting some simple physics scaling between devices with multiple resolutions, but I’m running into some problems. I was hoping that I would just be able to scale the force(s) applied to my objects based on the difference in resolutions to accomodate for the larger screens, but it doesn’t seem to give 100% perfect results. Here’s an example of what I’m doing:

[lua]xForce = 300;
yForce = 400;
newForce = xForce * (display.viewableContentWidth/480);
newForce = yForce * (display.viewableContentHeight/320);[/lua]

xForce and yForce could equal any amount, but we’ll use -300 and -400 as an example. So, in my game, I’m firing objects using object:applyForce(). I need to be sure that the object hits a target object at a certain distance away, which is currently no problem. The problem occurs when I move into a different environment from my standard iPhone resolution of 320x480 into, say, an Amazon Fire resolution of 1024x600.

I’ve tried scaling everything uniformly, so positioning the object to be fired would look like this:

[lua]xForce = -300;
yForce = -400;
xPos = 250;
yPos = 200;
targetObjectX = 50
targetObjectY = 30

newForceX = xForce * (display.viewableContentWidth/480);
newForceY = yForce * (display.viewableContentHeight/320);
newPosX = display.viewableContentWidth * (xPos/480);
newPosY = display.viewableContentHeight * (yPos/320);
newTargetObjX = display.viewableContentWidth * (targetObjectX/480);
newTargetObjY = display.viewableContentHeight * (targetObjectY/320);

objectToFire:applyForce(newForceX, newForceY, objectToFire.x, objectToFire.y)[/lua]

However, my issue seems to be that increase the Force by a uniform about according to the new change in distance doesn’t seem to guarantee that it will have the force required to travel the new distance. I’ve even tried scaling my gravity as well, as such:

[lua]physics.setGravity(0, 15 * (display.viewableContentHeight/320));[/lua]

All of the X values are scaled by the new contentWidth divided by 480 because 480 was the original width of the resolution on the iPhone simulator that I was using. All Y values are scaled by the new contentHeight divided by 320 for the same reason as mentioned before, 320 was my original height on my iPhone simulator.

So, is this a viable solution and perhaps I’ve messed up somewhere down the line with my math? Or will scaling the Force on a 1:1 scale with the increased distance between the starting point and the target not result in the proper amount of force necessary to propel the ball over the new distance?

Thanks for any advice! [import]uid: 138013 topic_id: 28334 reply_id: 328334[/import]

Hi Tom,

Are you using any of the dynamic content scaling options (http://developer.coronalabs.com/content/configuring-projects#Dynamic_Content_Scaling)?

The reason I ask is, if I understand your problem correctly, letterbox scaling may be a solution. You would specify a width of 320 and height of 480, which would allow you to use those “virtual” coordinates on any device. As a result, however you’ve calibrated your applyForce for the iPhone coordinates will work on any device.

  • Andrew [import]uid: 109711 topic_id: 28334 reply_id: 114521[/import]

Hey andrew,

I am using the dynamic scaling options, but I’m using zoomEven to fill the entire display on each different device/resolution I’m supporting. I’ve set specific widths/heights for each one, so I figured setting zoomEven or Letterbox wouldn’t make much of a difference.

I have set every device to use ‘zoomStretch’ and was able to get that working well, but this of course stretched all my assets to fit and the quality definitely suffered.

I will definitely try switching over to letterbox to see if I can get that to work, thanks for the suggestion. [import]uid: 138013 topic_id: 28334 reply_id: 114547[/import]

Here’s an update:

Letterbox scaling versus ZoomStretch versus ZoomEven don’t seem to make any particular difference to the virtual coordinates other than the content width and content height that I am specifying. Letterbox scaling of something more unusual, say 330x552, behaves the same as zoomEven or zoomStretch at the same 330x552 resolution.

*EDIT*
By behaves the same, I mean in relation to the virtual coordinates. Letterbox of course will give me unfilled pixels, zoomStretch stretches my elements to fit, etc, but the virtual coordinates of 330x552 remain the same as far as the physics is concerned.
*/EDIT*

The big issue for me is: Mathematically scaling the physics based on the resolution to make identical shots between multiple resolutions. If I set all of my platforms to use a 320x480 resolution and use the same physics scaling I currently have, everything works fine, but I want to take advantage of the different screen sizes and pixel densities, so I would prefer not to limit every platform to use this 320x480 content dimension if I don’t have to.

As it currently stands, scaling the physics by the math I posted in the opening post doesn’t result in 100% accuracy. It works pretty close, but it just gets the object in the general area, based on the position/force it sometimes will still hit the target, and sometimes it won’t have enough X or Y force to reach its goal. I’m currently scaling Force to Distance on a 1:1 ration, so for every percentage of new distance I’m putting between the target, I’m also putting that same amount percentage increase on the force. The blunder seems to be that increasing force to distance on a 1:1 doesn’t guarantee the same trajectory/path to be taken.

Any advice or tips are appreciated [import]uid: 138013 topic_id: 28334 reply_id: 114769[/import]

Hi there,

Thanks for that extra detail – I understand better now what you’re trying to do, although I’m still not clear why letterbox isn’t suitable (is the only issue the unfilled pixels, or is it also still the physics?).

One thing you may want to try is applying an impulse instead of a force. Since the force is applied over time, it’s not clear to me why scaling the force proportionately would necessarily have to yield a proportionate increase in distance.

  • Andrew [import]uid: 109711 topic_id: 28334 reply_id: 115245[/import]