• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Moving sprites?

droidzip

Lurker
Hi All

I have a question that on the surface, would appear to be quite valid, however, I've not been able to find an answer to it for well over a year!!
I really hope someone could point me in the right direction!! :p

I'm developing a 2d game based on Surfaceview and basically all I would like to know is now do I move a sprite so it moves the same speed on all screen dpi's/resolutions?

What I mean is:

I use an explicit value (say x=x+1) then after 240 cycles, on a 240x400 screen, the sprite would have moved the whole width of the screen. But, on a 800x480 screen it would only have made it halfway (thus appearing slower).

My sprites are scaled and appear on the screens OK (ie, they looks the same size relative to the screen / ratio they are being displayed on).

The above was just an example - I'm not using an explicit value, I'm using a value based on the DPI of the screen , but this isn't working either and I'm wondering if anyone knows how it's meant to be done?

Someone recently tried to explain to me that it's done with calculations based around the delta-time but I don't seem to be able to get that to work - I really would appreciate someone pointing me in the right direction or explaining it in (very) basic steps!!h :rolleyes:

Thank you sooooo much!! :D
 
You need to create a dpi scale by taking the current device's dpi and then divide it by a target dpi. Then, whenever you modify an object's transform in any way, you need to first multiply the transform value by the dpi scale. for example:
Code:
gameObject.transform.position.x += 3.0f * dt * dpiScale;

where dt is the time since the last frame (promotes frame-rate independance) and dpiScale is the scale factor for the current device's dpi. If you are on a device with the target dpi, then this would be the multiplicative identity, 1.
 
Back
Top Bottom