Skip to content

Instantly share code, notes, and snippets.

@najmam
Created October 18, 2014 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save najmam/57465c6423f5965f6416 to your computer and use it in GitHub Desktop.
Save najmam/57465c6423f5965f6416 to your computer and use it in GitHub Desktop.
Integrating gdx-ai's steering behaviours with Ashley and Box2D
// apply steering behaviors
ImmutableArray<Entity> entities = engine.getEntitiesFor(Family.getFor(SteerAgentComponent.class));
for(int i = 0; i < entities.size(); i++) {
Entity e = entities.get(i);
Body body = e.getComponent(PhysicalBodyComponent.class).body;
SteerAgentComponent steer = e.getComponent(SteerAgentComponent.class);
if(steer.steeringBehavior == null) {
continue;
}
steer.steeringBehavior.calculateSteering(steer.steeringOutput);
Vector2 linear_force = steer.steeringOutput.linear.cpy();
linear_force.scl(body.getMass());
body.applyForceToCenter(linear_force, true);
body.applyTorque(steer.steeringOutput.angular*body.getInertia(), true);
}
// step the simulation
world.step(Constants.PHYSICS_TIME_STEP, Constants.PHYSICS_VELOCITY_ITERATIONS, Constants.PHYSICS_POSITION_ITERATIONS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment