Jump to content

Physics collision question


sapfeer0k
 Share

Recommended Posts

Hello, folks!

 

I'm making simple game, "Landing on the Mars".  This is a link http://projects.lomakov.net/html5/Racing/index.html

Currently, I'm stuck with the ground generation. I'm using a poligon to emulate uneven ground. But, in this case, physics system works wrong :( Collision doesn't happen with polygon body, but little above its borders. I've tried to play with body.translate(), but got nothing :(

 

And second question, how can I fill the ground polygon with texture? I've tried Pixi mask, but no luck :( Any help very appreciated! :-)

 

Code: 

function groundGenerator(){    var points = [];    points.push(0);    points.push(getRandomInt(0, 50) - 30);    for(var x=getRandomInt(50, 150); x<1024; x+=getRandomInt(10, 100)) {        points.push(x);        points.push(getRandomInt(0, -150) + 10);    }    points.push(1024)    points.push(0);    points.push(1024)    points.push(49);    points.push(0)    points.push(49);    return points;}function create(){    game.physics.gravity.y = 15;    bg = game.add.tileSprite(0, 0, 1024, 512, 'background');    bg.body.allowGravity = false;    ship = game.add.sprite(512, 100, 'ship');    ship.body.collideWorldBounds = false;    ship.anchor.setTo(0.5, 0.5);    ship.body.setPolygon(5, 5, 30, 5, 30, 35, 5, 35);    var points = groundGenerator();    ground = game.add.sprite(0, 512 - 50, 'ground');    ground.body.setPolygon(points);    ground.body.backgroundColor = '#111111';    ground.body.allowGravity = false;    ground.body.immovable = true;}function collisionHandler(ship, ground){    console.log("ship landed");    isLanded = true;    console.log(vSpeed);    if (ship.angle != 0 || Math.abs(hSpeed) > 20 || vSpeed < -40) {        ship.kill();        explosion = game.add.sprite(ship.body.x, ship.body.y, 'explosion');        setTimeout(function() {explosion.kill();}, 150);        // explosion    } else {        resetPhysicsOnLanding(ship);    }    acceleration = 0;}
Link to comment
Share on other sites

If it's colliding with the polygon bounds and not the polygon itself the most common cause of that is that your poly isn't a proper convex polygon, maybe some points are intersecting or the angles are too much, so it's failing to build and resorting to a rectangle.

 

http://en.wikipedia.org/wiki/Convex_and_concave_polygons

 

Thanks for the response! Yes, you're right. My polygon is a concave polygon. Could you give me any other clue how to emulate uneven ground?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...