Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Centering an object in the screen  (Read 936 times)
0 Members and 1 Guest are viewing this topic.
Offline thunderfist123

Senior Newbie





« Posted 2013-11-07 02:58:39 »

Hey guys! First post here Smiley

So I have a little problem. I have a 2D platformer, and I want to keep the player in the center of the screen. Well, that should be easy right? Just set the camera position to half the size of the screen plus half the size of the player. So, I did that. But the camera actually scrolls slower than the player, so that the player can actually escape the screen. I'll show you some code now:

The camera class:
1  
2  
3  
4  
5  
6  
public Camera(){
   }
   
   public void update(Player p){
      glTranslatef(Main.WIDTH / 64 / 2 + -p.getPos().x / 2, Main.HEIGHT / 64 / 2 - -p.getPos().y / 2, 0);
   }

Here's how I update my player's position:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public void update(){
      if(Keyboard.isKeyDown(Keyboard.KEY_D)){
         this.move(MOVESPEED, 0);
      }
     
      if(Keyboard.isKeyDown(Keyboard.KEY_A)){
         this.move(-MOVESPEED, 0);
      }
   }

Which then in turn calls the move method:
1  
2  
3  
public void move(float x, float y){
      this.getPos().set(this.getPos().x + x, this.getPos().y + y);
   }

I create the player and initialize it's position to half the screen, which works fine. But when I start scrolling, the player isn't centered in the screen! How can I fix this?
Offline lcass
« Reply #1 - Posted 2013-11-07 07:43:57 »

If you are scaling your screen remember to increase the xoffset and yoffset correspondingly.
Offline thunderfist123

Senior Newbie





« Reply #2 - Posted 2013-11-07 11:04:20 »

I already do, that shouldn't matter.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dxu1994
« Reply #3 - Posted 2013-11-07 11:05:28 »

try adding a call to glLoadIdentity() before your glTranslatef() call.

I have a feeling that your glTranslate( .. ) calls are stacking up causing the viewport to go out of bounds.

Offline thunderfist123

Senior Newbie





« Reply #4 - Posted 2013-11-07 11:08:37 »

Nope, that didn't work :/ Here, I'll show you how I update the camera, maybe that'll help:
1  
2  
3  
4  
5  
6  
7  
8  
public void render() {
      glLoadIdentity();
      glPushMatrix();
      cam.update(mobManager.getP());
      tileManager.render();
      mobManager.render();
      glPopMatrix();
   }

That's my method for rendering all my stuff. cam.update is there because it calls glTranslate, and it needs to translate the geometry in the tileManager and mobManager. I added the glLoadIdentity call like you said.

Edit: The weird thing is, is that I print out the camera and player position vectors, and they're the same. Yet, the "camera" scrolls slower.
Offline Dxu1994
« Reply #5 - Posted 2013-11-07 11:12:42 »

Nope, that didn't work :/ Here, I'll show you how I update the camera, maybe that'll help:
1  
2  
3  
4  
5  
6  
7  
8  
public void render() {
      glLoadIdentity();
      glPushMatrix();
      cam.update(mobManager.getP());
      tileManager.render();
      mobManager.render();
      glPopMatrix();
   }

That's my method for rendering all my stuff. cam.update is there because it calls glTranslate, and it needs to translate the geometry in the tileManager and mobManager. I added the glLoadIdentity call like you said.

Note how you have

1  
glTranslatef(Main.WIDTH / 64 / 2 + -p.getPos().x / 2, Main.HEIGHT / 64 / 2 - -p.getPos().y / 2, 0);


?

See how it says p.getPos().x / 2? That should not be divided by 2.

That's why your viewport is moving at half the speed of the player.

Quick example of how I do it in my platformer:

1  
2  
Core.view.tx = x - Core.GameWidth/2;
        Core.view.ty = y - _area.height / 2 - 350 * Core.GameHeight / 800;


So what I'd change your code to is:

1  
glTranslatef(-(p.getPos().x - Main.WIDTH/2 + 64/2), -(p.getPos().y - Main.HEIGHT/2 + 64/2), 0);


Assuming your player is 64x64 and origin is top left.

Hope this helps Smiley

PS: your glPushMatrix and glPopMatrix functions are pointless, you're saving a copy of the identity matrix to the stack, doing nothing with it and then popping it after.
Those can be removed.

Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #6 - Posted 2013-11-07 11:13:02 »

I had a problem like this just a couple of weeks ago. I fixed it by subtracting the camera position when moving the player.
So something like this:
1  
2  
3  
public void move(float x, float y){
      this.getPos().set((this.getPos().x + x) - camera.getPos.x, (this.getPos().y + y) - camera.getPos.y);
}

Why are all OpenGL tutorials written in Brainf**k?
Offline thunderfist123

Senior Newbie





« Reply #7 - Posted 2013-11-07 11:18:27 »

@Dxu1994 That still didn't work Sad Quite honestly, I think the camera moved slower! And the player wasn't centered in the window anymore, he was up and to the left! I should note that my origin is in the top left, no the bottom because I am using slick-util to render my text.

@Troubleshoots, I would do that, but my camera doesn't have a vector. I literally just use my player's vector for the camera.
Offline Dxu1994
« Reply #8 - Posted 2013-11-07 11:21:41 »

@Dxu1994 That still didn't work Sad Quite honestly, I think the camera moved slower! And the player wasn't centered in the window anymore, he was up and to the left! I should note that my origin is in the top left, no the bottom because I am using slick-util to render my text.

@Troubleshoots, I would do that, but my camera doesn't have a vector. I literally just use my player's vector for the camera.

I only just realised that haha.

Well if your origin is top left try this code:

1  
glTranslatef(-(p.getPos().x - Main.WIDTH/2 + 64/2), -(p.getPos().y - Main.HEIGHT/2 + 64/2), 0);

Offline thunderfist123

Senior Newbie





« Reply #9 - Posted 2013-11-07 11:25:14 »

Well, I think I should give up Sad Still not working... I have absolutely no idea what the problem could be! I mean, here's how I create the player:
1  
2  
3  
public Player() {
      super(new Vector2f(Main.WIDTH / 2 / 64, Main.HEIGHT / 2 / 64), Type.PLAYER);
   }

And then the update method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void update(){
      MOVESPEED += .0001f;
      if(Keyboard.isKeyDown(Keyboard.KEY_D)){
         this.move(MOVESPEED, 0);
      }
     
      if(Keyboard.isKeyDown(Keyboard.KEY_A)){
         this.move(-MOVESPEED, 0);
      }
   }

Its literally the most simple thing ever! Would the moveSpeed variable have anything to do with what's wrong?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dxu1994
« Reply #10 - Posted 2013-11-07 11:26:34 »

Why do you divide by 2 and then 64?

Offline thunderfist123

Senior Newbie





« Reply #11 - Posted 2013-11-07 11:27:35 »

Because that's translating it to the center of the screen, I believe. Dividing by two to get to the center of the screen, then dividing by 64 which is the tile size to get to the center of the map. Or something like that.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #12 - Posted 2013-11-07 11:28:57 »

Currently if you move the player 100px, you're moving the camera 50px.
1  
+ -p.getPos().x / 2

Thus why you need to update the player based on the camera position.

Edit:
Create a vector for the camera position, and do this:
1  
2  
3  
public void move(float x, float y){
      this.getPos().set(this.getPos().x + x, this.getPos().y + y).sub(camera.getPos);
}

Why are all OpenGL tutorials written in Brainf**k?
Offline Dxu1994
« Reply #13 - Posted 2013-11-07 11:29:07 »

Because that's translating it to the center of the screen, I believe. Dividing by two to get to the center of the screen, then dividing by 64 which is the tile size to get to the center of the map. Or something like that.

No, translating to the center of the screen would be:

x: ScreenWidth / 2 - TileSize / 2
y: ScreenHeight / 2 - TileSize / 2

Your code is translating somewhere near the top left corner.

Offline thunderfist123

Senior Newbie





« Reply #14 - Posted 2013-11-07 11:37:18 »

Currently if you move the player 100px, you're moving the camera 50px.
1  
+ -p.getPos().x / 2

Thus why you need to update the player based on the camera position.

Edit:
Create a vector for the camera position, and do this:
1  
2  
3  
public void move(float x, float y){
      this.getPos().set(this.getPos().x + x, this.getPos().y + y).sub(camera.getPos);
}

Ok, so how should I move the camera now? Should I not have the input change the players position? I did it like you said, and the player doesn't move anymore because I don't know what I should be putting in my translate method of my camera anymore.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #15 - Posted 2013-11-07 11:46:20 »

Currently if you move the player 100px, you're moving the camera 50px.
1  
+ -p.getPos().x / 2

Thus why you need to update the player based on the camera position.

Edit:
Create a vector for the camera position, and do this:
1  
2  
3  
public void move(float x, float y){
      this.getPos().set(this.getPos().x + x, this.getPos().y + y).sub(camera.getPos);
}

Ok, so how should I move the camera now? Should I not have the input change the players position? I did it like you said, and the player doesn't move anymore because I don't know what I should be putting in my translate method of my camera anymore.

Set its position based on the players position:
1  
glTranslateF((p.getPos().sub(GameContainer.getWidth/2, GameContainer.getHeight/2)), 0);

Why are all OpenGL tutorials written in Brainf**k?
Offline SHC
« Reply #16 - Posted 2013-11-07 11:46:26 »

I use this code and it works fine for me.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public void follow(GObject obj)
{
    int cam_x = 0;
    int cam_y = 0;

    if (Map.getWidth() > SCREEN_WIDTH)
    {
        cam_x = Math.round(SCREEN_WIDTH / 2 - obj.getX() - Map.TILE_WIDTH);
        cam_x = Math.min(cam_x, 0);
        cam_x = Math.max(cam_x, SCREEN_WIDTH - Map.getWidth());
    }
    if (Map.getHeight() > SCREEN_HEIGHT)
    {
        cam_y = Math.round(SCREEN_HEIGHT / 2 - obj.getY() - Map.TILE_HEIGHT);
        cam_y = Math.min(cam_y, 0);
        cam_y = Math.max(cam_y, SCREEN_HEIGHT - Map.getHeight());
    }

    glTranslatef(-cam_x, -cam_y);
}


Hope this helps.

Offline thunderfist123

Senior Newbie





« Reply #17 - Posted 2013-11-08 02:33:07 »

So I rewrote all my stuff, and I figured out most of the problem. Now, however, the player slowly slides out of the center of the screen. Here's my new camera class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
private Player p;
   private Vector2f pos;
   
   public Camera(Player p){
      this.p = p;
      this.pos = new Vector2f(0, 0);
   }
   
   public void update(){
      glTranslatef(-p.getPos().x + pos.x, -p.getPos().y + pos.y, 0);
      if(Keyboard.isKeyDown(Keyboard.KEY_D)){
         p.getPos().setX(p.getPos().x + 1);
         pos.setX(pos.x - 1 * 64);
      }
      if(Keyboard.isKeyDown(Keyboard.KEY_A)){
         p.getPos().setX(p.getPos().x - 1);
         pos.setX(pos.x + 1 * 64);
      }
   }

That's really the only relevant code, nothing else has changed. I'm so close, I just need to fix this last part! Any help?
Offline JVallius
« Reply #18 - Posted 2013-11-08 09:08:20 »

When you are stuck with this kind of problems I recommend to take a sheet of paper and pen and draw. It can really help when you visualize the problem instead just playing around with variables and finding a solution by guessing.

Also, do not be afraid to name your variables in their lenght (p -> player, pos -> position). It's easier for other people to study your code.

Offline SHC
« Reply #19 - Posted 2013-11-08 12:45:53 »

Try this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
public class Camera
{
    private Player player;

    public Camera(Player player)
    {
        this.player = player;
    }

    public void update()
    {
        int cam_x = 0;
        int cam_y = 0;

        if (Map.getWidth() > SCREEN_WIDTH)
        {
            cam_x = Math.round(SCREEN_WIDTH / 2 - player.getX() - player.getWidth());
            cam_x = Math.min(cam_x, 0);
            cam_x = Math.max(cam_x, SCREEN_WIDTH - Map.getWidth());
        }
        if (Map.getHeight() > SCREEN_HEIGHT)
        {
            cam_y = Math.round(SCREEN_HEIGHT / 2 - player.getY() - player.getHeight());
            cam_y = Math.min(cam_y, 0);
            cam_y = Math.max(cam_y, SCREEN_HEIGHT - Map.getHeight());
        }

        glTranslatef(-cam_x, -cam_y);
    }
}

This way, the camera always centers the player, there are no slowness etc.,

Hope this helps.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (58 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!