• 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
Oyed

Collision Detection Issue

8 posts in this topic

Hello there,

 

I'm developing a HTML5 Canvas Game and am having issues with the method I've taken for collision detection. I can find and retrieve a Tile Object in my Game based on an X and Y position, which led me to believe generating "points" on Entity Sprites and checking each of them for a collision was the best way to go about it. I've had a lot of issues with this and don't really know what else to do.

 

The points generated on my character look like this when visualised:

 

Hl6z6.png

 

Each side of the sprite has points generated for it specifically so I can tell which direction the solid tile is. In the update loop the tiles under each point are fetched and checked for if they are solid, and if they are, it prevents movement in that direction. The problem I'm facing is that when walking left + up in to a wall to the left of the player, the top and bottom collision points are also being triggered due to them being relative to the players position and having the X velocity applied to them before checking.

 

I can't figure out a way to solve this; below is the code I'm using to generate the points, check them and also correct the X & Y positions of the player.

 

Generating Points function:

generatePoints: function(config) {
	var points = [],
		scale = Engine.config('GLOBAL_GRAPHICS_SCALE'),
		halfTile = (config.tileWidth / 2) * scale,
		tilesWidth = Math.ceil(config.spritesheet.getWidth() / (config.tileWidth / 2)),
		tilesHeight = Math.ceil(config.spritesheet.getHeight() / (config.tileWidth / 2)),
		x = config.x, y = config.y, yOffset = 0, xOffset = 0,
		directions = config.directions,
		xVelocity = config.xVelocity, yVelocity = config.yVelocity;

	for(var h = 0; h <= tilesHeight; h++) {
		yOffset = ((h == 0) ? 1 : ((h >= tilesHeight) ? -1 : 0));
		/* Left Side */
		if(directions.left) points.push([ (x + xVelocity), (y + yVelocity) + (h * halfTile) + yOffset, 0 ]);
		/* Right Side */
		if(directions.right) points.push([ (x + xVelocity) + (tilesWidth * halfTile), (y + yVelocity) + (h * halfTile) + yOffset, 1 ]);
	}
	for(var w = 0; w <= tilesWidth; w++) {
		xOffset = ((w == 0) ? 1 : ((w >= tilesWidth) ? -1 : 0));
		/* Top */
		if(directions.up) points.push([ (x + xVelocity) + (w * halfTile) + xOffset, (y + yVelocity), 2 ]);
		/* Bottom */
		if(directions.down) points.push([ (x + xVelocity) + (w * halfTile) + xOffset, (y + yVelocity) + (tilesHeight * halfTile), 3 ]);
	}

	this._points = this._points.concat(points);

	return points;
},

Looping through and checking the points function:

checkPointCollision: function(points) {
	var tile = null, dir = null, solid = false,
		tempX = 0, tempY = 0,
		tileWidth = Engine.manager('map').getActive().getTileWidth(),
		collision = {
			left:	{ move: true, x: 0, y: 0 },
			right:	{ move: true, x: 0, y: 0 },
			up:		{ move: true, x: 0, y: 0 },
			down:	{ move: true, x: 0, y: 0 }
		};

	for(var p = 0; p < points.length; p++) { // For all the generated checking points...
		solid = false;
		dir = points[p][2];
		tile = Engine.manager('map').getActive().findTile(points[p][0], points[p][1]);

		if(Array.isArray(tile)) {
			for(var t = 0; t < tile.length; t++) {
				if(tile[t] != null && tile[t].isSolid()) {
					solid = true;
					break;
				}
			}
		}else if(tile != null) {
			solid = tile.isSolid();
		}

		if(solid) { // If this Tile is Solid...
			tempX = points[p][0] - (points[p][0] % tileWidth);
			tempY = points[p][1] - (points[p][1] % tileWidth);

			switch(dir) { // Switch based on the Tile's direction
				case 0: // Left
					collision.left.move		= false;
					collision.left.x		= tempX;
					collision.left.y		= tempY;
				break;
				case 1: // Right
					collision.right.move	= false;
					collision.right.x		= tempX;
					collision.right.y		= tempY;
				break;
				case 2: // Up
					collision.up.move		= false;
					collision.up.x			= tempX;
					collision.up.y			= tempY;
				break;
				case 3: // Down
					collision.down.move		= false;
					collision.down.x		= tempX;
					collision.down.y		= tempY;
				break;
			}
		}
	}

	return collision;
},

And altogether now (Called in the update loop):

/* Begin checking Collisions */
while(checkCollision) {
	var points = this.generatePoints({ x: x, y: y, xVelocity: xVelocity, yVelocity: yVelocity, tileWidth: tileWidth, spritesheet: spritesheet, directions: directions }),
		collision = this.checkPointCollision(points),
		hasCollision = false;

	if(!collision.up.move) {
		yVelocity = -((y - collision.up.y) - tileWidth);
		hasCollision = true;
	}
	if(!collision.down.move) {
		yVelocity = (collision.down.y - (spritesheet.getHeight() * scale)) - y;
		hasCollision = true;
	}
	if(!collision.left.move) {
		xVelocity = 0;
		hasCollision = true;
	}
	if(!collision.right.move) {
		xVelocity = 0;
		hasCollision = true;
	}

	cycle++;
	if(!hasCollision || cycle >= maxCycle) checkCollision = false;
}
/* End checking Collisions */

