Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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 wall collision sliding problem [Need Help]  (Read 756 times)
0 Members and 1 Guest are viewing this topic.
Offline GlennBrann
« Posted 2013-05-03 00:13:16 »

Hey Everyone,

I'm having problems with my collisions. Basically when i colliding against a wall i cant slide if i press another key..For example. if i press The left arrow key and i am on the wall, if i continue to hold this key and try to press the up arrow my player wont move upwards. I hope you know what i mean. I know its probably a really easy fix but i need help.

Here is my player update method:
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  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
   public void update() {

      movePlayer();
      // Check Collision;

      int curCol = tileMap.getColTile((int) x);
      int curRow = tileMap.getRowTile((int) y);

      double xDest = x += dx;
      double yDest = y += dy;

      double tempX = x;
      double tempY = y;

      collision(x, yDest);
      if (dy < 0) {
         if (topLeft || topRight) {
            dy = 0;
            tempY = curRow * tileMap.getTileSize() + height / 2;
         } else {
            tempY += dy;
         }
      }
      if (dy > 0) {
         if (bottomLeft || bottomRight) {
            dy = 0;
            tempY = (curRow + 1) * tileMap.getTileSize() - height / 2;
         } else {
            tempY += dy;
         }
      }
      dy = 0;
      collision(xDest, y);
      if (dx < 0) {
         if (topLeft || bottomLeft) {
            dx = 0;
            tempX = curCol * tileMap.getTileSize() + width / 2;
         } else {
            tempX += dx;
         }
      }
      if (dx > 0) {
         if (topRight || bottomRight) {
            dx = 0;
            tempX = (curCol + 1) * tileMap.getTileSize() - width / 2;
         } else {
            tempX += dx;
         }
      }
      dx = 0;

      x = tempX;
      y = tempY;

      tileMap.setX((int) (Game.WIDTH * Game.SCALE / 2 - x));
      tileMap.setY((int) (Game.HEIGHT * Game.SCALE / 2 - y));
   }

   public void movePlayer() {
      if (left) {
         dx -= moveSpeed;
      }
      if (right) {
         dx += moveSpeed;
      }
      if (up) {
         dy -= moveSpeed;
      }
      if (down) {
         dy += moveSpeed;
      }
   }


I might have an idea on how to do it. i think you could use something like

1  
2  
3  
if(dx != 0 && dy != 0){
//Something in here
           }


Please any help is much appreciated.
Thanks
- GlennBrann
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #1 - Posted 2013-05-03 14:24:31 »

I'm not sure I understand completely because some of the code isn't there (collision() for example).

However, I think you are doing things a little backwards, you're moving then verifying that move. An easier way is to verify the move and only move if it is a legal move.

If you can give more information I can help further Smiley.
Offline Axeman

Senior Member


Medals: 7



« Reply #2 - Posted 2013-05-03 14:47:20 »

I belive that this is a common problem. First you check if there´s a collision in the y-axis and stops velocity if intersection returns true, then you do the same in the x-axis. The problem is that when intersection is true in the x-axis, it will also be true in the y-axis since for a collision to be true you have to be "in" a collision. H3ckboys solution is one, another one is the one I posted in your last topic about collisions here http://www.java-gaming.org/topics/corner-collision-detection-need-help/29399/msg/269818/view.html#msg269818: Check for intersection. If it returns true you check how much the objects overlap and then push back one object the amout of overlap. This way you´ll always be just outside of collision.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GlennBrann
« Reply #3 - Posted 2013-05-03 18:48:45 »

Hey sorry for not being clear enough. Basically if i try to slid along a wall i just stop moving. i cant slide. here is my collision code :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   public void collision(double x, double y) {
      int leftTile = tileMap.getColTile((int) (x - width / 2));
      int rightTile = tileMap.getColTile((int) (x + width / 2) - 1);
      int topTile = tileMap.getRowTile((int) (y - height / 2));
      int bottomTile = tileMap.getRowTile((int) (y + height / 2) - 1);
      topLeft = tileMap.getTile(topTile, leftTile) == 1;
      topRight = tileMap.getTile(topTile, rightTile) == 1;
      bottomLeft = tileMap.getTile(bottomTile, leftTile) == 1;
      bottomRight = tileMap.getTile(bottomTile, rightTile) == 1;
     
   }
Offline GlennBrann
« Reply #4 - Posted 2013-05-03 19:02:15 »

also, could you maybe show me the best way in which to move the player. what should i change ?
Offline Axeman

Senior Member


Medals: 7



« Reply #5 - Posted 2013-05-03 20:10:42 »

Did you read my reply?
Offline GlennBrann
« Reply #6 - Posted 2013-05-03 20:17:03 »

Yes i have read your post i meant to say thank you. Ill need to try that out i just didn't see it in time. Sorry about that. ill try it now Smiley
-GLenn
Offline gouessej
« Reply #7 - Posted 2013-05-03 22:19:45 »

Hi

I already solved a similar problem in TUER. Let me know if you still need some help.

Offline GlennBrann
« Reply #8 - Posted 2013-05-04 00:31:52 »

Yeah, id love some help please. every little helps. Thanks Smiley
Offline gouessej
« Reply #9 - Posted 2013-05-07 16:50:14 »

Ok. Actually, I would have loved to show you the source code but some people will say that this is a GPL trap and if someone reuses a non trivial part of my code, I will be the bad guy reminding that derivatives must be under GPL too... I can still explain to you what I did:
- I compute the theorical distance between the previous position and the new position (like there was no collision)
- I compute a step count which depends on the size of the bounding volume and this distance
- I compute a separate step count per dimension (x, z)
- I loop while no collision is found and while my counter is less than the step count
     - I compute the temporary position of the player at this step
     - I look at several candidates around this position by considering the player can modify x and z
     - if there is a collision, I try to do the same but by considering the player can't modify x
     - if there is a collision, I try to do the same but by considering the player can't modify z


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online SHC
« Reply #10 - Posted 2013-05-07 17:16:51 »

Quote
I compute a separate step count per dimension (x, z)

Where did z axis come in? I thought this is a 2d game

Offline gouessej
« Reply #11 - Posted 2013-05-07 20:03:08 »

Quote
I compute a separate step count per dimension (x, z)

Where did z axis come in? I thought this is a 2d game
My game is in 3D but as I can't jump, the collisions are handled in 2D.

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.

CogWheelz (15 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!