Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (768)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
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  
  Collision works one way, but not the other  (Read 170 times)
0 Members and 1 Guest are viewing this topic.
Offline kiefac

Junior Newbie





« Posted 2018-09-12 23:17:11 »

Collision code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public void handleCollisions(Game game) {
   if (!blockId.equals("air")) {
      if (game.player.getBounds().intersects(getBounds())) {
         if (game.player.playerY + Player.PLAYERHEIGHT > blockY) {
            if (game.player.playerY + Player.PLAYERHEIGHT < blockY + (BLOCKSIZE / 2)) {
               game.player.playerY = blockY - Player.PLAYERHEIGHT;
               game.player.playerYV = 0;
               game.player.jump = true;
            } else {
               if (game.player.playerX < blockX + paintWidth) {
                  game.player.playerX = blockX + paintWidth;
               }
               if (game.player.playerX > blockX - Player.PLAYERWIDTH) {
                  game.player.playerX = blockX - Player.PLAYERWIDTH;
               }
            }
         }
      }
   }
}


Mainly looking at:
1  
2  
3  
4  
5  
6  
if (game.player.playerX < blockX + paintWidth) {
   game.player.playerX = blockX + paintWidth;
}
if (game.player.playerX > blockX - Player.PLAYERWIDTH) {
   game.player.playerX = blockX - Player.PLAYERWIDTH;
}

paintWidth is the width of the block.
When the player walks right into the left side of a block, it works fine; the player stops in front of it. However, when the player walks left into the right side of a block, it teleports the player to the left side of the block. But to add even more to the confusion, if I put an else in front of
if (game.player.playerX > blockX - Player.PLAYERWIDTH)
the teleporting side will switch to the other side. (walking left into a block is fine, walking right will teleport)
My math seems to be fine, it looks like it's a quirk in Java...? Looking for some guidance on this. I can post more of my code if necessary, but I have 9 classes that all link into each other and I'd prefer not to post it all if possible.
(inb4 "use a game engine n00b")
Offline CoDi^R
« Reply #1 - Posted 2018-09-12 23:58:23 »

Your issue here is that this check is not only true for intersecting the block from the right, but also for every other playerX smaller than (blockX + paintWidth):

1  
2  
3  
if (game.player.playerX < blockX + paintWidth) {
   game.player.playerX = blockX + paintWidth;
}



You probably want to do something along this:

1  
2  
3  
if ((game.player.playerX > blockX) && (game.player.playerX < blockX + paintWidth)) {
   game.player.playerX = blockX + paintWidth;
}


... and

1  
2  
3  
if ((game.player.playerX + Player.PLAYERWIDTH > blockX) && (game.player.playerX + Player.PLAYERWIDTH < blockX + paintWidth)) {
   game.player.playerX = blockX - Player.PLAYERWIDTH;
}


Robotality - steamworks4j - @code_disaster - codi^r @ #java-gaming
Offline kiefac

Junior Newbie





« Reply #2 - Posted 2018-09-13 00:57:04 »

Thank you! Works perfectly.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1337 views)
2018-06-10 19:43:48

EgonOlsen (1214 views)
2018-06-10 19:43:44

EgonOlsen (939 views)
2018-06-10 19:43:20

DesertCoockie (1380 views)
2018-05-13 18:23:11

nelsongames (1112 views)
2018-04-24 18:15:36

nelsongames (1361 views)
2018-04-24 18:14:32

ivj94 (2095 views)
2018-03-24 14:47:39

ivj94 (1253 views)
2018-03-24 14:46:31

ivj94 (2259 views)
2018-03-24 14:43:53

Solater (806 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!