josephg / Chipmunk-js

Port of slembcke/Chipmunk-Physics to Javascript
http://dl.dropbox.com/u/2494815/demo/Joints.html
536 stars 59 forks source link

jerky player movement #26

Closed englercj closed 10 years ago

englercj commented 10 years ago

Hey I am using the "Tank Demo" method of controlling a top down player. That is, a control body is created attached to the player body via pivot and gear constraint, and then when movement controls are pressed I set the velocity of the control body.

Here is how I create and attach my bodies:

var body = new cp.Body(1, Infinity),
    cbody = new cp.Body(Infinity, Infinity),
    cpivot = new cp.PivotJoint(cbody, body, cp.vzero, cp.vzero),
    cgear = new cp.GearJoint(cbody, body, 0, 1);

cpivot.maxBias = 0; //disable join correction
cpivot.maxForce = 10000; //emulate linear friction

cgear.errorBias = 0; //attempt to fully correct the joint each step
cgear.maxBias = 1.2; //but limit the angular correction
cgear.maxForce = 50000; //emulate angular friction

space.addBody(body);
space.addBody(cbody);
space.addConstraint(cpivot);
space.addConstraint(cgear);

Each frame I step the space and update my sprites like so:

space.step(dt); //dt is time since last frame in seconds

//go through each changed shape
space.activeShapes.each(function(shape) {
    var spr = shape.sprite; //I set this when the shape is created so I have the ref here
    spr.position.x = shape.body.p.x;
    spr.position.y = shape.body.p.y;
    spr.rotation = shape.body.a;
});

What I am seeing is jerky movement. Specifically there are micro teleportations as the player moves. I have tried everything I can think of, but I can't figure out why the movement isn't smooth. Happen to have any ideas?

josephg commented 10 years ago

Failing that, you'd be better off asking on the chipmunk forum. Scott is a better person to ask than me.

englercj commented 10 years ago

@josephg My dt values are not constant (though are very very small floats). The problem is definitely coming form the position changes in chipmunk. The difference between each position is as big as 5-10 units which causes the strange jumping.

I'll try some more things then ask Scott on the forums.

englercj commented 10 years ago

Changing my time-step to be constant yeilded good results, though it still doesn't seem as smooth as it should be.

I posted on the forums here: http://chipmunk-physics.net/forum/viewtopic.php?f=1&t=3169

josephg commented 10 years ago

Oh - is your rendering happening smoothly? How are you rendering?

englercj commented 10 years ago

I'm rendering via WebGL and my fps/rendering is fine. I debugged for days before coming to the conclusion it had to be chipmunk (or my implementation of it). Objects moving around that are not simulated by chipmunk are all smooth, it is only when i try to move something in the method above that I get a strange jumping effect.

josephg commented 10 years ago

Ok, then yeah - see what scott says.

On Fri, Jan 24, 2014 at 3:14 PM, Chad Engler notifications@github.comwrote:

I'm rendering via WebGL and my fps/rendering works fine. I debugged for days before coming to the conclusion it had to be chipmunk (or my implementation of it)

— Reply to this email directly or view it on GitHubhttps://github.com/josephg/Chipmunk-js/issues/26#issuecomment-33270982 .

englercj commented 10 years ago

Seems I fixed the issue by completely separating my render loop and physics simulation, plus used a constant step for physics.