PageRenderTime 26ms CodeModel.GetById 23ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://jbox2d.googlecode.com/
Java | 80 lines | 65 code | 15 blank | 0 comment | 1 complexity | b1896bd22ecc4def3b601a4b3bbc55ae MD5 | raw file
Possible License(s): BSD-3-Clause
 1
 2package org.jbox2d.testbed.tests;
 3
 4import org.jbox2d.collision.shapes.CircleShape;
 5import org.jbox2d.collision.shapes.PolygonShape;
 6import org.jbox2d.collision.shapes.Shape;
 7import org.jbox2d.common.Vec2;
 8import org.jbox2d.dynamics.Body;
 9import org.jbox2d.dynamics.BodyDef;
10import org.jbox2d.dynamics.BodyType;
11import org.jbox2d.dynamics.joints.RevoluteJointDef;
12import org.jbox2d.testbed.framework.TestbedTest;
13
14public class FreePendulumTest extends TestbedTest
15{
16   private final boolean switchBodiesInJoint;
17
18   public FreePendulumTest(boolean switchBodiesInJoint)
19   {
20      this.switchBodiesInJoint = switchBodiesInJoint;
21   }
22   
23   @Override
24   public void initTest(boolean argDeserialized)
25   {
26      Body pendulum;
27      Body base;
28      Body ground;
29
30      {
31         CircleShape circleShape = new CircleShape();
32         circleShape.m_radius = 1;
33         Shape shape = circleShape;
34   
35         BodyDef bodyDef = new BodyDef();
36         bodyDef.type = BodyType.DYNAMIC;
37         bodyDef.position.set(-5, 0);
38         bodyDef.allowSleep = false;
39         pendulum = getWorld().createBody(bodyDef);
40         pendulum.createFixture(shape, 1);
41      }
42      
43      {
44         PolygonShape shape = new PolygonShape();
45         shape.setAsBox(1, 1);
46         
47         BodyDef bodyDef = new BodyDef();
48         bodyDef.type = BodyType.DYNAMIC;
49         bodyDef.position.set(0, 2);
50         bodyDef.allowSleep = false;
51         base = getWorld().createBody(bodyDef);
52         base.createFixture(shape, 1);
53      }
54      
55      {
56         PolygonShape shape = new PolygonShape();
57         shape.setAsBox(3, 1);
58         
59         BodyDef bodyDef = new BodyDef();
60         bodyDef.type = BodyType.STATIC;
61         ground = getWorld().createBody(bodyDef);
62         ground.createFixture(shape, 0);
63      }
64      
65      RevoluteJointDef jointDef = new RevoluteJointDef();
66      
67      if (switchBodiesInJoint)
68         jointDef.initialize(pendulum, base, new Vec2(0, 0));
69      else
70         jointDef.initialize(base, pendulum, new Vec2(0, 0));
71      
72      getWorld().createJoint(jointDef);
73   }
74
75   @Override
76   public String getTestName()
77   {
78      return "Free Pendulum " + (switchBodiesInJoint ? "1" : "0");
79   }
80}