Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Collision Detection Issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Oyed   Members   -  Reputation: 117

Like
0Likes
Like

Posted 24 July 2014 - 01:07 PM

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, 24 July 2014 - 01:08 PM.


Sponsor:

#2 Krohm   Crossbones+   -  Reputation: 3189

Like
0Likes
Like

Posted 28 July 2014 - 12:03 AM

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.



#3 Oyed   Members   -  Reputation: 117

Like
0Likes
Like

Posted 31 July 2014 - 04:41 PM

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!



#4 Krohm   Crossbones+   -  Reputation: 3189

Like
1Likes
Like

Posted 01 August 2014 - 02:46 AM

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.

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.

ball.png 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):

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, 01 August 2014 - 02:47 AM.


#5 Oyed   Members   -  Reputation: 117

Like
0Likes
Like

Posted 01 August 2014 - 06:18 AM

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



#6 Krohm   Crossbones+   -  Reputation: 3189

Like
0Likes
Like

Posted 01 August 2014 - 11:58 PM

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"?



#7 Oyed   Members   -  Reputation: 117

Like
0Likes
Like

Posted 02 August 2014 - 05:38 AM

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!



#8 Krohm   Crossbones+   -  Reputation: 3189

Like
0Likes
Like

Posted 04 August 2014 - 04:25 AM

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.



#9 Oyed   Members   -  Reputation: 117

Like
0Likes
Like

Posted 04 August 2014 - 09:36 AM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS