Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (756) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Player moving faster than game scrolls  (Read 2815 times) 0 Members and 1 Guest are viewing this topic.
Troubleshoots

JGO Knight

Medals: 36
Exp: 7-9 months

Damn maths.

 « Posted 2013-10-20 17:54:56 »

I've been confused as to why my player moves faster than the map scrolls. This is my algorithm.

 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 `public void drawMap(Graphics2D g2d) {      int minOffsetX = 0, minOffsetY = 0;      int maxOffsetX = (mapX - Frame.CANVAS_WIDTH);      int maxOffsetY = (mapY - Frame.CANVAS_HEIGHT);      int cameraX = (int) (Player.getX() - (Frame.CANVAS_WIDTH / 2));      int cameraY = (int) (Player.getY() - (Frame.CANVAS_HEIGHT / 2));      if(cameraX < minOffsetX)         cameraX = minOffsetX;      if(cameraY < minOffsetY)         cameraY = minOffsetY;      if(cameraX > maxOffsetX)         cameraX = maxOffsetX;      if(cameraY > maxOffsetY)         cameraY = maxOffsetY;            int tileX = cameraX, tileY = cameraY;      while(tileX % TILE_SIZE != 0) {         tileX--;      }      while(tileY % TILE_SIZE != 0) {         tileY--;      }            for (int y = tileY; y < cameraY + Frame.CANVAS_HEIGHT; y += TILE_SIZE) {         for (int x = tileX; x < cameraX + Frame.CANVAS_WIDTH; x += TILE_SIZE) {            g2d.drawImage(map[y / TILE_SIZE][x / TILE_SIZE], x - cameraX, y - cameraY, null);         }      }   }`

It's pretty much designed to only paint the area of the map being viewed, however for some reason, when I move my player to the right, the player will move at around 1.2x the speed that the map scrolls, and when I move my player to the left, it'll go back towards the center of the screen.

Can anyone spot the issue? Thanks.

Why are all OpenGL tutorials written in Brainf**k?
jeb_

Junior Devvie

Medals: 7

 « Reply #1 - Posted 2013-10-21 07:25:30 »

How do you calculate where to render the player?
SHC
 « Reply #2 - Posted 2013-10-21 12:33:17 »

Are you using delta time to move the player? I think that might be the issue.

Opiop
 « Reply #3 - Posted 2013-10-21 20:05:42 »

Delta time just smooths the animation between frames, it shouldn't affect scrolling like that.
@OP Your player rendering code is needed to even begin to see how to fix this.
Troubleshoots

JGO Knight

Medals: 36
Exp: 7-9 months

Damn maths.

 « Reply #4 - Posted 2013-10-21 20:29:22 »

This is my algorithm updating the player position. It's pretty simple. So yeah, there isn't a problem with my player movement.

 1  2  3  4  5  6 `public void move(float deltaTime) {   if(left && !right)      xPos -= 100*(deltaTime/1000);   if(right && !left)      xPos += 100*(deltaTime/1000);}`

In the game loop (if you must see this):

 1  2  3 `long currentTime = System.nanoTime();deltaTime = (currentTime - previousTick) / 1000000F;previousTick = currentTime;`

Why are all OpenGL tutorials written in Brainf**k?
jeb_

Junior Devvie

Medals: 7

 « Reply #5 - Posted 2013-10-22 07:45:53 »

I repeat my question, how do you calculate where to render the player?
Troubleshoots

JGO Knight

Medals: 36
Exp: 7-9 months

Damn maths.

 « Reply #6 - Posted 2013-10-22 09:27:27 »

Oh stupid me I misread the question. Here is my code for rendering the player.

 1  2  3  4  5  6 `         p.animate(System.nanoTime());         player = p.getSprite();         AffineTransform t = new AffineTransform();         t.translate(Player.getX(), Player.getY());         g2d.drawImage(player, t, null);`

Why are all OpenGL tutorials written in Brainf**k?
jeb_

Junior Devvie

Medals: 7

 « Reply #7 - Posted 2013-10-22 09:39:47 »

I might be missing something, but shouldn't you compensate for the camera position when you render the player too?

E.g,

 1 `t.translate(Player.getX() - cameraX, Player.getY() - cameraY);`
Troubleshoots

JGO Knight

Medals: 36
Exp: 7-9 months

Damn maths.

 « Reply #8 - Posted 2013-10-22 10:10:24 »

I might be missing something, but shouldn't you compensate for the camera position when you render the player too?

E.g,

 1 `t.translate(Player.getX() - cameraX, Player.getY() - cameraY);`

Thanks it worked after a little playing around, but could you explain why I had to do that? I thought that drawing the camera like this:

 1 `      cameraX = (int) (Player.getX() - (Frame.CANVAS_WIDTH / 2));`

would draw the camera like so

Why would the player have to take into account the cameras position if the camera already does so for the player?

Why are all OpenGL tutorials written in Brainf**k?
jeb_

Junior Devvie

Medals: 7

 « Reply #9 - Posted 2013-10-22 11:24:21 »

I'll try to explain...

Basically, what you want to do is to render the whole game relative to the screen's coordinates, and those coordinates never change. The upper left corner is always [0, 0]. That means that if you want to render something from the camera's perspective, you will have to recalculate the position from its world coordinates to the screen's coordinate system.

This code,

 1 `cameraX = (int) (Player.getX() - (Frame.CANVAS_WIDTH / 2));`

doesn't move any coordinates. All it does is to calculate an integer, which tells you where the camera is located in the world. Lets say it's calculated to be "100". Since you want this to be the upper left corner on screen, everything has to be moved relative to the camera.

For example, if your player is located at "250", it should be rendered at screen coordinate "150".

Another way to think about it is, what if the player is standing still (for example, at world coordinates [500, 500]), and instead of moving the player you are moving the camera? How would you render the tiles and the player?

Hope this helps.
Troubleshoots

JGO Knight

Medals: 36
Exp: 7-9 months

Damn maths.

 « Reply #10 - Posted 2013-10-22 11:38:41 »

I see. Thanks for the very clear and easy to understand definition.

Why are all OpenGL tutorials written in Brainf**k?
Pages: [1]
 ignore  |  Print

 DesertCoockie (54 views) 2018-05-13 18:23:11 nelsongames (86 views) 2018-04-24 18:15:36 nelsongames (76 views) 2018-04-24 18:14:32 ivj94 (761 views) 2018-03-24 14:47:39 ivj94 (93 views) 2018-03-24 14:46:31 ivj94 (645 views) 2018-03-24 14:43:53 Solater (106 views) 2018-03-17 05:04:08 nelsongames (187 views) 2018-03-05 17:56:34 Gornova (428 views) 2018-03-02 22:15:33 buddyBro (1088 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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