Permalink
Browse files

Added a bunch of little missing getters & setters.

  • Loading branch information...
1 parent d0a855b commit 6d57c6cf4f8a8b01388ed2eff8aa9bf73ef9afb2 @josephg committed Oct 23, 2012
Showing with 51 additions and 9 deletions.
  1. +26 −4 cp.js
  2. +2 −2 cp.min.js
  3. +1 −3 demo/buoyancy.js
  4. +9 −0 lib/cpArbiter.js
  5. +3 −0 lib/cpBody.js
  6. +7 −0 lib/cpPolyShape.js
  7. +1 −0 lib/cpShape.js
  8. +2 −0 lib/cpSpace.js
View
30 cp.js
@@ -710,6 +710,7 @@ Shape.prototype.setFriction = function(u) { this.body.activate(); this.u = u; };
Shape.prototype.setLayers = function(layers) { this.body.activate(); this.layers = layers; };
Shape.prototype.setSensor = function(sensor) { this.body.activate(); this.sensor = sensor; };
Shape.prototype.setCollisionType = function(collision_type) { this.body.activate(); this.collision_type = collision_type; };
+Shape.prototype.getBody = function() { return this.body; };
Shape.prototype.active = function()
{
@@ -1275,6 +1276,13 @@ PolyShape.prototype.containsVertPartial = function(vx, vy, n)
return true;
};
+// These methods are provided for API compatibility with Chipmunk. I recommend against using
+// them - just access the poly.verts list directly.
+PolyShape.prototype.getNumVerts = function() { return this.verts.length / 2; };
+PolyShape.prototype.getVert = function(i)
+{
+ return new Vect(this.verts[i * 2], this.verts[i * 2 + 1]);
+};
/* Copyright (c) 2007 Scott Lembcke
*
@@ -1403,6 +1411,9 @@ if (typeof DEBUG !== 'undefined' && DEBUG) {
Body.prototype.sanityCheck = function(){};
}
+Body.prototype.getPos = function() { return this.p; };
+Body.prototype.getVel = function() { return new Vect(this.vx, this.vy); };
+
/// Returns true if the body is sleeping.
Body.prototype.isSleeping = function()
{
@@ -1570,19 +1581,19 @@ Body.prototype.applyImpulse = function(j, r)
Body.prototype.getVelAtPoint = function(r)
{
- return vadd(new Vect(this.vx, this.vy), vmult(vperp(r), body.w));
+ return vadd(new Vect(this.vx, this.vy), vmult(vperp(r), this.w));
};
/// Get the velocity on a body (in world units) at a point on the body in world coordinates.
Body.prototype.getVelAtWorldPoint = function(point)
{
- return this.getVelAtPoint(vsub(point, body.p));
+ return this.getVelAtPoint(vsub(point, this.p));
};
/// Get the velocity on a body (in world units) at a point on the body in local coordinates.
Body.prototype.getVelAtLocalPoint = function(point)
{
- return this.getVelAtPoint(vrotate(point, body.rot));
+ return this.getVelAtPoint(vrotate(point, this.rot));
};
Body.prototype.eachShape = function(func)
@@ -1596,7 +1607,7 @@ Body.prototype.eachConstraint = function(func)
{
var constraint = this.constraintList;
while(constraint) {
- var next = constraint.next(body);
+ var next = constraint.next(this);
func(constraint);
constraint = next;
}
@@ -2650,6 +2661,15 @@ var Arbiter = function(a, b) {
this.state = 'first coll';
};
+Arbiter.prototype.getShapes = function()
+{
+ if (this.swappedColl){
+ return [this.b, this.a];
+ }else{
+ return [this.a, this.b];
+ }
+}
+
/// Calculate the total impulse that was applied by this arbiter.
/// This function should only be called from a post-solve, post-step or cpBodyEachArbiter callback.
Arbiter.prototype.totalImpulse = function()
@@ -3453,6 +3473,8 @@ var Space = cp.Space = function() {
this.collideShapes = this.makeCollideShapes();
};
+Space.prototype.getCurrentTimeStep = function() { return this.curr_dt; };
+
/// returns true from inside a callback and objects cannot be added/removed.
Space.prototype.isLocked = function()
{
View
4 cp.min.js
2 additions, 2 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
4 demo/buoyancy.js
@@ -106,7 +106,6 @@ Buoyancy.prototype.update = function(dt)
};
Buoyancy.prototype.waterPreSolve = function(arb, space, ptr) {
-
var shapes = arb.getShapes();
var water = shapes[0];
var poly = shapes[1];
@@ -158,7 +157,7 @@ Buoyancy.prototype.waterPreSolve = function(arb, space, ptr) {
body.applyImpulse( cp.v.mult(g, -displacedMass*dt), r);
// Apply linear damping for the fluid drag.
- var v_centroid = cp.v.add(body.v, cp.v.mult(cp.v.perp(r), body.w));
+ 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
@@ -171,7 +170,6 @@ Buoyancy.prototype.waterPreSolve = function(arb, space, ptr) {
return true;
};
-Buoyancy.prototype = Object.create(Demo.prototype);
addDemo('Buoyancy', Buoyancy);
View
9 lib/cpArbiter.js
@@ -90,6 +90,15 @@ var Arbiter = function(a, b) {
this.state = 'first coll';
};
+Arbiter.prototype.getShapes = function()
+{
+ if (this.swappedColl){
+ return [this.b, this.a];
+ }else{
+ return [this.a, this.b];
+ }
+}
+
/// Calculate the total impulse that was applied by this arbiter.
/// This function should only be called from a post-solve, post-step or cpBodyEachArbiter callback.
Arbiter.prototype.totalImpulse = function()
View
3 lib/cpBody.js
@@ -125,6 +125,9 @@ if (typeof DEBUG !== 'undefined' && DEBUG) {
Body.prototype.sanityCheck = function(){};
}
+Body.prototype.getPos = function() { return this.p; };
+Body.prototype.getVel = function() { return new Vect(this.vx, this.vy); };
+
@ricardoquesada
ricardoquesada added a line comment Oct 23, 2012

Should the getter be renamed to getVelocity since the setter is called setVelocity ?
Or should the setter be renamed to setVel ?

@josephg
Owner
josephg added a line comment Oct 23, 2012

Its called cpBodyGetVel in Chipmunk, so I think getVel makes sense. Do we have a setter called setVelocity?

@ricardoquesada
ricardoquesada added a line comment Oct 23, 2012

It seems so:
fee633f#L1L1490

And then I added setAngularVelocity instead of setAngVel just to follow the Chipmunk-JS convention.
But if you think it should be called getVel() / setVel() (and I agree with you), then we should also rename set/get AngularVelocity() to set/get AngVel() too.

@josephg
Owner
josephg added a line comment Oct 23, 2012

Yeah, good idea. Feel free to fix!

@ricardoquesada
ricardoquesada added a line comment Oct 24, 2012

ok. I've just sent a pull request with those changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
/// Returns true if the body is sleeping.
Body.prototype.isSleeping = function()
{
View
7 lib/cpPolyShape.js
@@ -268,4 +268,11 @@ PolyShape.prototype.containsVertPartial = function(vx, vy, n)
return true;
};
+// These methods are provided for API compatibility with Chipmunk. I recommend against using
+// them - just access the poly.verts list directly.
+PolyShape.prototype.getNumVerts = function() { return this.verts.length / 2; };
+PolyShape.prototype.getVert = function(i)
+{
+ return new Vect(this.verts[i * 2], this.verts[i * 2 + 1]);
+};
View
1 lib/cpShape.js
@@ -84,6 +84,7 @@ Shape.prototype.setFriction = function(u) { this.body.activate(); this.u = u; };
Shape.prototype.setLayers = function(layers) { this.body.activate(); this.layers = layers; };
Shape.prototype.setSensor = function(sensor) { this.body.activate(); this.sensor = sensor; };
Shape.prototype.setCollisionType = function(collision_type) { this.body.activate(); this.collision_type = collision_type; };
+Shape.prototype.getBody = function() { return this.body; };
Shape.prototype.active = function()
{
View
2 lib/cpSpace.js
@@ -97,6 +97,8 @@ var Space = cp.Space = function() {
this.collideShapes = this.makeCollideShapes();
};
+Space.prototype.getCurrentTimeStep = function() { return this.curr_dt; };
+
/// returns true from inside a callback and objects cannot be added/removed.
Space.prototype.isLocked = function()
{

0 comments on commit 6d57c6c

Please sign in to comment.