Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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  
  Wall Collisions  (Read 2944 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21
« Posted 2012-05-27 15:29:38 »

I'm having problems with code that I've come up with to detect if the player hits a wall. Currently, I've got it set that if the wall and the player collide, it sets the horizontal movement to 0. But then, when i go to move away from the wall, I can't because I'm still colliding with the wall and the horizontal movement gets set to 0. What do you guys do for wall collisions?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Ciaran54

Junior Devvie


Medals: 4



« Reply #1 - Posted 2012-05-27 15:52:58 »

I tend to calculate how far into the wall the player hitbox is on the x axis, then displace it by the negative of that amount instantly, so it never appears inside the wall.

I then repeat this for the y axis, after the x position has been corrected.

I find that this gives the cleanest collisions, that stay working regardless of the framerate in a variable timestep environment. (so long as the framerate isn't so low that the player can skip right across it)
Offline 65K
« Reply #2 - Posted 2012-05-27 17:40:24 »

- The next sprite position gets calculated but not yet applied.
- The calculated position is handed over to the almighty collision master.
- In case of a free way, the sprite is moved.
- In case of collision, movement is cleared, no need to correct any position here.

Collision check points depends on the current heading, there are eight points overall.

If the position delta is too large, the distance is broken into multiple collision checks to prevent wall tunneling.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline krasse
« Reply #3 - Posted 2012-05-29 14:59:21 »

Additional hints to 65K's:
* Test collision in both x-y first and if it is OK to move, go ahead
* If not, test collision in the x-direction only (and then y-direction only if it fails). With this extra test, you don't get the problem that you get stuck if you move diagonally and collide.

Offline Ciaran54

Junior Devvie


Medals: 4



« Reply #4 - Posted 2012-05-29 15:26:06 »

- The next sprite position gets calculated but not yet applied.
- The calculated position is handed over to the almighty collision master.
- In case of a free way, the sprite is moved.
- In case of collision, movement is cleared, no need to correct any position here.

Collision check points depends on the current heading, there are eight points overall.

If the position delta is too large, the distance is broken into multiple collision checks to prevent wall tunneling.

Surely this would cause the player to appear to collide with an invisible shield surrounding the wall, because the player would never be able to get close enough to touch it. While at high framerates it probably not be noticeable, at lower framerates or higher speeds, this would look a bit odd to the player...
Offline minosu

Junior Newbie





« Reply #5 - Posted 2012-05-30 04:32:37 »

I spent weeks making a collision/physics system (though it's mostly collision)
if player is moving right
- check if player's destination collides with anything
-- if true, snap player just short of the collision
- if false
-- just move the player

etc

This method won't work if you move faster than the width of the collidable object (that's a whole other problem...)
Offline philfrei
« Reply #6 - Posted 2012-05-30 05:23:00 »

I like Ciaran54's solution the best so far. Minosa's has the problem that if the object is very close to begin with and is moving very fast, it will seem to have suddenly (momentarily) lost its velocity as the move gets cut short at the wall rather than rebounding.

I have to confess, I've tended to use 65k's method (easier to code)--but I haven't done anything particularly critical.

"It's after the end of the world! Don't you know that yet?"
Offline Oskuro

JGO Knight


Medals: 40
Exp: 6 years


Coding in Style


« Reply #7 - Posted 2012-07-27 00:07:46 »

An idea I want to try for 2d collision detection is to "draw" object movements like brush strokes (The canvas being a byte buffer, and the "color" the object's id), then "cut" out the walls from the canvas and see where the stroke ends, or where it crosses other strokes.

It's a visually interesting idea that might end up being a resource hog (specially when dealing with several moving objects colliding with each other), though, but at least it should help with static wall collisions.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #8 - Posted 2012-07-27 00:35:17 »

I just make a detection box around the player (just a box around the player that's a bit wider and taller on all 4 sides) -
when the detection box collides into the wall you stop - so there's ~1 pixel space (hacky solution, you probably shouldn't use this)
1 pixel is unnoticeable, anyways...
Offline Oskuro

JGO Knight


Medals: 40
Exp: 6 years


Coding in Style


« Reply #9 - Posted 2012-07-27 16:19:06 »

Regarding your sticky wall issue, you need to find out the wall's "normal", that is, the vector it uses to push back against the player, and use it to only nullify the movement components that push it, not the rest.

As a conceptual example, in an axis-aligned environment (All walls are 90ยบ from each other, as if drawn on an square grid) where +X is right and +Y is up, a right facing wall would nullify -X movement, but not the rest.

In the following off the top of my head code, colliding with the wall sets the appropriate isColliding* flag to true based on the orientation of the wall, so when the player attempts to move:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
void movePlayer(int newXvelocity, int newYvelocity)
{
    if( (newXvelocity > 0 && isCollidingRight())  ||  (newXvelocity < 0 && isCollidingLeft())  )
    {
        playerXvelocity = 0;

    } else
    {
        playerXvelocity = newXvelocity;
    }

    if( (newYvelocity > 0 && isCollidingUp())  ||  (newYvelocity < 0 && isCollidingDown())  )
    {
        playerYvelocity = 0;

    } else
    {
        playerYvelocity = newYvelocity;
    }
}



You should really take a look at vector arithmetic, though, specially if you don't want axis-aligned walls.

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.

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

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

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

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

JscottyBieshaar (36 views)
2014-12-05 12:39:02

SHC (49 views)
2014-12-03 16:27:13

CopyableCougar4 (44 views)
2014-11-29 21:32:03

toopeicgaming1999 (112 views)
2014-11-26 15:22:04

toopeicgaming1999 (98 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
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

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!