Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (764)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
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  
  Horizontal collisions  (Read 1231 times)
0 Members and 1 Guest are viewing this topic.
Offline Luminem

Senior Newbie





« Posted 2013-06-09 20:35:58 »

Hello guys I don't know how to check horizontal collisions on a tile map Cry I can check vertical collisions and they works perfectly but I don't know how to check horizontal ways. My problem is if the player is near the "tile" horizontal collision works but the player is teleported to the top of the tile. :S It's a platform game and I hope you understand what I mean

Simply Perfect
Offline actual

JGO Coder


Medals: 25



« Reply #1 - Posted 2013-06-09 22:41:21 »

Sounds like the problem is in the code that gets run when a collision is detected. What is the behavior you would like to have? I assume you want the player to stop horizontal motion or bounce backwards. Post the code that is run when a collision is triggered and it will make it easier to see what the problem is.

EDIT: Another question. Does your game code know when a horizontal collision has occurred vs. a vertical one?
Offline relminator
« Reply #2 - Posted 2013-06-10 01:57:41 »

This might help:
www.java-gaming.org/topics/basic-2d-tile-collision/29657/view.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Luminem

Senior Newbie





« Reply #3 - Posted 2013-06-10 13:22:13 »

Sorry for my explanation. Smiley Really what I want to do is "separating both collisions". I wanna say, don't mix the vertical collision with the horizontal collision. Actually they're mixed and it's very strange because every time I'm near a wall the code says "eh you're together a block then you must be on the top of this block". I don't want that the player go to the top of the tile when the player is together a wall :S
I hope you understand better this explanation.
My collision code:
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  
public class Collision {
   
   public Collision () {
     
   }
   
   public void Update(Camera camera, Player player, Level level) {
      for (int i = 0; i < level.getMap().length; i++) {
         for (int j = 0; j < level.getMap()[i].length; j++) {
            if (level.getMap()[i][j] != 0)
               Check(i, j, camera, player, level);
         }
      }
   }
   
   private void Check(int i, int j, Camera camera, Player player, Level level) {
      if (player.getPosition().getX() - camera.getX() > i * level.getBlockSize() + level.getBlockSize() - camera.getX() ||
            player.getPosition().getY() - camera.getY() > j * level.getBlockSize() + level.getBlockSize() - camera.getY() ||
            player.getPosition().getX() - camera.getX() + player.getPosition().getWidth() * player.getScale() < i * level.getBlockSize() - camera.getX() ||
            player.getPosition().getY() - camera.getY() + player.getPosition().getHeight() * player.getScale() < j * level.getBlockSize() - camera.getY()) {
      } else {
         player.onFloor(j * level.getBlockSize(), camera);
      }
   }
}


OnFloor(player functon)
1  
2  
3  
4  
5  
6  
public void onFloor(float j, Camera camera) {
      jump = ((position.getY() + position.getHeight() * scale) - camera.getY() >= j - camera.getY());
      if (jump) {
         position.setY((j-camera.getY())  - (position.getHeight() * scale - camera.getY()));
      }
   }


Thanks you very much @relminator for the link but I saw your post before I asked this and I didn't understand it so well :S Anyway thanks you Cheesy

Simply Perfect
Offline actual

JGO Coder


Medals: 25



« Reply #4 - Posted 2013-06-10 14:11:56 »


From this StackExchange answer simply check collisions one axis at a time.

In your case, it may make sense to first check for a vertical collision then a horizontal.
Offline Luminem

Senior Newbie





« Reply #5 - Posted 2013-06-10 16:50:53 »

Thanks you very much Smiley It's solved

Simply Perfect
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

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

Solater (568 views)
2018-03-17 05:04:08
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

Deployment and Packaging
by philfrei
2018-08-19 23:53:08

Deployment and Packaging
by philfrei
2018-08-19 23:50:04

Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39
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!