/** Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the * angular velocity. This wakes up the body. * @param forceX the world force vector on x, usually in Newtons (N). * @param forceY the world force vector on y, usually in Newtons (N). * @param pointX the world position of the point of application on x. * @param pointY the world position of the point of application on y. */ public void applyForce (float forceX, float forceY, float pointX, float pointY, boolean wake) { tmp.set(forceX, forceY); tmp2.set(pointX, pointY); body.applyForce(tmp, tmp2); }
/** Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the * angular velocity. This wakes up the body. * @param force the world force vector, usually in Newtons (N). * @param point the world position of the point of application. */ public void applyForce (Vector2 force, Vector2 point, boolean wrap) { tmp.set(force.x, force.y); tmp2.set(point.x, point.y); body.applyForce(tmp, tmp2); }
@Override public void step(TestbedSettings settings) { // TODO Auto-generated method stub super.step(settings); // Traverse the contact results. Apply a force on shapes // that overlap the sensor. for (int i = 0; i < e_count; ++i) { if (m_touching[i].tf == false) { continue; } Body body = m_bodies[i]; Body ground = m_sensor.getBody(); CircleShape circle = (CircleShape) m_sensor.getShape(); Vec2 center = ground.getWorldPoint(circle.m_p); Vec2 position = body.getPosition(); Vec2 d = center.sub(position); if (d.lengthSquared() < Settings.EPSILON * Settings.EPSILON) { continue; } d.normalize(); Vec2 F = d.mulLocal(100f); body.applyForce(F, position); } }
@Override public void step(TestbedSettings settings) { super.step(settings); addTextLine("Use 'wasd' to move, 'e' and 's' drift."); if (getModel().getKeys()['w']) { Vec2 f = m_body.getWorldVector(new Vec2(0.0f, -30.0f)); Vec2 p = m_body.getWorldPoint(m_body.getLocalCenter().add(new Vec2(0.0f, 2.0f))); m_body.applyForce(f, p); } else if (getModel().getKeys()['q']) { Vec2 f = m_body.getWorldVector(new Vec2(0.0f, -30.0f)); Vec2 p = m_body.getWorldPoint(m_body.getLocalCenter().add(new Vec2(-.2f, 0f))); m_body.applyForce(f, p); } else if (getModel().getKeys()['e']) { Vec2 f = m_body.getWorldVector(new Vec2(0.0f, -30.0f)); Vec2 p = m_body.getWorldPoint(m_body.getLocalCenter().add(new Vec2(.2f, 0f))); m_body.applyForce(f, p); } else if (getModel().getKeys()['s']) { Vec2 f = m_body.getWorldVector(new Vec2(0.0f, 30.0f)); Vec2 p = m_body.getWorldCenter(); m_body.applyForce(f, p); } if (getModel().getKeys()['a']) { m_body.applyTorque(20.0f); } if (getModel().getKeys()['d']) { m_body.applyTorque(-20.0f); } }
void applyForce(GameObjectInstance aInstance, Force aForce) { Body theBody = dynamicObjects.get(aInstance); if (theBody != null) { theBody.applyForce(new Vec2(aForce.forceX, aForce.forceY), theBody.getWorldCenter()); } }
body.applyForce(new Vector2(0,-SensorManager.GRAVITY_EARTH), new Vector2(body.getWorldCenter())); if(vr<destroyerVelocity){ if(vx>0&&vy<0) body.applyForce((float) ((destroyerVelocity-vr)*Math.cos(t)*body.getMass()), (float) (-(destroyerVelocity-vr)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y); else if(vx>0&&vy>0) body.applyForce((float) ((destroyerVelocity-vr)*Math.cos(t)*body.getMass()), (float) ((destroyerVelocity-vr)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y); else if(vx<0&&vy>0) body.applyForce((float) (-(destroyerVelocity-vr)*Math.cos(t)*body.getMass()), (float) ((destroyerVelocity-vr)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y); else if(vx<0&&vy<0) body.applyForce((float) (-(destroyerVelocity-vr)*Math.cos(t)*body.getMass()), (float) (-(destroyerVelocity-vr)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y); body.applyForce((float) (-(vr-destroyerVelocity)*Math.cos(t)*body.getMass()), (float) ((vr-destroyerVelocity)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y); else if(vx>0&&vy>0) body.applyForce((float) (-(vr-destroyerVelocity)*Math.cos(t)*body.getMass()), (float) (-(vr-destroyerVelocity)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y); else if(vx<0&&vy>0) body.applyForce((float) ((vr-destroyerVelocity)*Math.cos(t)*body.getMass()), (float) (-(vr-destroyerVelocity)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y); else if(vx<0&&vy<0) body.applyForce((float) ((vr-destroyerVelocity)*Math.cos(t)*body.getMass()), (float) ((vr-destroyerVelocity)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y);
Body body, centerBody; Vector2 center = new Vector2(0, 0); ... //in render method float G = 1; //modifier of gravity value - you can make it bigger to have stronger gravity float distance = body.getPosition().dst( center ); float forceValue = G / (distance * distance); Vector2 direction = center.sub( body.getPosition() ) ); body.applyForce( direction.scl( forceValue ), body.getWorldCenter() );
@Override public void step(TestbedSettings settings) { // TODO Auto-generated method stub super.step(settings); // Traverse the contact results. Apply a force on shapes // that overlap the sensor. for (int i = 0; i < e_count; ++i) { if (m_touching[i].tf == false) { continue; } Body body = m_bodies[i]; Body ground = m_sensor.getBody(); CircleShape circle = (CircleShape) m_sensor.getShape(); Vec2 center = ground.getWorldPoint(circle.m_p); Vec2 position = body.getPosition(); Vec2 d = center.sub(position); if (d.lengthSquared() < Settings.EPSILON * Settings.EPSILON) { continue; } d.normalize(); Vec2 F = d.mulLocal(100f); body.applyForce(F, position); } }
@Override public void step(TestbedSettings settings) { super.step(settings); addTextLine("Use 'wasd' to move, 'e' and 's' drift."); if (getModel().getKeys()['w']) { Vec2 f = m_body.getWorldVector(new Vec2(0.0f, -30.0f)); Vec2 p = m_body.getWorldPoint(m_body.getLocalCenter().add(new Vec2(0.0f, 2.0f))); m_body.applyForce(f, p); } else if (getModel().getKeys()['q']) { Vec2 f = m_body.getWorldVector(new Vec2(0.0f, -30.0f)); Vec2 p = m_body.getWorldPoint(m_body.getLocalCenter().add(new Vec2(-.2f, 0f))); m_body.applyForce(f, p); } else if (getModel().getKeys()['e']) { Vec2 f = m_body.getWorldVector(new Vec2(0.0f, -30.0f)); Vec2 p = m_body.getWorldPoint(m_body.getLocalCenter().add(new Vec2(.2f, 0f))); m_body.applyForce(f, p); } else if (getModel().getKeys()['s']) { Vec2 f = m_body.getWorldVector(new Vec2(0.0f, 30.0f)); Vec2 p = m_body.getWorldCenter(); m_body.applyForce(f, p); } if (getModel().getKeys()['a']) { m_body.applyTorque(20.0f); } if (getModel().getKeys()['d']) { m_body.applyTorque(-20.0f); } }