Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added (incomplete) joints demo

  • Loading branch information...
commit 2fcdb06cec64ab8a5e4fb3fd02ef4aede407b799 1 parent d7b1fe2
Joseph Gentle josephg authored

Showing 2 changed files with 246 additions and 0 deletions. Show diff stats Hide diff stats

  1. +7 0 demo/Joints.html
  2. +239 0 demo/Joints.js
7 demo/Joints.html
... ... @@ -0,0 +1,7 @@
  1 +<canvas></canvas>
  2 +<script src="../cp.js"></script>
  3 +<script src="demo.js"></script>
  4 +<script src="Joints.js"></script>
  5 +<script>
  6 +(new Joints()).run();
  7 +</script>
239 demo/Joints.js
... ... @@ -0,0 +1,239 @@
  1 +/* Copyright (c) 2007 Scott Lembcke
  2 + *
  3 + * Permission is hereby granted, free of charge, to any person obtaining a copy
  4 + * of this software and associated documentation files (the "Software"), to deal
  5 + * in the Software without restriction, including without limitation the rights
  6 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7 + * copies of the Software, and to permit persons to whom the Software is
  8 + * furnished to do so, subject to the following conditions:
  9 + *
  10 + * The above copyright notice and this permission notice shall be included in
  11 + * all copies or substantial portions of the Software.
  12 + *
  13 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19 + * SOFTWARE.
  20 + */
  21 +
  22 +var Joints = function() {
  23 + Demo.call(this);
  24 +
  25 + var space = this.space;
  26 +
  27 + var addBall = function(pos, boxOffset)
  28 + {
  29 + var radius = 15;
  30 + var mass = 1;
  31 + var body = space.addBody(new cp.Body(mass, cp.momentForCircle(mass, 0, radius, v(0,0))));
  32 + body.setPos(v.add(pos, boxOffset));
  33 +
  34 + var shape = space.addShape(new cp.CircleShape(body, radius, v(0,0)));
  35 + shape.setElasticity(0);
  36 + shape.setFriction(0.7);
  37 +
  38 + return body;
  39 + };
  40 +
  41 + var addLever = function(pos, boxOffset)
  42 + {
  43 + var mass = 1;
  44 + var a = v(0, 15);
  45 + var b = v(0, -15);
  46 +
  47 + var body = space.addBody(new cp.Body(mass, cp.momentForSegment(mass, a, b)));
  48 + body.setPos(v.add(pos, v.add(boxOffset, v(0, -15))));
  49 +
  50 + var shape = space.addShape(new cp.SegmentShape(body, a, b, 5));
  51 + shape.setElasticity(0);
  52 + shape.setFriction(0.7);
  53 +
  54 + return body;
  55 + };
  56 +
  57 + var addBar = function(pos, boxOffset)
  58 + {
  59 + var mass = 2;
  60 + var a = v(0, 30);
  61 + var b = v(0, -30);
  62 +
  63 + var body = space.addBody(new cp.Body(mass, cp.momentForSegment(mass, a, b)));
  64 + body.setPos(v.add(pos, boxOffset));
  65 +
  66 + var shape = space.addShape(new cp.SegmentShape(body, a, b, 5));
  67 + shape.setElasticity(0);
  68 + shape.setFriction(0.7);
  69 +
  70 + return body;
  71 + };
  72 +
  73 + var addWheel = function(pos, boxOffset)
  74 + {
  75 + var radius = 15;
  76 + var mass = 1;
  77 + var body = space.addBody(new cp.Body(mass, cp.momentForCircle(mass, 0, radius, v(0,0))));
  78 + body.setPos(v.add(pos, boxOffset));
  79 +
  80 + var shape = space.addShape(new cp.CircleShape(body, radius, v(0,0)));
  81 + shape.setElasticity(0);
  82 + shape.setFriction(0.7);
  83 + shape.group = 1; // use a group to keep the car parts from colliding
  84 +
  85 + return body;
  86 + };
  87 +
  88 + var addChassis = function(pos, boxOffset)
  89 + {
  90 + var mass = 5;
  91 + var width = 80;
  92 + var height = 30;
  93 +
  94 + var body = space.addBody(new cp.Body(mass, cp.momentForBox(mass, width, height)));
  95 + body.setPos(v.add(pos, boxOffset));
  96 +
  97 + var shape = space.addShape(new cp.BoxShape(body, width, height));
  98 + shape.setElasticity(0);
  99 + shape.setFriction(0.7);
  100 + shape.group = 1; // use a group to keep the car parts from colliding
  101 +
  102 + return body;
  103 + };
  104 +
  105 + space.iterations = 10;
  106 + space.gravity = v(0, -100);
  107 + space.sleepTimeThreshold = 0.5;
  108 +
  109 + var staticBody = space.staticBody;
  110 + var shape;
  111 +
  112 + for(var y = 480; y >= 0; y -= 120) {
  113 + shape = space.addShape(new cp.SegmentShape(staticBody, v(0,y), v(640,y), 0));
  114 + shape.setElasticity(1);
  115 + shape.setFriction(1);
  116 + shape.layers = NOT_GRABABLE_MASK;
  117 + }
  118 +
  119 + for(var x = 0; x <= 640; x += 160) {
  120 + shape = space.addShape(new cp.SegmentShape(staticBody, v(x,0), v(x,480), 0));
  121 + shape.setElasticity(1);
  122 + shape.setFriction(1);
  123 + shape.layers = NOT_GRABABLE_MASK;
  124 + }
  125 +
  126 + var boxOffset;
  127 + var body1, body2;
  128 +
  129 + var posA = v( 50, 60);
  130 + var posB = v(110, 60);
  131 +
  132 + var POS_A = function() { return v.add(boxOffset, posA); };
  133 + var POS_B = function() { return v.add(boxOffset, posB); };
  134 + //#define POS_A vadd(boxOffset, posA)
  135 + //#define POS_B vadd(boxOffset, posB)
  136 +
  137 + // Pin Joints - Link shapes with a solid bar or pin.
  138 + // Keeps the anchor points the same distance apart from when the joint was created.
  139 + boxOffset = v(0, 0);
  140 + body1 = addBall(posA, boxOffset);
  141 + body2 = addBall(posB, boxOffset);
  142 + space.addConstraint(new cp.PinJoint(body1, body2, v(15,0), v(-15,0)));
  143 +
  144 + // Slide Joints - Like pin joints but with a min/max distance.
  145 + // Can be used for a cheap approximation of a rope.
  146 + boxOffset = v(160, 0);
  147 + body1 = addBall(posA, boxOffset);
  148 + body2 = addBall(posB, boxOffset);
  149 + space.addConstraint(new cp.SlideJoint(body1, body2, v(15,0), v(-15,0), 20, 40));
  150 +
  151 + // Pivot Joints - Holds the two anchor points together. Like a swivel.
  152 + boxOffset = v(320, 0);
  153 + body1 = addBall(posA, boxOffset);
  154 + body2 = addBall(posB, boxOffset);
  155 + body2.setAngle(Math.PI);
  156 + // cp.PivotJoint(a, b, v) takes it's anchor parameter in world coordinates. The anchors are calculated from that
  157 + // Alternately, specify two anchor points using cp.PivotJoint(a, b, anch1, anch2)
  158 + space.addConstraint(new cp.PivotJoint(body1, body2, v.add(boxOffset, v(80,60))));
  159 +
  160 + // Groove Joints - Like a pivot joint, but one of the anchors is a line segment that the pivot can slide in
  161 + boxOffset = v(480, 0);
  162 + body1 = addBall(posA, boxOffset);
  163 + body2 = addBall(posB, boxOffset);
  164 + //space.addConstraint(cpGrooveJointNew(body1, body2, v(30,30), v(30,-30), v(-30,0)));
  165 +
  166 + // Damped Springs
  167 + boxOffset = v(0, 120);
  168 + body1 = addBall(posA, boxOffset);
  169 + body2 = addBall(posB, boxOffset);
  170 + //space.addConstraint(cpDampedSpringNew(body1, body2, v(15,0), v(-15,0), 20, 5, 0.3f));
  171 +
  172 + // Damped Rotary Springs
  173 + boxOffset = v(160, 120);
  174 + body1 = addBar(posA, boxOffset);
  175 + body2 = addBar(posB, boxOffset);
  176 + // Add some pin joints to hold the circles in place.
  177 + space.addConstraint(new cp.PivotJoint(body1, staticBody, POS_A()));
  178 + space.addConstraint(new cp.PivotJoint(body2, staticBody, POS_B()));
  179 + //space.addConstraint(cpDampedRotarySpringNew(body1, body2, 0, 3000, 60));
  180 +
  181 + // Rotary Limit Joint
  182 + boxOffset = v(320, 120);
  183 + body1 = addLever(posA, boxOffset);
  184 + body2 = addLever(posB, boxOffset);
  185 + // Add some pin joints to hold the circles in place.
  186 + space.addConstraint(new cp.PivotJoint(body1, staticBody, POS_A()));
  187 + space.addConstraint(new cp.PivotJoint(body2, staticBody, POS_B()));
  188 + // Hold their rotation within 90 degrees of each other.
  189 + //space.addConstraint(cpRotaryLimitJointNew(body1, body2, -M_PI_2, M_PI_2));
  190 +
  191 + // Ratchet Joint - A rotary ratchet, like a socket wrench
  192 + boxOffset = v(480, 120);
  193 + body1 = addLever(posA, boxOffset);
  194 + body2 = addLever(posB, boxOffset);
  195 + // Add some pin joints to hold the circles in place.
  196 + space.addConstraint(new cp.PivotJoint(body1, staticBody, POS_A()));
  197 + space.addConstraint(new cp.PivotJoint(body2, staticBody, POS_B()));
  198 + // Ratchet every 90 degrees
  199 + //space.addConstraint(cpRatchetJointNew(body1, body2, 0, M_PI_2));
  200 +
  201 + // Gear Joint - Maintain a specific angular velocity ratio
  202 + boxOffset = v(0, 240);
  203 + body1 = addBar(posA, boxOffset);
  204 + body2 = addBar(posB, boxOffset);
  205 + // Add some pin joints to hold the circles in place.
  206 + space.addConstraint(new cp.PivotJoint(body1, staticBody, POS_A()));
  207 + space.addConstraint(new cp.PivotJoint(body2, staticBody, POS_B()));
  208 + // Force one to sping 2x as fast as the other
  209 + //space.addConstraint(cpGearJointNew(body1, body2, 0, 2));
  210 +
  211 + // Simple Motor - Maintain a specific angular relative velocity
  212 + boxOffset = v(160, 240);
  213 + body1 = addBar(posA, boxOffset);
  214 + body2 = addBar(posB, boxOffset);
  215 + // Add some pin joints to hold the circles in place.
  216 + space.addConstraint(new cp.PivotJoint(body1, staticBody, POS_A()));
  217 + space.addConstraint(new cp.PivotJoint(body2, staticBody, POS_B()));
  218 + // Make them spin at 1/2 revolution per second in relation to each other.
  219 + //space.addConstraint(cpSimpleMotorNew(body1, body2, M_PI));
  220 +
  221 + // Make a car with some nice soft suspension
  222 + boxOffset = v(320, 240);
  223 + var wheel1 = addWheel(posA, boxOffset);
  224 + var wheel2 = addWheel(posB, boxOffset);
  225 + var chassis = addChassis(v(80, 100), boxOffset);
  226 +
  227 + //space.addConstraint(cpGrooveJointNew(chassis, wheel1, v(-30, -10), v(-30, -40), v(0,0)));
  228 + //space.addConstraint(cpGrooveJointNew(chassis, wheel2, v( 30, -10), v( 30, -40), v(0,0)));
  229 +
  230 + //space.addConstraint(cpDampedSpringNew(chassis, wheel1, v(-30, 0), v(0,0), 50, 20, 10));
  231 + //space.addConstraint(cpDampedSpringNew(chassis, wheel2, v( 30, 0), v(0,0), 50, 20, 10));
  232 +
  233 +
  234 +};
  235 +
  236 +Joints.prototype = Object.create(Demo.prototype);
  237 +
  238 +addDemo('Joints', Joints);
  239 +

0 comments on commit 2fcdb06

Please sign in to comment.
Something went wrong with that request. Please try again.