• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
bananaoomarang

Good way to differentiate between horizontal/vertical collisions?

2 posts in this topic

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:

[code]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;
}
}
[/code]

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

[code]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[i])) {
break;
} else {
continue;
}
}
player.update(dt);
}
lastUpdate = startTime;
}[/code]


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: [url="https://github.com/Awevation/Falling"]https://github.com/Awevation/Falling[/url]
0

Share this post


Link to post
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.
0

Share this post


Link to post
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:

[code]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;
}
}
[/code]

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0