2
\$\begingroup\$

I am making a breakout clone for mobile and was wanting to make the ball speed to be related to the screen size. Larger screen would mean faster speed.

Right now I have it set like so:

dirx=math.random(-5,5)*(maxx/320)
diry=math.random(3,7)*(maxy/480)

My reasoning for this is that 320x480 is iPhone resolution so I figured it would be a good base to start at. For the record, it actually runs pretty well on iPhone setting.

Then for the actual movement I just add dir to the appropriate direction.

ball.x=ball.x+dirx
ball.y=ball.y+diry

The problem is if I change to a device with a larger screen resolution, the ball is crazy fast and is very jumpy.

Is some kind of formula that I can use to multiply to the random that will make the speed appear uniform across devices?

\$\endgroup\$
5
\$\begingroup\$

You really shouldn't use pixel coordinates for game logic.

Just use coordinates that are independent of screen size, and make rendering code account for different screen sizes, this way you get much more flexible and maintainable code.

So for example assume you are using 1 meter as one unit. Your screen is 2 meters high and 1 meter wide(on every device). So the resulting pixel position can be calculated like:

PixelCoord.x = Coordinate.x * (DeviceSize.x / TableSize.x)
PixelCoord.y = Coordinate.y * (DeviceSize.y / TableSize.y)

where Devicesize is the device resolution and TableSize is the size of play area in meters( in this case the latter is { x=1, y=2 } )

Note: This is just a very simple implementation, and needs some refinement, but I hope that you get the idea.

Note 2: I assume you are using floating point numbers to represent coordinates.(should be the case already with lua.)

Edit:

In the example above Coordinate.xy(the position of the object) ranges[0, TableSize.xy], which means x - [0,1] y - [0, 2]. So for example an object in the middle of the screen would have the coordinates [0.5, 1]

\$\endgroup\$
  • \$\begingroup\$ I'm having trouble conceptualizing this answer. Can you offer an example? \$\endgroup\$ – David Starkey Dec 23 '13 at 21:31
  • \$\begingroup\$ @DavidStarkey Edited answer, any questions? \$\endgroup\$ – akaltar Dec 23 '13 at 21:37
  • \$\begingroup\$ So there is essentially a grid over the screen and the object moves around the grid which stretches based on resolution? Wouldn't there still be some choppy movements on larger screens? Or would you make the grid big and have smaller screen move fractions of a pixel? \$\endgroup\$ – David Starkey Dec 23 '13 at 22:01
  • 1
    \$\begingroup\$ By using floating point positions you have a grid which is extremely dense, but Yes, the grid stretches based on resolution. No there would be no choppy movements as mentioned above. Yes you are correct, the grid is dense enough and an object could move a fraction of a pixel. I also edited the answer which should clarify this. \$\endgroup\$ – akaltar Dec 23 '13 at 22:09
  • 2
    \$\begingroup\$ But you should take into account also aspect ratio. \$\endgroup\$ – Emanuele Paolini Dec 23 '13 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.