Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

117 Neutral

About Oyed

  • Rank
  1.   Thank you for your input - I will look in to this!
  2.   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!
  3.   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
  4.   Could you elaborate please? I'm pretty new to this. Thanks!
  5. 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:     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!
  6.   If you look at the image, the top and bottom 3 points are above and below the left and right points, so when standing on a solid tile the left and right points are 1 pixel above the surface and aren't triggered.     This is exactly what I do - but collision for both Left, Right and Down is triggered when the player hits the floor after falling due to checking for collision on all sides and correcting as such, so they loose their X Velocity even if they're just hitting the ground.
  7. Hey there,   I'm pretty new to Game Development and am just learning mostly theory and simple physics by developing a HTML5 Canvas Game before moving on to other languages (Being a Web Developer all I had to learn was the Canvas API).   I'm having some issues with my Collision Detection/Correction. To put it simply, here is a run down of what happens to the Player Entity in every loop:   Apply the physics to a temporary X and Y Co-ordinates to find the new position of the Player (E.g. Friction and Gravity) Generate points around the sprite to check for collisions and assign them directions (Which allows for collision correction based on the direction of the collision) Loop through each of the points, find the Tile each point is on and check whether it is solid. If it is, mark that direction (The direction linked to the point, that I created earlier) as solid. After checking all of the points, only apply the physics to X and/or Y if no collision is detected for that direction. The issue is, that when it's finished checking for collisions, if the player is falling the collision for Left and Right will be triggered, as well as Down. This is because it doesn't order collisions; or limit it to just one collision per loop. Here is a "pseudo-code" version of the loop: LOOP tempX = x tempY = y IF up_arrow IS pressed THEN IF NOT jumping AND grounded THEN jumping = TRUE grounded = FALSE yVelocity = -(speed / 2) * 2 // Ignore division and then multiplication by 2, as I'll be using one as a modifier END IF END IF IF left_arrow IS pressed THEN IF xVelocity < speed THEN xVelocity++ END IF END IF IF right_arrow IS pressed THEN IF xVelocity > -speed THEN xVelocity-- END IF END IF xVelocity *= friction yVelocity += gravity grounded = false // COLLISION POINT GENERATION - See below moveLeft = true moveRight = true moveDown = true moveUp = true FOREACH points AS p thisTile = findTile(p.x, p.y) // Gets the Tile instance for the given co-ords thisSolid = thisTile.isSolid() IF thisSolid THEN SWITCH p.direction CASE 0 // Left moveLeft = false BREAK CASE 1 // Right moveRight = false BREAK CASE 2 // Up moveUp = false BREAK CASE 3 // Down moveDown = false BREAK END SWITCH END IF END FOREACH IF !moveDown THEN yVelocity = 0 jumping = false grounded = true END IF IF !moveLeft OR !moveRight THEN xVelocity = 0 jumping = false END IF IF grounded THEN yVelocity = 0 END IF x += xVelocity y += yVelocity END LOOP Collision Point Generation:   In order to check for collisions around the player, the sprite is used to calculate points in which to check for collisions. The points is generates look like this:   http://uploadir.com/u/7ns2jvm0   Each point has a Direction value so that the collision can be corrected based on that. Each point is also generated using a tempX and tempY value, which are just temporary variables which will have the players velocities applied to them before point generation, and then the point generation will use these values.   So, even if the player falls a long distance and hits the floor, because the new X and Y values it uses are un a tile, the Left and Right points are also in the floor tiles and trigger a collision.   I have no idea how to get around this; so any help is greatly appreciated!
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!