Sprite physics and screen res / DPI independence

Problems with Canvas, OpenGL, etc...

Sprite physics and screen res / DPI independence

Postby mooonbuggy » Fri Jan 06, 2012 3:48 am

Hi all

I'm writing a 2D game and am having a really hard time getting my head around screen independence, I've read all the official guides, but I still can't work it out so I'm hoping someone here will set me straight once and for all!

I have a sprite which jumps like so:

Y Co-ordinate = Y Co-ordinate - jumpamount (say 15 DIP worked out by 15 * (current screen dpi/160)
jumpamout = jumpamount - step amount (again in DIP, this time I've gone with 1 on an MDPI screen, so 1.5 on HDPI, .75 on an LDPI)

Now, obviously I can't subtract 1.5 from the jumpamount (or .75 for LDPI screens) as I can only work in whole pixels, so how exactly is this supposed to be done? What I'm trying to achieve is that the sprite jumps to the same heigh on each of the screens - when I say the same height, I mean of course, relative to the screen (like say, if, at it's max height it is exactly 1/2 way up the screen on a MDPI screen, then I want it be 1/2 way up the screen at that point on all the different screens).

If I explicitly use 1 for the 'step' amount in the above psuedo-example, then great, it's always a whole number, but it doesn't result in the correct jump height across all screens.

I just can't seem to find anyone who knows the answer - I've been trying to work it out for myself for about 4 months now with no luck so I'm getting a bit frustrated to say the least!! :lol:

Would really appreciate it if someone could explain to me exactly how this is supposed to work on Android!

Thanks very much and happy new year!!!!!

8)
mooonbuggy
Freshman
Freshman
 
Posts: 7
Joined: Wed Nov 02, 2011 1:26 am

Top

Re: Sprite physics and screen res / DPI independence

Postby flygirl » Fri Jan 06, 2012 4:23 am

MTS to MOV Converter is a powerful and easy-to-use MPEG to AVI converter, which enables you to effortlessly convert MTS to AVI, MTS to MPEG, MTS to MP4, MTS to 3GP, MTS to WMV, MTS to RM, MTS to RMVB, MTS to FLV(flash), MTS to VCD/SVCD/DVD, MTS to iPod MP4, MTS to PSP MP4 and so forth with high quality. It supports adding watermarks,subtitles to the videos.
MTS to MP4
MTS to WMV
MTS to MPG
flygirl
Freshman
Freshman
 
Posts: 9
Joined: Fri Jan 06, 2012 4:19 am

Re: Sprite physics and screen res / DPI independence

Postby MichaelEGR » Fri Jan 06, 2012 4:39 am

It's too bad the forums are filling with spam again...

>I'm writing a 2D game and am having a really hard time getting my head around screen independence

Indeed.. A tricky bit that you'll come across is the density independent pixels are not good enough now that there are tablets with larger screens and similar densities to the original HVGA / WVGA screen sizes / densities. What this means is that for a tablet with a higher screen resolution than WVGA may have the same density as a WVGA screen and you can't use DIP to calculate screen independence especially for game dev / entity movement.

The best solutions are OpenGL camera / perspective based or if you are using the Android 2D API roll your own coordinate translation system such that you define a coordinate system that maps to the device independent of pixels. I have code posted in my past posts on exactly how to do this for the 2D case without OpenGL, but don't have time to dig it up...

Best of luck.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Re: Sprite physics and screen res / DPI independence

Postby mooonbuggy » Tue Jan 10, 2012 9:12 pm

Thanks very much for the advise, yep it is a bit of a problem. I think I'm getting there in understanding it and will certainly look for your post. :)

One other question relating to the scale of drawables. When I display my sprites from the LDPI and HDPI folders (on low / high density screens respectively), they seem to 'appear' on the screen pretty much the same size which is great, however, when I'm displaying them from MDPI (on a medium density screen), they are slightly bigger (ie, in relation to the screen - they take up more of the available screen). I can't work it out as I'm pretty sure they are scaled correctly. This is how I've scaled them......

LDPI at 75%
MDPI at 100%
HDPI at 150%
XHDPI at 200% (I've not tested XHDPI though thus far).

Any ideas why the drawables from MDPI are appearing bigger in relation to the screen than the others which appear to match?! Thanks again for any advice.
mooonbuggy
Freshman
Freshman
 
Posts: 7
Joined: Wed Nov 02, 2011 1:26 am

Re: Sprite physics and screen res / DPI independence

Postby BUKsAPPs » Tue Jan 10, 2012 10:16 pm

dimensions depend on more than just the DPI, the other major factor is the actual screen size in inches, which also varies from device to device on android, aspect ration is also another issue that you will see.

First example will be only MDPI - 160dpi - I will be talking landscape when saying width and height.
---A fairly typical MDPI phone will have a resolution of 320x480
---Dell Streak has an MDPI resolution of 480x800
So if you try to design your character to be 32 pixels tall(even if you are counting for DPI) he will be 1/10 the screen on the phone, but only 1/15 of the screen of the Streak. This is very likely a similar issue to why your character looks bigger on your MDPI device than your HDPI, unless the resolution of your HDPI device is 1.5 times your MDPI device they will be a different size RELATIVE TO SCREEN SIZE.

What you need to do is scale by screen size NOT density. You can still use multiple densities images to get a better start place and reduce file sizes on MDPI/LDPI devices, but don't worry about that for now. Thinking about scaling your character to a percent of the screen height. Assuming you want the character to be 10% of the screen height, If your image is 32 pixels, and the screen height is 480, you need to multiply by 1.5, even though you are on an MDPI device.

Now the issue of aspect ratio come is because if you scaled the width by 1.5, you would not fill the screen on the streak because it is 1.666 times as wide even though it is only 1.5 times as tall(landscape) You will have to decide what is best, stretch the characters X by 1.66 to make them a bit fat, loose some screen space, if it is a scrolling game it probably doesn't matter.

But the basic idea is that for most games DPI doesn't matter as much as resolution. There may be cases where you only want your character to be 1/2 a "real" inch, and let any extra screen size show more of the map, and that is fine and when you do want to work in dpi - and you will see what you are currently seeing, your character will appear bigger or smaller IN RELATION TO THE SCREEN.

I hope this help, ask if anything is unclear.
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Re: Sprite physics and screen res / DPI independence

Postby mooonbuggy » Wed Jan 11, 2012 1:08 am

Thank you sooooo much for the detailed post, I'm going to study it and try to understand it & I will post any questions once I've read it if you don't mind!

My real/physical device is a Galaxy S i9000 but I'm testing on 3 emulators - a WQVGA400 for Low, an HVGA for Normal / Medium and a WVGA800 for high. Would you expect the MDPI one to look different on these emulator configurations? I'm selecting a 'scale display to real size' value which is uniform across all 3 emulators.

Again, thanks very much for the post - I will study it closely! :-)
mooonbuggy
Freshman
Freshman
 
Posts: 7
Joined: Wed Nov 02, 2011 1:26 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests