Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

body vel/angVel API compatible with Chipmunk C

setVelocity -> setVel
setAngularVelocity -> setAngVel
Added:
getAngVel

Updates buoyancy test too
  • Loading branch information...
commit 804f31e48d334f7af90d728903781ef61dffdce3 1 parent 2dfad07
@ricardoquesada ricardoquesada authored
Showing with 75 additions and 73 deletions.
  1. +25 −24 cp.js
  2. +2 −2 cp.min.js
  3. +23 −23 demo/buoyancy.js
  4. +25 −24 lib/cpBody.js
View
49 cp.js
@@ -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
@@ -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;
@@ -1340,7 +1340,7 @@ 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;
@@ -1348,26 +1348,26 @@ var Body = cp.Body = function(m, i) {
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;
@@ -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.");
@@ -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.");
};
@@ -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()
@@ -1486,7 +1487,7 @@ var filterConstraints = function(node, body, filter)
} else {
node.next_b = filterConstraints(node.next_b, body, filter);
}
-
+
return node;
};
@@ -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;
@@ -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();
};
@@ -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;
}
};
View
4 cp.min.js
2 additions, 2 deletions not shown
View
46 demo/buoyancy.js
@@ -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);
@@ -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);
@@ -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);
@@ -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);
};
@@ -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);
@@ -152,10 +152,10 @@ 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));
@@ -163,11 +163,11 @@ Buoyancy.prototype.waterPreSolve = function(arb, space, ptr) {
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;
};
View
49 lib/cpBody.js
@@ -1,15 +1,15 @@
/* 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
@@ -31,7 +31,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;
@@ -44,7 +44,7 @@ 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;
@@ -52,26 +52,26 @@ var Body = cp.Body = function(m, i) {
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;
@@ -106,7 +106,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.");
@@ -115,9 +115,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.");
};
@@ -127,6 +127,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()
@@ -190,7 +191,7 @@ var filterConstraints = function(node, body, filter)
} else {
node.next_b = filterConstraints(node.next_b, body, filter);
}
-
+
return node;
};
@@ -207,14 +208,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;
@@ -250,26 +251,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();
};
@@ -332,10 +333,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;
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.