# Good way to differentiate between horizontal/vertical collisions?

This topic is 2438 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I'm having trouble getting collision detection into my 2D WebGL game. I'd appreciate any help! I'm probably doing it badly, as I'm still learning! Here's the code for my player class:

Player.prototype = new Quad(); Player.prototype.constructor = Player; function Player(width, height, xPos, yPos) { this.width = width; this.height = height; this.xPos = xPos; this.yPos = yPos; this.xVel = 0; this.yVel = 0; this.falling = true; this.onCloud = false; this.horCol = false; this.update = function(delta) { this.xPos += (this.xVel * delta) / 1000.0; this.yPos += (this.yVel * delta) / 1000.0; if (keydown.left && !this.onCloud) { this.xVel += -2; } else if (keydown.right && !this.onCloud) { this.xVel += 2; } else if (keydown.left && this.onCloud) { this.xVel += -6; } else if (keydown.right && this.onCloud) { this.xVel += 6; } if (this.onCloud) { this.yPos -= (this.yVel * delta) / 1000.0; this.yVel = 0; } else if (this.horCol) { this.xPos -= (this.xVel * delta) / 1000.0; this.xVel = 0; this.horCol = false; } else { this.yVel += -2; } console.log("isOnCloud: " + this.onCloud + "\nIsHorColling: " + this.horCol); } this.checkCollision = function(cloud) { if (this.collides(this, cloud)) { this.onCloud = true; return true; } else { this.onCloud = false; return false; } } this.collides = function(a, b) { return a.xPos < b.xPos + b.width && a.xPos + a.width > b.xPos && a.yPos < b.yPos + b.height && a.yPos + a.height > b.yPos; } } 

And here's the updateWorld() function that is run in the main loop:

function updateScene() { var startTime = (new Date).getTime(); if(lastUpdate) { var dt = startTime - lastUpdate; sky.update(dt); for(var i = 0.0; i < sky.clouds.length; i++) { if(player.checkCollision(sky.clouds)) { break; } else { continue; } } player.update(dt); } lastUpdate = startTime; }

The trouble is, I can't figure out a reliable way to differentiate horzontal/vertical collisions. The guy is falling, but he can land on the clouds, but when he hits the side of one I don't want it to be counted as on the cloud! Currently he just slides through, whereas he should be stopped and continue falling. Am I thinking about this the wrong way?

Thanks

EDIT: Here's a git repo I threw up in case you need/want to know anything else: https://github.com/Awevation/Falling

##### Share on other sites
[font=courier new,courier,monospace]if (this.collides(this, cloud)) {
this.onCloud = true;
...
}[/font]

Clearly oversimplified: you should check the position relative to the cloud rectangle.
If the character is above the top and not far enough to the left or right to lose balance and fall down, he is standing on the cloud; if coming from the left or right or bottom, he is bouncing.

In the frames in which overlaps begin you also need collision response: presumably, position should be pushed upwards to a fixed offset above the top of the cloud and vertical velocity reduced to 0 to make characters stand on the cloud, or velocity should be reversed and possibly reduced to make characters bounce.

##### Share on other sites
Thanks, I've revamped the whole system (so now I have actual bounding boxes) and it works a little better, but my system of determining exactly what type of collision is still failing. Here's the new Player class:

Player.prototype = new Quad(); Player.prototype.constructor = Player; function Player(width, height, xPos, yPos) { this.width = width; this.height = height; this.xPos = xPos; this.yPos = yPos; this.xVel = 0; this.yVel = 0; this.falling = true; this.onCloud = false; this.horCol = false; this.verCol = false; this.cloudOn; this.update = function(delta) { this.xPos += (this.xVel * delta) / 1000.0; this.yPos += (this.yVel * delta) / 1000.0; this.alignBBoxes(); if(this.verCol) { this.yPos -= (this.yVel * delta) / 1000.0; this.verCol = false; this.onCloud = true; } else if (this.horCol) { this.xPos -= (this.xVel * delta) / 1000.0; this.yVel += -2; } if(!this.onCloud) { this.yVel += -2; } if (keydown.left &amp;&amp; !this.onCloud) { this.xVel += -2; } else if (keydown.right &amp;&amp; !this.onCloud) { this.xVel += 2; } else if (keydown.left &amp;&amp; this.onCloud) { this.xVel += -6; } else if (keydown.right &amp;&amp; this.onCloud) { this.xVel += 6; } else if (keydown.up &amp;&amp; this.onCloud) { this.yVel += 10; } } this.handleCollision = function(a, b) { var xOverlap; var yOverlap; if(a.xPos + a.width < b.xPos + b.width &amp;&amp; a.yPos > b.yPos) { xOverlap = a.xPos + a.width - b.xPos; yOverlap = b.yPos + b.height - a.yPos; } else if (a.xPos + a.width < b.xPos + b.width &amp;&amp; a.yPos < b.yPos) { xOverlap = a.xPos + a.width - b.xPos; yOverlap = a.yPos + a.height - b.yPos; } else if (a.xPos + a.width > b.xPos + b.width &amp;&amp; a.yPos > b.yPos) { xOverlap = b.xPos + b.width - a.xPos; yOverlap = b.yPos + b.height - a.yPos; } else if (a.xPos + a.width > b.xPos + b.width &amp;&amp; a.yPos < b.yPos) { xOverlap = b.xPos + b.width - a.xPos; yOverlap = a.yPos + a.height - b.yPos; } else { //Christ knows what's happening. Bail. Bail. Bail. console.log("ERROR"); return 0; } if(xOverlap > yOverlap) { this.verCol = true; this.cloudOn = b; } else { this.horCol = true; } } this.collides = function(a, b) { return a.xPos < b.xPos + b.width &amp;&amp; a.xPos + a.width > b.xPos &amp;&amp; a.yPos < b.yPos + b.height &amp;&amp; a.yPos + a.height > b.yPos; } this.isOnCloud = function(cloud) { return this.xPos < cloud.xPos + cloud.width &amp;&amp; this.xPos + this.width > cloud.xPos &amp;&amp; this.yPos < cloud.yPos + cloud.height &amp;&amp; this.yPos + this.height > cloud.yPos; } } 

It's not really working though! I'm not sure how to check if the guy's left the cloud or not, that last function there doesn't appear to work.... Also, when he does fall off he sort of slides down the edge then slides underneath, which seems to be counted as a vertical collision in my system. What's the best way to tell what type of collision it is and react to that?

EDIT: OK, fixed some major problems, I wasn't ever setting horCol to false which was problematic.... Also was never resetting velocities. Rather problematic.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633721
• Total Posts
3013537
×