Hi there,
I know that this post is a little old, but I work on a javascript version of Chipmunk, and I have the same problem.
The fact is that the dynamic body of the hero seems to sink inside other bodies (that are static), check the demo :
http://dooxe-creative.net/gwe/
(you can go left, right, with arrows and jump with the spacebar).
Here is some come if it can help (
http://dooxe-creative.net/gwe/src/states/MapState.js) :
Code: Select all
/**
* Created by dooxe on 12/05/2015.
*/
///
///
/// @author dooxe
///
define(function(require)
{
var utils = require('utils/utils');
return utils.extend(new Kiwi.State('MapState'),
{
//
preload : function()
{
console.log('SampleState : preload');
Kiwi.State.prototype.preload.call(this);
this.addJSON('tilemapjson', 'data/maps/Map_002.tiled.json');
},
//
create : function()
{
console.log('SampleState : create');
Kiwi.State.prototype.create.call(this);
//Initialise the physics debugger
this.game.chipmunkDebug.init();
var self = this;
// Physical world
var space = this.space = this.game.chipmunk.space;
space.gravity = cp.v(0, 1000);
space.iterations = 80;
space.sleepTimeThreshold = 0.5;
space.collisionSlop = 0.5;
//
var createStaticBody = function()
{
var body = new cp.Body(Infinity,Infinity);
body.nodeIdleTime = Infinity;
return body;
};
// Collision Map
var map = JSON.parse(this.data.tilemapjson.data);
var collisions = map.collisions;
for(var y = 0; y < map.height; ++y)
{
for(var x = 0; x < map.width; ++x)
{
var c = collisions[x][y];
if(c > 0)
{
var width = 32;
var height = 32;
var mass = width * height * 1 / 10000;
var tw = map.tilewidth;
var th = map.tileheight;
var position = cp.v(map.tilewidth/2 + x * map.tilewidth, map.tileheight/2 + y * map.tileheight);
switch(c)
{
// Square
case 1 :
{
var rock = createStaticBody();
rock.setPos(position);
var shape = space.addShape(new cp.BoxShape(rock, width, height));
shape.setFriction(50);
break;
}
// Bottom-Left triangle
case 2 :
{
var verts = [
0, 0, tw, th, 0, th, 0, 0
];
cp.convexHull(verts, null, 2);
var col = createStaticBody();
var pos = cp.v(x * map.tilewidth, y * map.tileheight);
col.setPos(pos);
var shape = space.addShape(new cp.PolyShape(col,verts,cp.v(0, 0)));
shape.setFriction(50);
break;
}
// Top-Left triangle
case 3 :
{
var verts = [
0, 0, tw, 0, 0, th, 0, 0
];
cp.convexHull(verts, null, 2);
var col = createStaticBody();
var pos = cp.v(x * map.tilewidth, y * map.tileheight);
col.setPos(pos);
var shape = space.addShape(new cp.PolyShape(col,verts,cp.v(0, 0)));
shape.setFriction(50);
break;
}
// Top-Right triangle
case 4 :
{
var verts = [
0, 0, tw, 0, tw, th, 0, 0
];
cp.convexHull(verts, null, 2);
var col = createStaticBody();
var pos = cp.v(x * map.tilewidth, y * map.tileheight);
col.setPos(pos);
var shape = space.addShape(new cp.PolyShape(col,verts,cp.v(0, 0)));
shape.setFriction(50);
break;
}
// Bottom-Right triangle
case 5 :
{
var verts = [
tw, 0, tw, th, 0, th, tw, 0
];
cp.convexHull(verts, null, 2);
var col = createStaticBody();
var pos = cp.v(x * map.tilewidth, y * map.tileheight);
col.setPos(pos);
var shape = space.addShape(new cp.PolyShape(col,verts,cp.v(0, 0)));
shape.setFriction(50);
break;
}
}
}
}
}
// Create the hero
var heroW = 25;
var heroH = 25;
var mass = 500;
var body = space.addBody(new cp.Body(mass, cp.momentForBox(mass, heroW, heroH)));
body.setPos(cp.v(9*32,7*32));
var box = space.addShape(new cp.BoxShape(body, heroW, heroH));
box.setFriction(500000);
this.hero = box;
// Keyboard management
this.keyboard = this.game.input.keyboard;
this.game.input.keyboard.onKeyDownOnce.add( this.keyDownOnce, this );
this.game.input.keyboard.onKeyUp.add( this.keyUp, this );
this.leftK = this.keyboard.addKey(Kiwi.Input.Keycodes.LEFT, true);
this.rightK = this.keyboard.addKey(Kiwi.Input.Keycodes.RIGHT, true);
this.jumpK = this.keyboard.addKey(Kiwi.Input.Keycodes.SPACEBAR, true);
this.jumpV = cp.v(0,0);
},
keyDownOnce : function(keyCode, key)
{
this.hero.body.applyForce(cp.v(0,50),this.hero.body.p);
if(keyCode == Kiwi.Input.Keycodes.SPACEBAR)
{
this.jumpV = cp.v(0,-50);
}
},
keyUp : function(keyCode, key)
{
if(keyCode == Kiwi.Input.Keycodes.SPACEBAR)
{
this.jumpV = cp.v(0,0);
}
},
buttonDownOnce : function(button)
{
console.log(button);
},
//
update : function()
{
Kiwi.State.prototype.update.call(this);
var heroPos = this.hero.body.p;
var moved = false;
var newVelo = cp.v(0,0);
if(this.rightK.isDown)
{
newVelo.x = 50;
moved = true;
}
if(this.leftK.isDown)
{
newVelo.x = -50;
moved = true;
}
if(moved)
{
//this.hero.body.applyForce(newVelo, this.hero.body.local2World(this.hero.body.p));
this.hero.body.setVel(this.hero.body.getVel().add(newVelo));
}
this.hero.body.setVel(this.hero.body.getVel().add(this.jumpV));
// Lock the hero's body rotation
this.hero.body.setAngVel(0);
this.hero.body.setAngle(0);
// Centering the camera on the hero
this.game.cameras.defaultCamera.transform.x = -this.hero.body.p.x + this.game.stage.width/2;
this.game.cameras.defaultCamera.transform.y = -this.hero.body.p.y + this.game.stage.height/2;
}
});
});
I have already searched for some help, but I didn't find a solution to this problem.
Do you have an idea of what happens ?
Thanks