Skip to content

Commit

Permalink
More physics work
Browse files Browse the repository at this point in the history
  • Loading branch information
kitskub committed Sep 27, 2014
1 parent 1f665cc commit da1d01b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/flowpowered/engine/entity/FlowEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ public WorldReference getWorld() {
void finalizeRun() {
FlowWorld worldLive = (FlowWorld) getWorld().get();
FlowWorld worldSnapshot = (FlowWorld) physics.getSnapshottedTransform().getPosition().getWorld().get();
//Move entity from Region A to Region B
//Move entity from World A to World B
if (worldSnapshot != worldLive) {
worldSnapshot.getEntityManager().removeEntity(this);
//Add entity to Region B
//Add entity to World B
worldLive.getEntityManager().addEntity(this);
physics.crossInto(worldSnapshot, worldLive);
}
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/flowpowered/engine/entity/FlowPhysics.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,20 @@ public FlowPhysics activate(final float mass, final CollisionShape shape, Dynami
deactivate();
Transform transform = live.get();
body = sim.createRigidBody(new org.spout.physics.math.Transform(ReactConverter.toReactVector3(transform.getPosition().getVector()), new Quaternion(0, 0, 0, 1)), mass, shape);
body.setMaterial(new Material());
Material mat = new Material();
mat.setBounciness(0f);
mat.setFrictionCoefficient(0f);
body.setMaterial(mat);
activated = true;
return this;
}

public void crossInto(final FlowWorld from, final FlowWorld to) {
if (entity != null && from != null && body != null) {
from.getPhysicsManager().queuePreUpdateTask((w) -> w.destroyRigidBody(body));
Material mat = body.getMaterial();
body = to.getPhysicsManager().getSimulation().createRigidBody(body.getTransform(), body.getMass(), body.getCollisionShape());
body.setMaterial(mat);
}
}

Expand Down Expand Up @@ -272,8 +277,11 @@ public void onPrePhysicsTick() {
if (body == null) {
return;
}
final org.spout.physics.math.Vector3 positionLiveToReact = ReactConverter.toReactVector3(live.get().getPosition().getVector());
body.getTransform().setPosition(positionLiveToReact);
org.spout.physics.math.Transform toReact = ReactConverter.toReactTransform(live.get());
if (!body.getTransform().equals(toReact)) {
body.setIsSleeping(false);
}
body.getTransform().set(toReact);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
import com.flowpowered.math.GenericMath;
import com.flowpowered.math.vector.Vector3f;
import gnu.trove.map.hash.TShortObjectHashMap;
import org.spout.physics.ReactDefaults;
import org.spout.physics.body.RigidBody;
import org.spout.physics.collision.shape.BoxShape;
import org.spout.physics.engine.Material;
import org.spout.physics.math.Quaternion;
import org.spout.physics.math.Transform;
import org.spout.physics.math.Vector3;
Expand Down Expand Up @@ -83,11 +85,12 @@ public FlowChunk(FlowRegion region, int x, int y, int z, int generationIndex, At
if (y < 0) {
((FlowWorld) region.getWorld().get()).getPhysicsManager().queuePreUpdateTask((w) -> {
RigidBody b = w.createRigidBody(
new Transform(ReactConverter.toReactVector3(getBlockX(), getBlockY(), getBlockZ()), Quaternion.identity()),
new Transform(ReactConverter.toReactVector3(getBlockX() + 8, getBlockY() + 8, getBlockZ() + 8), Quaternion.identity()),
1f,
new BoxShape(new Vector3(8f, 8f, 8f))
);
b.enableMotion(false);
b.setMaterial(new Material(0, ReactDefaults.DEFAULT_FRICTION_COEFFICIENT));
});
}
}
Expand Down

0 comments on commit da1d01b

Please sign in to comment.