Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (576) Games in Android Showcase (154) games submitted by our members Games in WIP (626) 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
 Player moving faster than game scrolls  (Read 1099 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.

 Games published by our own members! Check 'em out!
opiop65

JGO Kernel

Medals: 166
Projects: 7
Exp: 4 years

JumpButton Studios

 « 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.
 Games published by our own members! Check 'em out!
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

You cannot reply to this message, because it is very, very old.

 ClaasJG (40 views) 2015-04-30 20:33:25 Riven (24 views) 2015-04-30 13:25:12 ClaasJG (45 views) 2015-04-27 13:36:51 BurntPizza (51 views) 2015-04-23 03:42:11 theagentd (52 views) 2015-04-22 16:23:07 Riven (65 views) 2015-04-16 10:48:47 Duke0200 (76 views) 2015-04-16 01:59:01 Fairy Tailz (58 views) 2015-04-14 20:13:12 Riven (63 views) 2015-04-12 21:36:37 bus hotdog (88 views) 2015-04-10 02:39:32
 theagentd 24x BurntPizza 19x Spasi 16x kingroka123 11x wessles 11x alwex 11x Hanksha 9x Ecumene 8x Olo 8x chrislo27 7x ra4king 7x princec 6x Riven 6x kevglass 6x trollwarrior1 5x EgonOlsen 5x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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