Advertisement

Ortho Setup and centering player

Started by October 28, 2004 10:39 PM
4 comments, last by bu11frog 20 years, 1 month ago
I'm making a 2d space ship game and I need some advice. 1. I'm setting up my Ortho Projections like this glOrtho(0,width,0,height,-1,1); I let the user select the resolution, so the width and height is different for each resolution. Is there anything wrong with this? Most tutorials base it on the width and height but I think they always used a fixed resolution. 2. I want to keep the player in the center of the screen when they move. First I did this by drawing the player in ortho at the position that is half the width and height of the screen. This visually works but when I move the player by changing the players position variable, my player is still at the same point in opengl units at the center of the screen (ortho) but it looks correct becuase I scroll the background while the ship is at the same point. I ran into trouble when I added an enemy and tried to do collision detection. The players collision doesn't match up to the enemy on the screen because of the way I am centering it. I have to draw the enemy relative to the player in order to keep it on the ortho screen. Is there a better way to center the player? How can I make the screen always center around the players position? Should I not be using Ortho? Or maybe my problem is that I am trying to make the ships position match up to the same opengl units, I wanted to keep things simple and that seemed the easiest way. I know this is probably really confusing because I am having trouble explaining what I mean. If something needs to be explained again please let me know. Thanks
Quote: Original post by ssnhavok
I'm making a 2d space ship game and I need some advice.

1. I'm setting up my Ortho Projections like this
glOrtho(0,width,0,height,-1,1);
I let the user select the resolution, so the width and height is different for each resolution. Is there anything wrong with this? Most tutorials base it on the width and height but I think they always used a fixed resolution.


Nothing wrong in this. Many games allow you to set/change resolution.

Quote: Original post by ssnhavok
2. I want to keep the player in the center of the screen when they move. First I did this by drawing the player in ortho at the position that is half the width and height of the screen. This visually works but when I move the player by changing the players position variable, my player is still at the same point in opengl units at the center of the screen (ortho) but it looks correct becuase I scroll the background while the ship is at the same point.
I ran into trouble when I added an enemy and tried to do collision detection. The players collision doesn't match up to the enemy on the screen because of the way I am centering it. I have to draw the enemy relative to the player in order to keep it on the ortho screen.

Is there a better way to center the player? How can I make the screen always center around the players position? Should I not be using Ortho? Or maybe my problem is that I am trying to make the ships position match up to the same opengl units, I wanted to keep things simple and that seemed the easiest way.



Centering the player would be a matter of height/2, width/2 - add and subtract the size of rectangle for the player to get the square half way across on either sides. I assume you are using glTranslatef to move. This will actually move the axis so player position will still be the same. What I would suggest is to no use the glTranslatef method and manually modify the position of the vertices so the axis is at a constant position. Considering the player/enemies will be only rectangles you will hardly be making many calls to update the position per element.

There might be a better way.
The more applications I write, more I find out how less I know
Advertisement
Thanks for your help CRACK123.
I don't think I explained things very well which is causing some confusion.

Quote: Original post by CRACK123
Nothing wrong in this. Many games allow you to set/change resolution.


I know it's ok to let the user select resolution, my question is when they select different resolutions that changes the ammount of ortho units on the screen.

If they select 640x480 the ortho setup looks like this glOrtho(0,640,0,480,-1,1);

If they select 1024x768 the ortho setup looks like this glOrtho(0,1024,0,768,-1,1);

Is this ok for the units to change for different resolutions? Should I not be basing the ortho setup on the current resolutions width and height. If so, what values should I always use?


Quote: Original post by CRACK123
Centering the player would be a matter of height/2, width/2 - add and subtract the size of rectangle for the player to get the square half way across on either sides. I assume you are using glTranslatef to move. This will actually move the axis so player position will still be the same. What I would suggest is to no use the glTranslatef method and manually modify the position of the vertices so the axis is at a constant position. Considering the player/enemies will be only rectangles you will hardly be making many calls to update the position per element.

There might be a better way.


I am currently using "height/2, width/2" which I mentioned in my first post. Yes I am using glTranslatef but I don't see how that differs in ortho mode from manually drawing the vertices at the correct centered spot(maybe I'm wrong though). Will the axis move in ortho? In ortho wont the corners of the screen always correspond to the values you use in glOrtho()?

In ortho if your screen is (0,0) bottom left and (640,480) top right. And then you draw something at (1000,1000) is there a way to move the screen coordinates so that it centers around (1000,1000)?

I think I tried to use ortho in the wrong manner. I think I either need to stop using ortho or I need to separate my game world coordinates from the opengl ortho screen coordinates and just draw the 640x480 game world coordinates that surround my player, with the player coordinates being always in the exact center ( of course I have no idea how to do that :-/ ).

Does anyone understand that? I'm not even sure if I do. Anyone ever run into this problem and can give me some advice on what would be the best approach?

Thanks
I think what you should just do, is regardless of resolution, set the top right parameters to either (100, 100) or (1, 1). This would allow you to work in percentages of the screen height, independently of resolution. I.e, 40% from the top is 40% from the top, regardless of whether you're in 640x480 or 1600x1200, etc.

Edit: Part 2

As for your second problem, I think this would work: Instead of setting up the bottom left as (0,0) and the top-right as (100, 100) every frame, you should set these based on your player's position.

So, if your player is at (50, 50), then the bottom left should be (0,0), and top right (100, 100), but if your player is at (0,0), then set the bottom left as (-50, -50) and top right as (50,50).

I.e, always keep bottom left and top right the same distance from the player, regardless of his position.

Screen.BottomLeft.X = Player.X - 50
Screen.BottomLeft.Y = Player.Y - 50
Screen.TopRight.X = Player.X + 50
Screen.TopRight.Y = Player.Y + 50

[Edited by - slyterence on October 30, 2004 4:29:15 AM]
We scratch our eternal itchA twentieth century bitchWe are grateful forOur Iron Lung
Thanks for your reply slyterence.

Making the ortho setup like percentages sounds like a good idea.
Have you used this method before? Has anyone else tried it?

Now that I think of it, there might be one problem by doing it with percentages. The screen is wider than tall so if I do 100 units square, then my player will move faster along the y axis than the x axis (I think). I guess I can make the width 100% and then the height the correct percent in the ratio of screen size (roughly 75% i think).


About your suggestion for keeping the player centered, I am going to try that because I can't think of anything else at the moment. But I don't think it's the best way of doing it. How much will my games speed be affected by changing the ortho projection every time the player moves?

Thanks again.
Okay, mebbe I'm all confused, but...

First things first. Do all collision stuff before you start drawing. You need to have the final worldspace coords before you draw anything.

Next, cosider the following variables:

int AbsoluteResolutionX; //number of pixels wide
int AbsoluteResolutionY; //number of pixels high

int WorldToViewModifier = ?

If you made WorldSpace Units equal to ViewSpace Units then technically if your player's ship is 10 WorldSpace Units long then your Image of that ship would have to be 10 pixels tall. This would utterly suck cause you wouldnt be able to get any detail in your images. So, we use a modifer to decide how many pixels will make up a single world unit. So, again, if the ship is 10 WorldSpace Units long and your Image of that ship is 100 pixels tall then your Modifer would be 10.

Lets continue:

int ScreenLeftBounds;
int ScreenRightBounds;
int ScreenUpperBounds;
int ScreenLowerBounds;

ScreenLeftBounds = PlayerWorldPosition.X - (WorldToViewModifier *(AbsoluteResolutionX / 2));
ScreenRightBounds = PlayerWorldPosition.X + (WorldToViewModifier * (AbsoluteResolutionX / 2));
ScreenUpperBounds = PlayerWorldPosition.Y + (WorldToViewModifier * (AbsoluteResolutionY / 2));
ScreenLowerBounds = PlayerWorldPosition.Y - (WorldToViewModifier * (AbsoluteResolutionY / 2));

This gives you the worldspace range of the viewing space. Now, You can easily clip out the objects that arent in that space by checking their worldspace coords versus the Boundaries. If they are in view, then the way translate their worldspace coords into viewing coords is:

int PlayerViewPosition.X = AbsoluteResolutionX / 2;
int PlayerViewPosition.Y = AbsoluteResolutionY / 2;

int EnemyViewPosition.X = PlayerViewPosition.X + (WorldToViewModifier * (EnemyWorldPosition.X - PlayerWorldPosition.X));

int EnemyViewPosition.Y = PlayerViewPosition.Y + (WorldToViewModifier * (EnemyWorldPosition.Y - PlayerWorldPosition.Y));

Similar equations would be used for all objects in the world.

I hope this all makes sense and helps to solve your problems. Let me know how you're coming along or if you think my idea just outright sucks. Hehe... I dont post suggestions very often and very rarely get any feedback.

[Edited by - bu11frog on November 2, 2004 7:35:55 PM]
Hard work USUALLY pays off in the future, but laziness ALWAYS pays off right now.

This topic is closed to new replies.

Advertisement