Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is their a way to make it where no matter what the screen size is my sprites will always start at a certain position. I am trying to get my sprites to start in the lower right corner area of the screen. I am using a 2D SurfaceView.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Get the screen size (this StackOverflow answer says how) and you'll have a vector (of the mathematical variety) representing the bottom-right coordinate of the screen. Now you can just place stuff relative to this vector in order to place things relative to the bottom-right of the screen instead of the top-left.

I'm assuming that on Android, (0,0) is the top-left corner of the screen. If it isn't, I'll edit this answer to accommodate.


In case it isn't clear how that would work, here's an explanation

First: please make sure you have a basic understanding of vectors (not the list type, but the mathematical object). The vector tag's description links to a succinct tutorial on vectors within the context of games. Read part one at least. A basic understanding of vectors is critical for game programmers, if only because it will make your life easier.

Remember that the origin of the screen, which sits at the top left, is (0, 0). Consider that when you place something at (30, 30), you're actually placing it at (0, 0) + (30, 30). You're placing it at (30, 30) relative to a particular point on the screen - the origin.

So if you want to place something 25 pixels left and 20 pixels up from the bottom-right corner, you're placing it at (-25, -20) relative to the bottom-right corner. Assuming (right, bottom) represents the bottom-right corner of the screen, that means you just place your sprite at (right, bottom) + (-25, -20).

In code, assuming you have a 2-dimensional vector class named Vector2, which represents a mathematical vector (NOT the vector list type!):

// From the StackOverflow answer linked at the beginning
Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

// This is the coordinate of the bottom-right most pixel on the screen
Vector2 bottomRight = new Vector2(width, height);

// This is the position you want the sprite relative to the bottom-right point
// (25 pixels left, 20 pixels up)
Vector2 distFromBR = new Vector2(-25, -20);

// This is the position you should place that sprite to have it in the right position
Vector2 spritePos = bottomRight + distFromBR;
share|improve this answer
    
Thanks, I understood your original post, except I am getting error with the getWindowManager() but I just used the getWidth() & getHeight(). I know about vectors, from a question I posted trying making my bullet work. I do have a question though, when ever I use Vector2 it tells me I need to make class or interface for it. Could you possibly clarify that part. –  Manji Jul 10 '11 at 2:29
    
@Manji: I am making an assumption that you have a 2D vector class and it's named Vector2. You might not have a 2D vector class, or it might not be named Vector2 if you do. If Android doesn't natively support a vector class, there are vector math libraries available on the internet. I don't have any recommendation as to which you should use since I don't develop in Java, but this one came up in another SO question. –  doppelgreener Jul 10 '11 at 2:51
    
Android has a Vector class but it is the same as ArrayList with a few differences. Thanks for clearing that bit of information up for me :) –  Manji Jul 10 '11 at 2:58
    
Right, that's the sort of vector I don't mean. I'll edit to clarify - but I mean a mathematical vector, not the list type. –  doppelgreener Jul 10 '11 at 3:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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