Using Chipmunk-JS with ImpactJS is squishy/bouncy?

Official forum for the Chipmunk2D Physics Library.
Post Reply
kirbysayshi
Posts: 3
Joined: Fri Sep 23, 2011 10:38 pm
Contact:

Using Chipmunk-JS with ImpactJS is squishy/bouncy?

Post by kirbysayshi »

I'm very new to chipmunk, but am writing a small plugin that lets you use Chipmunk-JS (https://github.com/josephg/Chipmunk-js) with ImpactJS (http://impactjs.com). It's working, but things are squishy, even with a very small timestep (1/60, 1/180).

You can view a demo here: https://dl.dropbox.com/u/52514/games/im ... sktop.html

By giving the static platforms friction of 1, elasticity 0, things are pretty stiff, but the character definitely seems to embed himself slightly when landing and when pressing against a wall. If friction is set to a lower value, like 0.5, things are much much more bouncy and squishy. When moving the player, he tends to hop as well...

I realize that platformers aren't the best use for a full physics engine (I read a post here mentioning similar things that were solved with a custom velocity function: https://github.com/maximile/Your-Story/ ... cter.m#L40), but I just want to make sure things are working as expected, even if they don't behave exactly how a platformer should.

I'm trying to rule out a few things:

1) Misconfiguration/integration on my part (likely!)
2) Bugs in Chipmunk-JS (?)

Does anyone playing around with the demo see a "oh yeah, it's probably X"? Or something obviously wrong?
kirbysayshi
Posts: 3
Joined: Fri Sep 23, 2011 10:38 pm
Contact:

Re: Using Chipmunk-JS with ImpactJS is squishy/bouncy?

Post by kirbysayshi »

I also wanted to mention that I don't expect anyone to look at code, and nor do I expect support for an unsupported port. I only hoped that someone would look at it, and go, "oh yeah, that effect happens all the time with Chipmunk proper, and involves X".

Anyway, thanks to anyone who looks!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Using Chipmunk-JS with ImpactJS is squishy/bouncy?

Post by slembcke »

Ok, sorry that I missed it before, taking a look now.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Using Chipmunk-JS with ImpactJS is squishy/bouncy?

Post by slembcke »

Hmm. So it definitely acts a little weird yeah, but it doesn't act quite like any of the common errors that I can recognize. Could you share some of your code on how you implement the movement or how you are creating the static collision geometry maybe?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Parasyte
Posts: 2
Joined: Mon Oct 22, 2012 12:13 am
Contact:

Re: Using Chipmunk-JS with ImpactJS is squishy/bouncy?

Post by Parasyte »

Hi there!

I create a game using Chipmunk-js in July: http://parasyte.kodewerx.org/projects/lpcgame/

Here is some advice, based on things that I learned from the project:
  • The first thing you want to do is switch the player movement to use applyForce; You should also use resetForces, because applyForce is additive. Using impulses or large forces will cause the player to get "embedded" into walls and other objects.
  • You might also increase the space collisionSlop property (I set it to 5, default is 0.1), and increase the number of steps to help prevent the embedding.
  • The bounciness comes from resetting the angle and angular velocity in your player update. Because the body is still rotating due to friction, it causes a slight "kick". You should instead set the moment of inertia to Infinity, so that it can never rotate by physical interactions with other shapes.
Also, since my game is top-down, I use a very low space damping (0.00005) to emulate friction. But at the cost that high forces are required to move bodies.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Using Chipmunk-JS with ImpactJS is squishy/bouncy?

Post by slembcke »

@Parasyte. Neat game. I'll have to play more of it later. :)

A few tips for the things you mention:
  • Forces are actually applied the same as impulses, but after the position has been integrated. If you use impulses, or modify the velocity inside of a custom velocity function it should be okay as long as you aren't making unrealistically large velocity changes each step. Because impulses are applied immediately, applying large impulses before stepping the space can cause large overlaps. Setting the same impulse in a velocity function should cause minimal overlap.
  • Setting collision slop to 5, allows shapes to overlap by up to 5 units without trying to push them apart more. I usually recommend setting the collision slop as high as possible without causing visible persistent overlap. Using smaller steps will help with the embedding problem too.

@kirbysayshi

So I realized I could just pull up the JS code in the WebKit debugger. Duh. (I don't do a lot of JS programming) @Parasyte was correct about resetting the rotation. You don't want to do that. Basically you are allowing the object to rotate and have that affect it's motion, and then telling it to reset only the rotation after it had already affected other things. Set the moment of inertia to infinity instead so that the player is impossible to rotate.

For a 2D platformer like that, don't use impulses to propel your player. The YourStory game uses surface velocity to do it instead. Think of a conveyor belt, the overall shape doesn't move, but the surface does. That's what surface velocities allow you to do. They let you say that the feet of the player are moving at a different speed than the rest of the overall shape without having to give the player actual physical feet.

Also, use velocity functions to update these sorts of things. The playerUpdateVelocity() you linked from YourStory is actually called from Chipmunk and not the game. You'll see later there is a line of code like this: "body->velocity_func = playerUpdateVelocity;" that tells Chipmunk to update the velocity of the player by calling that function. I'm sure ChipmunkJS exposes that fairly similarly.

Do use impulses or modify the velocity for things like jumping, but it works best to put them in a custom velocity function.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
kirbysayshi
Posts: 3
Joined: Fri Sep 23, 2011 10:38 pm
Contact:

Re: Using Chipmunk-JS with ImpactJS is squishy/bouncy?

Post by kirbysayshi »

Wow, thanks for the extremely detailed reply, @slembcke, and thanks for the real-world example @Parasyte!

I need to digest and experiment with what was said, but wanted to thank you both initially for even responding.

And @slembcke, I really want to make clear that my second post wasn't meant to be antagonistic or passive agressive in any way; I hope you didn't take it as such, and if you did I'm sorry.

Anyway, I'll experiment a bit and will almost definitely have further questions...
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Using Chipmunk-JS with ImpactJS is squishy/bouncy?

Post by slembcke »

Nah. It's fine. I would have missed the thread otherwise.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests