PageRenderTime 31ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 0ms app.codeStats 0ms

/jbox2d-testbed/src/main/java/org/jbox2d/testbed/tests/PistonTest.java

http://jbox2d.googlecode.com/
Java | 209 lines | 171 code | 33 blank | 5 comment | 4 complexity | a8660aa34a7d09e88d9e86957c7a4361 MD5 | raw file
Possible License(s): BSD-3-Clause
  1package org.jbox2d.testbed.tests;
  2
  3import org.jbox2d.collision.shapes.CircleShape;
  4import org.jbox2d.collision.shapes.PolygonShape;
  5import org.jbox2d.common.MathUtils;
  6import org.jbox2d.common.Vec2;
  7import org.jbox2d.dynamics.Body;
  8import org.jbox2d.dynamics.BodyDef;
  9import org.jbox2d.dynamics.BodyType;
 10import org.jbox2d.dynamics.FixtureDef;
 11import org.jbox2d.dynamics.World;
 12import org.jbox2d.dynamics.joints.PrismaticJoint;
 13import org.jbox2d.dynamics.joints.PrismaticJointDef;
 14import org.jbox2d.dynamics.joints.RevoluteJoint;
 15import org.jbox2d.dynamics.joints.RevoluteJointDef;
 16import org.jbox2d.testbed.framework.TestbedTest;
 17
 18public class PistonTest extends TestbedTest {
 19
 20  public RevoluteJoint m_joint1;
 21  public PrismaticJoint m_joint2;
 22
 23  private boolean bullet = false;
 24
 25  @Override
 26  public boolean isSaveLoadEnabled() {
 27    return true;
 28  }
 29
 30  @Override
 31  public void initTest(boolean argDeserialized) {
 32    if (argDeserialized) {
 33      return;
 34    }
 35    World world = getWorld();
 36    Body ground = null;
 37    {
 38      BodyDef bd = new BodyDef();
 39      ground = getWorld().createBody(bd);
 40
 41      PolygonShape shape = new PolygonShape();
 42      shape.setAsBox(5.0f, 100.0f);
 43      bd = new BodyDef();
 44      bd.type = BodyType.STATIC;
 45      FixtureDef sides = new FixtureDef();
 46      sides.shape = shape;
 47      sides.density = 0;
 48      sides.friction = 0;
 49      sides.restitution = .8f;
 50      sides.filter.categoryBits = 4;
 51      sides.filter.maskBits = 2;
 52
 53      bd.position.set(-10.01f, 50.0f);
 54      Body bod = world.createBody(bd);
 55      bod.createFixture(sides);
 56      bd.position.set(10.01f, 50.0f);
 57      bod = world.createBody(bd);
 58      bod.createFixture(sides);
 59    }
 60
 61    // turney
 62    {
 63      CircleShape cd;
 64      FixtureDef fd = new FixtureDef();
 65      BodyDef bd = new BodyDef();
 66      bd.type = BodyType.DYNAMIC;
 67      int numPieces = 5;
 68      float radius = 4f;
 69      bd.position = new Vec2(0.0f, 25.0f);
 70      Body body = getWorld().createBody(bd);
 71      for (int i = 0; i < numPieces; i++) {
 72        cd = new CircleShape();
 73        cd.m_radius = .5f;
 74        fd.shape = cd;
 75        fd.density = 25;
 76        fd.friction = .1f;
 77        fd.restitution = .9f;
 78        float xPos = radius * (float) Math.cos(2f * Math.PI * (i / (float) (numPieces)));
 79        float yPos = radius * (float) Math.sin(2f * Math.PI * (i / (float) (numPieces)));
 80        cd.m_p.set(xPos, yPos);
 81
 82        body.createFixture(fd);
 83      }
 84
 85      body.setBullet(false);
 86
 87      RevoluteJointDef rjd = new RevoluteJointDef();
 88      rjd.initialize(body, getGroundBody(), body.getPosition());
 89      rjd.motorSpeed = MathUtils.PI;
 90      rjd.maxMotorTorque = 1000000.0f;
 91      rjd.enableMotor = true;
 92      RevoluteJoint joint = (RevoluteJoint) getWorld().createJoint(rjd);
 93    }
 94
 95
 96    {
 97      Body prevBody = ground;
 98
 99      // Define crank.
100      {
101        PolygonShape shape = new PolygonShape();
102        shape.setAsBox(0.5f, 2.0f);
103
104        BodyDef bd = new BodyDef();
105        bd.type = BodyType.DYNAMIC;
106        bd.position.set(0.0f, 7.0f);
107        Body body = getWorld().createBody(bd);
108        body.createFixture(shape, 2.0f);
109
110        RevoluteJointDef rjd = new RevoluteJointDef();
111        rjd.initialize(prevBody, body, new Vec2(0.0f, 5.0f));
112        rjd.motorSpeed = 1.0f * MathUtils.PI;
113        rjd.maxMotorTorque = 20000;
114        rjd.enableMotor = true;
115        m_joint1 = (RevoluteJoint) getWorld().createJoint(rjd);
116
117        prevBody = body;
118      }
119
120      // Define follower.
121      {
122        PolygonShape shape = new PolygonShape();
123        shape.setAsBox(0.5f, 4.0f);
124
125        BodyDef bd = new BodyDef();
126        bd.type = BodyType.DYNAMIC;
127        bd.position.set(0.0f, 13.0f);
128        Body body = getWorld().createBody(bd);
129        body.createFixture(shape, 2.0f);
130
131        RevoluteJointDef rjd = new RevoluteJointDef();
132        rjd.initialize(prevBody, body, new Vec2(0.0f, 9.0f));
133        rjd.enableMotor = false;
134        getWorld().createJoint(rjd);
135
136        prevBody = body;
137      }
138
139      // Define piston
140      {
141        PolygonShape shape = new PolygonShape();
142        shape.setAsBox(7f, 2f);
143
144        BodyDef bd = new BodyDef();
145        bd.type = BodyType.DYNAMIC;
146        bd.position.set(0.0f, 17.0f);
147        Body body = getWorld().createBody(bd);
148        FixtureDef piston = new FixtureDef();
149        piston.shape = shape;
150        piston.density = 2;
151        piston.filter.categoryBits = 1;
152        piston.filter.maskBits = 2;
153        body.createFixture(piston);
154
155        RevoluteJointDef rjd = new RevoluteJointDef();
156        rjd.initialize(prevBody, body, new Vec2(0.0f, 17.0f));
157        getWorld().createJoint(rjd);
158
159        PrismaticJointDef pjd = new PrismaticJointDef();
160        pjd.initialize(ground, body, new Vec2(0.0f, 17.0f), new Vec2(0.0f, 1.0f));
161
162        pjd.maxMotorForce = 1000.0f;
163        pjd.enableMotor = true;
164
165        m_joint2 = (PrismaticJoint) getWorld().createJoint(pjd);
166      }
167
168      // Create a payload
169      {
170        PolygonShape sd = new PolygonShape();
171        BodyDef bd = new BodyDef();
172        bd.type = BodyType.DYNAMIC;
173        FixtureDef fixture = new FixtureDef();
174        Body body;
175        for (int i = 0; i < 100; ++i) {
176          sd.setAsBox(0.4f, 0.3f);
177          bd.position.set(-1.0f, 23.0f + i);
178
179          bd.bullet = bullet;
180          body = world.createBody(bd);
181          fixture.shape = sd;
182          fixture.density = .1f;
183          fixture.filter.categoryBits = 2;
184          fixture.filter.maskBits = 1 | 4 | 2;
185          body.createFixture(fixture);
186        }
187
188        CircleShape cd = new CircleShape();
189        cd.m_radius = 0.36f;
190        for (int i = 0; i < 100; ++i) {
191          bd.position.set(1.0f, 23.0f + i);
192          bd.bullet = bullet;
193          fixture.shape = cd;
194          fixture.density = 2f;
195          fixture.filter.categoryBits = 2;
196          fixture.filter.maskBits = 1 | 4 | 2;
197          body = world.createBody(bd);
198          body.createFixture(fixture);
199        }
200      }
201    }
202
203  }
204
205  @Override
206  public String getTestName() {
207    return "Piston Stress Test";
208  }
209}