Added player control example base
This commit is contained in:
@@ -70,28 +70,28 @@ public final class App {
|
||||
// Bottom boundary
|
||||
Entity newEnt = gameEngine.createEntity();
|
||||
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(-50., -50.,0));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0., -1.));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0.25, -1.));
|
||||
gameEngine.addComponent(newEnt, Collider.class, new Collider(200., 50.)); // 200 along, 50 up
|
||||
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
|
||||
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(200, 50, 64, 64, 64));
|
||||
// Left boundary
|
||||
newEnt = gameEngine.createEntity();
|
||||
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(-50., -50.,0));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0., -1.));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0.25, -1.));
|
||||
gameEngine.addComponent(newEnt, Collider.class, new Collider(50., 200.));
|
||||
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
|
||||
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(50, 200, 64, 64, 64));
|
||||
// Top boundary
|
||||
newEnt = gameEngine.createEntity();
|
||||
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(-50., 100.,0));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0., -1.));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0.25, -1.));
|
||||
gameEngine.addComponent(newEnt, Collider.class, new Collider(200., 50.));
|
||||
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
|
||||
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(200, 50, 64, 64, 64));
|
||||
// Right boundary
|
||||
newEnt = gameEngine.createEntity();
|
||||
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(100., -50.,0));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0., -1.));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0.25, -1.));
|
||||
gameEngine.addComponent(newEnt, Collider.class, new Collider(50., 200.));
|
||||
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
|
||||
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(50, 200, 64, 64, 64));
|
||||
@@ -110,8 +110,8 @@ public final class App {
|
||||
gameEngine.addComponent(newEnt, Reporter.class, new Reporter());
|
||||
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(5,5,0));
|
||||
|
||||
/* // Uncomment this block to enable the stress-test (runs 500 square entities, with physics etc.)
|
||||
Random rand = new Random();
|
||||
// Uncomment this block to enable the stress-test (runs 500 square entities, with physics etc.)
|
||||
/*Random rand = new Random();
|
||||
for (int i = 0; i < 500; i++) {
|
||||
newEnt = gameEngine.createEntity();
|
||||
double size = 1 + rand.nextDouble();
|
||||
@@ -122,7 +122,7 @@ public final class App {
|
||||
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
|
||||
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(size, size, 30 + rand.nextInt(225), 30 + rand.nextInt(225), 30 + rand.nextInt(225)));
|
||||
gameEngine.addComponent(newEnt, TimedExistence.class, new TimedExistence(25 + rand.nextDouble() * 90));
|
||||
} */
|
||||
}*/
|
||||
|
||||
/************************************
|
||||
** Run init() on the systems **
|
||||
|
||||
@@ -42,9 +42,8 @@ public class CollisionSystem extends ECSSystem {
|
||||
Collider collider = (Collider) gameEngine.getComponentData(entity, Collider.class);
|
||||
if (ridgidBody.mass >= 0) {
|
||||
// Only ignore non-static colliders
|
||||
//processed.add(entity);
|
||||
for (Entity entity2 : entities) {
|
||||
if (!processed.contains(entity2)) {
|
||||
if (!processed.contains(entity2)) { // If this entity has been added, we've already calc'ed its forces completely
|
||||
// skip entities that have already been processed
|
||||
Vec2D otherPos = (Vec2D) gameEngine.getComponentData(entity2, Vec2D.class);
|
||||
RidgidBody otherRidgidBody = (RidgidBody) gameEngine.getComponentData(entity2,
|
||||
@@ -74,7 +73,7 @@ public class CollisionSystem extends ECSSystem {
|
||||
double totalYVel = Math.abs(ridgidBody.yVel) + Math.abs(otherRidgidBody.yVel);
|
||||
double xMult = 0;
|
||||
double yMult = 0;
|
||||
double multTot = (1 + ridgidBody.bounciness); // reflect a force back 100%, (srtabilises) then add any extra in bounce
|
||||
double multTot = 1; // reflect a force back 100%, (srtabilises) then add any extra in bounce
|
||||
double totalMass = ridgidBody.mass + otherRidgidBody.mass;
|
||||
boolean fullEdge = false;
|
||||
// left-hand collision
|
||||
@@ -90,7 +89,7 @@ public class CollisionSystem extends ECSSystem {
|
||||
xMult = -multTot;
|
||||
}
|
||||
// Bottom collision
|
||||
if (blCollide && brCollide) {
|
||||
else if (blCollide && brCollide) {
|
||||
fullEdge = true;
|
||||
//pos.y = otherPos.y + otherCollider.y;
|
||||
pos.y = otherPos.y - collider.y;
|
||||
@@ -127,12 +126,12 @@ public class CollisionSystem extends ECSSystem {
|
||||
}
|
||||
else if (deltaYVel > deltaXVel){
|
||||
// Top edge
|
||||
if (deltaYVel > deltaMin) pos.y = otherPos.y + otherCollider.y;
|
||||
//if (deltaYVel > deltaMin) pos.y = otherPos.y + otherCollider.y;
|
||||
yMult = multTot;
|
||||
}
|
||||
else{
|
||||
|
||||
if (deltaXVel > deltaMin) pos.x = otherPos.x + collider.x;
|
||||
//if (deltaXVel > deltaMin) pos.x = otherPos.x + collider.x;
|
||||
xMult = -multTot;
|
||||
}
|
||||
}
|
||||
@@ -150,11 +149,11 @@ public class CollisionSystem extends ECSSystem {
|
||||
}
|
||||
else if (deltaYVel < deltaXVel){
|
||||
//pos.y = otherPos.y + otherCollider.y;
|
||||
if (deltaYVel > deltaMin) pos.y = otherPos.y - collider.y;
|
||||
//if (deltaYVel > deltaMin) pos.y = otherPos.y - collider.y;
|
||||
yMult = -multTot;
|
||||
}
|
||||
else{
|
||||
if (deltaXVel > deltaMin) pos.x = otherPos.x - collider.x;
|
||||
//if (deltaXVel > deltaMin) pos.x = otherPos.x - collider.x;
|
||||
xMult = -multTot;
|
||||
}
|
||||
}
|
||||
@@ -171,11 +170,11 @@ public class CollisionSystem extends ECSSystem {
|
||||
}
|
||||
}
|
||||
else if (deltaYVel > deltaXVel){
|
||||
if (deltaYVel > deltaMin) pos.y = otherPos.y - collider.y;
|
||||
//if (deltaYVel > deltaMin) pos.y = otherPos.y - collider.y;
|
||||
yMult = -multTot;
|
||||
}
|
||||
else{
|
||||
if (deltaXVel > deltaMin) pos.x = otherPos.x + otherCollider.x;
|
||||
//if (deltaXVel > deltaMin) pos.x = otherPos.x + otherCollider.x;
|
||||
xMult = multTot;
|
||||
}
|
||||
}
|
||||
@@ -192,11 +191,11 @@ public class CollisionSystem extends ECSSystem {
|
||||
}
|
||||
}
|
||||
else if (deltaYVel > deltaXVel){
|
||||
if (deltaYVel > deltaMin) pos.y = otherPos.y +otherCollider.y;
|
||||
//if (deltaYVel > deltaMin) pos.y = otherPos.y +otherCollider.y;
|
||||
yMult = multTot;
|
||||
}
|
||||
else {
|
||||
if (deltaXVel > deltaMin) pos.x = otherPos.x + otherCollider.x;
|
||||
//if (deltaXVel > deltaMin) pos.x = otherPos.x + otherCollider.x;
|
||||
xMult = multTot;
|
||||
}
|
||||
}
|
||||
@@ -207,17 +206,16 @@ public class CollisionSystem extends ECSSystem {
|
||||
// entity 2 is immovable; full acceleration applied to the current entity
|
||||
|
||||
if (otherRidgidBody.mass >= 0){
|
||||
yMult *= otherRidgidBody.bounciness;
|
||||
xMult *= otherRidgidBody.bounciness;
|
||||
ridgidBody.xVel += totalXVel * xMult * ((totalMass - ridgidBody.mass) / totalMass);
|
||||
ridgidBody.yVel += totalYVel * yMult * ((totalMass - ridgidBody.mass) / totalMass);
|
||||
|
||||
otherRidgidBody.xVel -= totalXVel * xMult * ((totalMass - otherRidgidBody.mass) / totalMass);
|
||||
otherRidgidBody.yVel -= totalYVel * yMult * ((totalMass - otherRidgidBody.mass) / totalMass);
|
||||
ridgidBody.xVel += totalXVel * ((totalMass - ridgidBody.mass) / totalMass) * (xMult + (Math.abs(xMult) + (ridgidBody.bounciness * otherRidgidBody.bounciness)));
|
||||
ridgidBody.yVel += totalYVel * ((totalMass - ridgidBody.mass) / totalMass) * (yMult + (Math.abs(yMult) + (ridgidBody.bounciness * otherRidgidBody.bounciness)));
|
||||
otherRidgidBody.xVel -= totalXVel * ((totalMass - otherRidgidBody.mass) / totalMass) * (xMult + (Math.abs(xMult) + (ridgidBody.bounciness * otherRidgidBody.bounciness)));
|
||||
otherRidgidBody.yVel -= totalYVel * ((totalMass - otherRidgidBody.mass) / totalMass) * (yMult + (Math.abs(yMult) + (ridgidBody.bounciness * otherRidgidBody.bounciness)));
|
||||
}
|
||||
else{
|
||||
ridgidBody.xVel += totalXVel * xMult;
|
||||
ridgidBody.yVel += totalYVel * yMult;
|
||||
double newXVel = totalXVel * (xMult * (Math.abs(xMult) + ridgidBody.bounciness));
|
||||
double newYVel = totalYVel * (yMult * (Math.abs(yMult) + ridgidBody.bounciness));
|
||||
ridgidBody.xVel += newXVel;
|
||||
ridgidBody.yVel += newYVel;
|
||||
}
|
||||
Object grav = gameEngine.getComponentData(entity, Gravity.class);
|
||||
if (grav != null && otherRidgidBody.mass < 0) {
|
||||
@@ -231,6 +229,7 @@ public class CollisionSystem extends ECSSystem {
|
||||
}
|
||||
}
|
||||
}
|
||||
//processed.add(entity); // Add this entity to the already processed ones
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user