Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

body vel/angVel API compatible with Chipmunk C #12

Merged
merged 1 commit into from Nov 8, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 25 additions & 24 deletions cp.js
Expand Up @@ -1295,17 +1295,17 @@ PolyShape.prototype.getVert = function(i)
};

/* Copyright (c) 2007 Scott Lembcke
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -1327,7 +1327,7 @@ var Body = cp.Body = function(m, i) {
//this.m;
/// Mass inverse.
//this.m_inv;

/// Moment of inertia of the body.
/// Must agree with cpBody.i_inv! Use body.setMoment() when changing the moment for this reason.
//this.i;
Expand All @@ -1340,34 +1340,34 @@ var Body = cp.Body = function(m, i) {
this.vx = this.vy = 0;
/// Force acting on the rigid body's center of gravity.
this.f = new Vect(0,0);

/// Rotation of the body around it's center of gravity in radians.
/// Must agree with cpBody.rot! Use cpBodySetAngle() when changing the angle for this reason.
//this.a;
/// Angular velocity of the body around it's center of gravity in radians/second.
this.w = 0;
/// Torque applied to the body around it's center of gravity.
this.t = 0;

/// Cached unit length vector representing the angle of the body.
/// Used for fast rotations using cpvrotate().
//cpVect rot;

/// Maximum velocity allowed when updating the velocity.
this.v_limit = Infinity;
/// Maximum rotational rate (in radians/second) allowed when updating the angular velocity.
this.w_limit = Infinity;

// This stuff is all private.
this.v_biasx = this.v_biasy = 0;
this.w_bias = 0;

this.space = null;

this.shapeList = [];
this.arbiterList = null; // These are both wacky linked lists.
this.constraintList = null;

// This stuff is used to track information on the collision graph.
this.nodeRoot = null;
this.nodeNext = null;
Expand Down Expand Up @@ -1402,7 +1402,7 @@ if (typeof DEBUG !== 'undefined' && DEBUG) {
{
assert(this.m === this.m && this.m_inv === this.m_inv, "Body's mass is invalid.");
assert(this.i === this.i && this.i_inv === this.i_inv, "Body's moment is invalid.");

v_assert_sane(this.p, "Body's position is invalid.");
v_assert_sane(this.f, "Body's force is invalid.");
assert(this.vx === this.vx && Math.abs(this.vx) !== Infinity, "Body's velocity is invalid.");
Expand All @@ -1411,9 +1411,9 @@ if (typeof DEBUG !== 'undefined' && DEBUG) {
assert(this.a === this.a && Math.abs(this.a) !== Infinity, "Body's angle is invalid.");
assert(this.w === this.w && Math.abs(this.w) !== Infinity, "Body's angular velocity is invalid.");
assert(this.t === this.t && Math.abs(this.t) !== Infinity, "Body's torque is invalid.");

v_assert_sane(this.rot, "Internal error: Body's rotation vector is invalid.");

assert(this.v_limit === this.v_limit, "Body's velocity limit is invalid.");
assert(this.w_limit === this.w_limit, "Body's angular velocity limit is invalid.");
};
Expand All @@ -1423,6 +1423,7 @@ if (typeof DEBUG !== 'undefined' && DEBUG) {

Body.prototype.getPos = function() { return this.p; };
Body.prototype.getVel = function() { return new Vect(this.vx, this.vy); };
Body.prototype.getAngVel = function() { return this.w; };

/// Returns true if the body is sleeping.
Body.prototype.isSleeping = function()
Expand Down Expand Up @@ -1486,7 +1487,7 @@ var filterConstraints = function(node, body, filter)
} else {
node.next_b = filterConstraints(node.next_b, body, filter);
}

return node;
};

Expand All @@ -1503,14 +1504,14 @@ Body.prototype.setPos = function(pos)
this.p = pos;
};

Body.prototype.setVelocity = function(velocity)
Body.prototype.setVel = function(velocity)
{
this.activate();
this.vx = velocity.x;
this.vy = velocity.y;
};

Body.prototype.setAngularVelocity = function(w)
Body.prototype.setAngVel = function(w)
{
this.activate();
this.w = w;
Expand Down Expand Up @@ -1546,26 +1547,26 @@ Body.prototype.velocity_func = function(gravity, damping, dt)
var scale = (lensq > v_limit*v_limit) ? v_limit / Math.sqrt(len) : 1;
this.vx = vx * scale;
this.vy = vy * scale;

var w_limit = this.w_limit;
this.w = clamp(this.w*damping + this.t*this.i_inv*dt, -w_limit, w_limit);

this.sanityCheck();
};

Body.prototype.position_func = function(dt)
{
//this.p = vadd(this.p, vmult(vadd(this.v, this.v_bias), dt));

//this.p = this.p + (this.v + this.v_bias) * dt;
this.p.x += (this.vx + this.v_biasx) * dt;
this.p.y += (this.vy + this.v_biasy) * dt;

this.setAngleInternal(this.a + (this.w + this.w_bias)*dt);

this.v_biasx = this.v_biasy = 0;
this.w_bias = 0;

this.sanityCheck();
};

Expand Down Expand Up @@ -1628,10 +1629,10 @@ Body.prototype.eachArbiter = function(func)
var arb = this.arbiterList;
while(arb){
var next = arb.next(this);

arb.swappedColl = (this === arb.body_b);
func(arb);

arb = next;
}
};
Expand Down
4 changes: 2 additions & 2 deletions cp.min.js

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions demo/buoyancy.js
Expand Up @@ -10,9 +10,9 @@ var Buoyancy = function() {
// cpSpaceSetDamping(space, 0.5);
space.sleepTimeThreshold = 0.5;
space.collisionSlop = 0.5;

var staticBody = space.staticBody;

// Create segments around the edge of the screen.
var shape = space.addShape( new cp.SegmentShape(staticBody, cp.v(0,0), cp.v(0,480), 0.0));
shape.setElasticity(1.0);
Expand All @@ -28,17 +28,17 @@ var Buoyancy = function() {
shape.setElasticity(1.0);
shape.setFriction(1.0);
shape.setLayers(NOT_GRABABLE_MASK);

shape = space.addShape( new cp.SegmentShape(staticBody, cp.v(0,480), cp.v(640,480), 0.0));
shape.setElasticity(1.0);
shape.setFriction(1.0);
shape.setLayers(NOT_GRABABLE_MASK);

// {
// Add the edges of the bucket
var bb = new cp.BB(20, 40, 420, 240);
var radius = 5.0;

shape = space.addShape( new cp.SegmentShape(staticBody, cp.v(bb.l, bb.b), cp.v(bb.l, bb.t), radius));
shape.setElasticity(1.0);
shape.setFriction(1.0);
Expand All @@ -53,7 +53,7 @@ var Buoyancy = function() {
shape.setElasticity(1.0);
shape.setFriction(1.0);
shape.setLayers(NOT_GRABABLE_MASK);

// Add the sensor for the water.
shape = space.addShape( new cp.BoxShape2(staticBody, bb) );
shape.setSensor(true);
Expand All @@ -66,31 +66,31 @@ var Buoyancy = function() {
var height = 50.0;
var mass = 0.3*FLUID_DENSITY*width*height;
var moment = cp.momentForBox(mass, width, height);

body = space.addBody( new cp.Body(mass, moment));
body.setPos( cp.v(270, 140));
body.setVelocity( cp.v(0, -100));
body.setAngularVelocity( 1 );
body.setVel( cp.v(0, -100));
body.setAngVel( 1 );

shape = space.addShape( new cp.BoxShape(body, width, height));
shape.setFriction(0.8);
// }

// {
width = 40.0;
height = width*2;
mass = 0.3*FLUID_DENSITY*width*height;
moment = cp.momentForBox(mass, width, height);

body = space.addBody( new cp.Body(mass, moment));
body.setPos(cp.v(120, 190));
body.setVelocity(cp.v(0, -100));
body.setAngularVelocity(1);
body.setVel(cp.v(0, -100));
body.setAngVel(1);

shape = space.addShape(new cp.BoxShape(body, width, height));
shape.setFriction(0.8);
// }

space.addCollisionHandler( 1, 0, null, this.waterPreSolve, null, null);
};

Expand Down Expand Up @@ -129,13 +129,13 @@ Buoyancy.prototype.waterPreSolve = function(arb, space, ptr) {
clipped.push( a.x );
clipped.push( a.y );
}

var a_level = a.y - level;
var b_level = b.y - level;

if(a_level*b_level < 0.0){
var t = Math.abs(a_level)/(Math.abs(a_level) + Math.abs(b_level));

var v = cp.v.lerp(a, b, t);
clipped.push(v.x);
clipped.push(v.y);
Expand All @@ -152,22 +152,22 @@ Buoyancy.prototype.waterPreSolve = function(arb, space, ptr) {

var dt = space.getCurrentTimeStep();
var g = space.gravity;

// Apply the buoyancy force as an impulse.
body.applyImpulse( cp.v.mult(g, -displacedMass*dt), r);

// Apply linear damping for the fluid drag.
var v_centroid = cp.v.add(body.getVel(), cp.v.mult(cp.v.perp(r), body.w));
var k = 1; //k_scalar_body(body, r, cp.v.normalize_safe(v_centroid));
var damping = clippedArea*FLUID_DRAG*FLUID_DENSITY;
var v_coef = Math.exp(-damping*dt*k); // linear drag
// var v_coef = 1.0/(1.0 + damping*dt*cp.v.len(v_centroid)*k); // quadratic drag
body.applyImpulse( cp.v.mult(cp.v.sub(cp.v.mult(v_centroid, v_coef), v_centroid), 1.0/k), r);

// Apply angular damping for the fluid drag.
var w_damping = cp.momentForPoly(FLUID_DRAG*FLUID_DENSITY*clippedArea, clipped, cp.v.neg(body.p));
body.w *= Math.exp(-w_damping*dt* (1/body.i));

return true;
};

Expand Down