[BEHAVIOR] Chipmunk Physics

  • Prominent

    I forget exactly where. Search for "poly" in the runtime.js for how I accessed it. It was basically mirroring what the built in physics behavior does. They both use some helper functions that are defined in commonjs.js. (I think that's the file, it's in the exporters/html5 folder).

    The functions allow you to get a list of the points that make up the collision polygon, but transformed to match layout coordinants. The sprite behavior may be another place to find the list of points.

  • R0J0hound , I managed to create a behavior to get nearest polygon point coordinates. The problem I have now is that it isn't correct position if it is retrieved during on post collision in the chipmunk behavior. If I have an object rotating and moving the coordinate is off (maybe it is from the previous game cycle?)..

    Any suggestions on how to apply the correct offsets to get the correct position when a collision happens?

  • Prominent

    Yeah, it looks like it's from the previous position. You can try adding these two lines to line#6528 :



    That should update the c2 object from it's physics body. I haven't tested it, so I don't know if it has any other side effects, because usually it's only done at the end of the tick. Looking at the function it may but I don't have the time to really analyze and debug it.

  • R0J0hound , Thanks! that seems to help. The positions are more synced now, so now my events are working more like how I expect them to. I'll let you know if I run into any side-effects.

  • Oh my god,

    This plugin is absolutely godly.

    I had been using the default box2d plugin in my project and thought it would be not worth the hassle replacing it with this Chipmunk plugin.

    Well, I done it anyway. And...DAYMN. I need to get measurements, it has improved the FPS dramatically. Not to mention the collision system is much more suited for my project.

    Thank you so much R0J0hound

  • Nice work Hound

  • R0J0hound Is there a way to use multiple collision groups with one object? Let's say I want that object A does not collide with objects B and C but collide width D and F. In the chipmunk doc I found that it's possible to set multiple collision groups on one object, but in C2 plugin I can't find the way to achieve that.

  • xoros - you want the Collision Layers property for the behavior to do that. It takes 8 hexadecimal digits to give you 32 possible layers, but I just use 0's & 1's, giving me 8 collision layers.

    In your example, this is how I'd set up collision layers for each object:

    10000000 - A

    01000000 - B

    01000000 - C

    11000000 - D

    11000000 - F

    So you can see each column in the matrix is a layer, & any two objects in a column sharing a 1 will collide.

    Here's a more complex example where A doesn't collide with B & C, but collides with D & F.

    B doesn't collide with C or D, but does with F.

    C collides with D, but not B or F.

    D collides with A, F & C but not B.

    F collides with A, B & D but not C.

    10000000 - A

    01000000 - B

    00100000 - C

    10100000 - D

    11000000 - F

    Then you could extend this by giving all instances of a particular object a Collision Group number (which is just a single integer). Objects with the same non-zero group number won't collide.

    This is handy if say object F is physics debris & doesn't need to collide with other F's for performance reasons.

  • mattb thanks for explantion

  • Had a go at making some dynamic smoke for a stealth game, the idea being that it can block LOS or laser raycasts & triggers smoke detectors...

    https://dl.dropboxusercontent.com/u/523 ... _test.capx

  • > DatapawWolf

    > It's not using the object's origin, it's just using the center of the object regardless of where the origin is. It was an oversight to not take into account the center of a polygon shape. I'll be taking a look into it.


    Dont want to seem pushy but how is this coming along.


    R0J0hound , DatapawWolf & spongehammer

    I was looking into that, but I'm not sure what would be the best way? Perhaps directly addressing to the center of the object as the first imagepoint of the sprite?

    I believe the part of the code that should be edited is around lines 183->

    cp.centroidForPoly = function(verts)
    	var sum = 0;
    	var vsum = new Vect(0,0);
    	for(var i=0, len=verts.length; i<len; i+=2){
    		var v1 = new Vect(verts[i], verts[i+1]);
    		var v2 = new Vect(verts[(i+2)%len], verts[(i+3)%len]);
    		var cross = vcross(v1, v2);
    		sum += cross;
    		vsum = vadd(vsum, vmult(vadd(v1, v2), cross));
    	return vmult(vsum, 1/(3*sum));
    cp.recenterPoly = function(verts)
    	var centroid = cp.centroidForPoly(verts);
    	for(var i=0; i<verts.length; i+=2){
    		verts[i] -= centroid.x;
    		verts[i+1] -= centroid.y;
    also lines 1532->
    /// Position of the rigid body's center of gravity.
    	this.p = new Vect(0,0);
    	/// Velocity of the rigid body's center of gravity.
    	this.vx = this.vy = 0;
    	/// Force acting on the rigid body's center of gravity.
    	this.f = new Vect(0,0);[/code:2noityua]
    However, the problem with the solution is that how objects without imagepoint such as tilemaps would have their center calculated correctly with the introduction of the secondary imagepoint as center of mass feature? Did you have any breakthrough idea on this R0J0?

    mattb that's nicely done steam effect

  • R0J0hound

    Sometimes objects with chipmunk physics do not register standard overlapping/ on collision events. Actually they do not register most of the time. They tend to register standard collision only if collision impact is very strong (was moving at high speed). Is it a bug or a feature?

  • xoros

    It independent of C2's collision detection, so it doesn't trigger a collision event. The built-in physics does, but for this plugin it's better to use the collision triggers it has since you can get more info about the collision.


    It's not exactly a simple change. Right now a vector is calculated from the object's center to the origin, and that is used to update the sprite's position from the physics position, and vise versa. I just need to update the math to use the COM instead of the center, which is tricky enough to require a re-think of a lot of the code. I need to handle moving the com when resizing and changing animation frames and collision shapes while at the same time keeping the object stationary.

    Calculating the COM isn't an issue as Chipmunk provides functions to do that for all of it's shapes. The tilemap would be a special case which would be an average of the tile's COM. But I probable won't do that since tilemaps can't rotate and are usually static so it would be a needless calculation.

  • Thanks for the clarification R0J0hound

    it indeed seems very complicated change - and good point about the tilemaps! Just after posting here, I thought about the tilemap case being unnecessary (it must be these late working hours that made me not think straight). After few "quick" tests with image points, I decided better to not edit the physics engine but go with "fake it until you make it" approach instead. Basically "tricking" the system to have the center correctly set by having size adjusted invisible sprite with chipmunk physics and the real sprite pinned onto the chipmunk object

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I added a pivot joint between two object, but when I set, make changes to the ang velocity the joint origin don't keep up with the changes. Same happens when it hits something.

    Is this some limitation of chipmunk?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)