Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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 933 times)
0 Members and 1 Guest are viewing this topic.
Offline 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?
Offline jeb_

Junior Devvie


Medals: 7



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

How do you calculate where to render the player?
Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 161
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.

Offline 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?
Offline 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?
Offline 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?
Offline 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);
Offline 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?
Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline 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. Smiley

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.

 

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

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (16 views)
2014-12-28 02:10:29

BurntPizza (17 views)
2014-12-27 22:38:51

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!