public void move(float forceX, float forceY, float forceZ) {
this.velocity.add(forceX, forceY, forceZ);
}
public void rotate(float rotX, float rotY, float rotZ) {
Vector v = new Vector();
v.x = rotX;
v.y = rotY + Common.sin(this.location.pitch) * rotZ;
v.z = Common.cos(this.location.pitch) * rotZ;
this.rotation.add(v);
}
public void push(Vector location, Vector force, float DiceHeight) {
location = location.clone();
location.subtract(this.location.getPosition());
location.rotate(new Vector(-this.location.yaw, 0, 0));
float forcefactor = 150 * (DiceHeight - 1);
float constantforce = 0.0f;
float pitch = 0;
float roll = 0;
float max = 0.48f;
float min = 0.001f;
if (location.x < -min && location.x > -max) {
roll = forcefactor * (force.y + constantforce);
} else if (location.x > min && location.x < max) {
roll = -forcefactor * (force.y + constantforce);
}
if (location.z < -min && location.z > -max) {
pitch = forcefactor * (force.y + constantforce);
} else if (location.z > min && location.z < max) {
pitch = -forcefactor * (force.y + constantforce);
}
this.rotate(0, pitch, roll);
this.move(force.x, force.y, force.z);
}
private void doCollisionCheck() {
java.util.Vector<Vector> collidedpoints = new java.util.Vector<Vector>();
float lowestpoint = 1;
float highestpoint = 0;
for (Vector v : geometry.getRelativePoints(this.location)) {
//Check for each point of this box if it pierces (is inside) another box
//Check if any points pierce the terrain
if (v.y <= 0) {
collidedpoints.add(v);
}
if (v.y < lowestpoint) lowestpoint = v.y;
if (v.y > highestpoint) highestpoint = v.y;
}
if (collidedpoints.size() != 0) {
Vector offsetcenter = Vector.getCenter(collidedpoints);
//float fallforce = -0.6f * this.velocity.y;
this.location.y -= lowestpoint;
float fallforce = 0.5f * (topheight - this.location.y);
fallforce += 0.09f * (highestpoint - lowestpoint);
topheight = this.location.y;
this.velocity.y = 0;
this.push(offsetcenter, new Vector(this.velocity.x * -0.1f, fallforce, this.velocity.z * -0.1f), highestpoint - lowestpoint);
if (fallforce > 0.2f) Resources.s_collide.play(this.location.getPosition());
}
}
public void doLogic(float timefactor) {
//Do collision detection before or after velocity and location update?
//======================Velocity and location update================
acceleration.y = -9.81f;
velocity.x += acceleration.x * timefactor;
velocity.y += acceleration.y * timefactor;
velocity.z += acceleration.z * timefactor;
//Air friction move factor
velocity.x -= 0.2f * velocity.x * timefactor;
velocity.y -= 0.2f * velocity.y * timefactor;
velocity.z -= 0.2f * velocity.z * timefactor;
//Air friction rotation factor
rotation.x -= 0.8f * rotation.x * timefactor;
rotation.y -= 0.8f * rotation.y * timefactor;
rotation.z -= 0.8f * rotation.z * timefactor;
location.x += velocity.x * timefactor;
location.y += velocity.y * timefactor;
location.z += velocity.z * timefactor;
location.yaw += rotation.x * timefactor;
location.pitch += rotation.y * timefactor;
location.roll += rotation.z * timefactor;
if (location.y > topheight) topheight = location.y;
//=======================================================================
doCollisionCheck();
}