Variables should (hopefully) be self explanatory, feel free to ask for any information, any help is appreciated, thank you in advance!

Edited by Oyed
0

Share this post


Link to post
Share on other sites

I'm not sure I would understand your problem with bottom-left collision points. I honestly believe just XOR'ing pixels masks could be more than viable for those kind of games in 2014.

0

Share this post


Link to post
Share on other sites

I'm not sure I would understand your problem with bottom-left collision points. I honestly believe just XOR'ing pixels masks could be more than viable for those kind of games in 2014.

 

Could you elaborate please? I'm pretty new to this. Thanks!

0

Share this post


Link to post
Share on other sites

Since I have to explain it to you, I'll explain a more complex (but more powerful) way.

We want to see if the ball hits the odd shaped poly.

[attachment=22946:test.png]

 

Then what we want is to take those two sprites and run a threshold of some kind.

Example: if alpha is >= 25 consider solid.

We mark "solid" pixels "white" to obtain two 1-bit per pixel masks.

[attachment=22947:ball.png][attachment=22948:poly.png]

 

Now, we count the amount of stuff that could be colliding (this might be not trivial) and decide

- black pixels are "empty" and do not collide

- white pixels do.

But all masks are "white" so we decide to assign a bit of color to each sprite. So 32 bit color --> 32 collidable sprites.

Say we decide the ball gets red=128 while the poly gets blue=128.

 

Now the tricky stuff. You need to figure out where the ball sprite is located relatively to the poly sprite.

Then you "draw" them on top of each other by adding the values of the "colored masks" pixel-by-pixel.

What you get is (of course I moved the ball here, otherwise it would be very uninteresting):

[attachment=22949:hitting.png]

 

So this tells you "sprite red hits sprite blue".

 

My previous approach with XOR is simplier as you don't need to choose colors (you can just XOR the original masks) but it doesn't tell you what collides with what. It's not even so reliable. You decide what's going to cut it for you.

Edited by Krohm
1

Share this post


Link to post
Share on other sites

Since I have to explain it to you, I'll explain a more complex (but more powerful) way.

We want to see if the ball hits the odd shaped poly.

attachicon.giftest.png

 

Then what we want is to take those two sprites and run a threshold of some kind.

Example: if alpha is >= 25 consider solid.

We mark "solid" pixels "white" to obtain two 1-bit per pixel masks.

attachicon.gifball.pngattachicon.gifpoly.png

 

Now, we count the amount of stuff that could be colliding (this might be not trivial) and decide

- black pixels are "empty" and do not collide

- white pixels do.

But all masks are "white" so we decide to assign a bit of color to each sprite. So 32 bit color --> 32 collidable sprites.

Say we decide the ball gets red=128 while the poly gets blue=128.

 

Now the tricky stuff. You need to figure out where the ball sprite is located relatively to the poly sprite.

Then you "draw" them on top of each other by adding the values of the "colored masks" pixel-by-pixel.

What you get is (of course I moved the ball here, otherwise it would be very uninteresting):

attachicon.gifhitting.png

 

So this tells you "sprite red hits sprite blue".

 

My previous approach with XOR is simplier as you don't need to choose colors (you can just XOR the original masks) but it doesn't tell you what collides with what. It's not even so reliable. You decide what's going to cut it for you.

 

I think for a Top-Down 2D game made in JavaScript with only rectangles this is a bit of overkill; I wouldn't even know where to start with it.

 

I just need a base, something simple that actually works. I've been looking at my current system for a long time now and just keep on doing things wrong. As long as the player doesn't collide with the other rectangles and can correct it's position, that's fine.

 

Thanks

0

Share this post


Link to post
Share on other sites

Many people here seem to want very accurate pixel collisions.

If you can cut it with rectangles then yes, I agree this is overkill. Keep that in mind anyway.

Are you aware of the "separating axis theorem" or "slabs"?

0

Share this post


Link to post
Share on other sites

Many people here seem to want very accurate pixel collisions.

If you can cut it with rectangles then yes, I agree this is overkill. Keep that in mind anyway.

Are you aware of the "separating axis theorem" or "slabs"?

 

I am not, no - but after some google'ing I found this library https://github.com/jriecken/sat-js

 

Is it worth me implementing my own methods to do this, or using the library? If it's using the library, how can I implement that in to my current loop? I'm a bit confused as to finding out which direction etc. for fixing the collision.

 

Thanks!

0

Share this post


Link to post
Share on other sites

The main problem with collisions is that they are taken for granted. You will never sell them as a bullet point. That's why you should try to minimize the effort using a library. I'm not well aware of collision libraries in JS however a quick google search pointed me to physicsJS, which also seems to support dynamics. I suggest you to take a look at both. Both are possibly overkill for your purpose but both are likely to be a better choice than rolling your physics code long term.

0

Share this post


Link to post
Share on other sites

The main problem with collisions is that they are taken for granted. You will never sell them as a bullet point. That's why you should try to minimize the effort using a library. I'm not well aware of collision libraries in JS however a quick google search pointed me to physicsJS, which also seems to support dynamics. I suggest you to take a look at both. Both are possibly overkill for your purpose but both are likely to be a better choice than rolling your physics code long term.

 

Thank you for your input - I will look in to this!

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