@Override public void init() { initDisplay(new GLDisplay(), new DisplayMode(), new PixelFormat(), new VideoSettings()); cam.setFlyCam(true); cam.translateTo(0f, 0f, 5); cam.rotateTo(0, 0); defaultshader2 = new Shader( ShaderLoader.loadShaderFromFile( "res/shaders/defaultshader.vert", "res/shaders/defaultshader.frag")); addShader2d(defaultshader2); space = new PhysicsSpace2( new EulerIntegration(), new SAP2(), new GJK2(new EPA2()), new LinearImpulseResolution(), new ProjectionCorrection(), new SimpleManifoldManager<Vector2f>()); space.setGlobalGravitation(new Vector2f(0, 120)); Quad ground = new Quad(400, 550, 300, 20); RigidBody2 rb = new RigidBody2(PhysicsShapeCreator.create(ground)); space.addRigidBody(ground, rb); defaultshader2.addObject(ground); Quad q = new Quad(100, 450, 10, 10); RigidBody2 rb1 = new RigidBody2(PhysicsShapeCreator.create(q)); rb1.setMass(1f); rb1.applyCentralImpulse(new Vector2f(100f, 0f)); space.addRigidBody(q, rb1); defaultshader2.addObject(q); }
@Override public void update(int delta) { // System.out.println(rb1.getLinearVelocity()); if (tempdelta > 200) { if (inputs.isMouseButtonDown("0")) { Quad q = new Quad(100, 450, 10, 10); RigidBody2 rb = new RigidBody2(PhysicsShapeCreator.create(q)); rb.setMass(1f); rb.applyCentralImpulse(new Vector2f(100f, 0f)); space.addRigidBody(q, rb); defaultshader2.addObject(q); tempdelta = 0; } if (inputs.isMouseButtonDown("1")) { Quad q = new Quad(700, 450, 10, 10); RigidBody2 rb = new RigidBody2(PhysicsShapeCreator.create(q)); rb.setMass(1f); rb.applyCentralImpulse(new Vector2f(-100f, 0f)); space.addRigidBody(q, rb); defaultshader2.addObject(q); tempdelta = 0; } } else { tempdelta += delta; } space.update(delta); cam.update(delta); }
@Override public void resolve2(CollisionManifold<Vector2f> manifold) { RigidBody2 A = (RigidBody2) manifold.getObjects().getFirst(); RigidBody2 B = (RigidBody2) manifold.getObjects().getSecond(); Vector2f normal = manifold.getCollisionNormal(); // velAlongNormal = (B - A) dot normal float velAlongNormal = (B.getLinearVelocity().x - A.getLinearVelocity().x) * normal.x + (B.getLinearVelocity().y - A.getLinearVelocity().y) * normal.y; if (velAlongNormal > 0) return; float e = Math.min(A.getRestitution(), B.getRestitution()); float j = (-(1 + e) * velAlongNormal) / (A.getInverseMass() + B.getInverseMass()); Vector2f impulse = VecMath.scale(normal, j); B.applyCentralImpulse(impulse); impulse.negate(); A.applyCentralImpulse(impulse); }