Skip to content

Instantly share code, notes, and snippets.

@nanonull
Created January 5, 2016 22:29
Show Gist options
  • Save nanonull/edb4b1ef5e2f352adbdc to your computer and use it in GitHub Desktop.
Save nanonull/edb4b1ef5e2f352adbdc to your computer and use it in GitHub Desktop.
protected void process(Entity e) {
// TODO if baseBody was destroyed then disable rotation
StationaryGunComponent gunComponent = components.get(e);
Box2dBodyComponent bodyComponent = Box2dBodySystem.components.get(e);
float expectedWorldAngle = gunComponent.getExpectedWorldAngleRads();
float currWorldAngle = bodyComponent.body.getAngle();
float angleDelta = MathUtilsExt.diffAbsoluteRadians(currWorldAngle, expectedWorldAngle);
float absDelta = Math.abs(angleDelta);
float angleStep;
if (absDelta > ROTATE_STEP) {
angleStep = angleDelta / absDelta * ROTATE_STEP;
} else {
angleStep = angleDelta;
}
float rotationBalance = gunComponent.getRotationBalance();
float newRotationBalance = rotationBalance + angleStep;
if (newRotationBalance < 0 || newRotationBalance > gunComponent.getRangeRadians()) {
// TODO rotate to bound
return;
}
gunComponent.setRotationBalance(newRotationBalance);
Vector2 position = bodyComponent.body.getPosition();
bodyComponent.body.setTransform(position.x, position.y, currWorldAngle + angleStep);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment