Compare commits
2 Commits
4d76428553
...
294c0fc52f
Author | SHA1 | Date | |
---|---|---|---|
294c0fc52f | |||
0db1abcbdf |
@ -1,10 +1,8 @@
|
||||
package nz.ac.massey.javaecs.examples;
|
||||
|
||||
import java.sql.Time;
|
||||
import java.util.Random;
|
||||
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.text.Text;
|
||||
import nz.ac.massey.javaecs.*;
|
||||
import nz.ac.massey.javaecs.examples.Components.*;
|
||||
import nz.ac.massey.javaecs.examples.Systems.*;
|
||||
@ -22,8 +20,14 @@ public final class App {
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
/************************************
|
||||
** Initialise engine **
|
||||
*************************************/
|
||||
Engine gameEngine = new Engine(16384);
|
||||
|
||||
/************************************
|
||||
** Register game components **
|
||||
*************************************/
|
||||
gameEngine.registerComponent(Gravity.class);
|
||||
gameEngine.registerComponent(RidgidBody.class);
|
||||
gameEngine.registerComponent(Vec2D.class);
|
||||
@ -35,6 +39,9 @@ public final class App {
|
||||
gameEngine.registerComponent(Reporter.class);
|
||||
gameEngine.registerComponent(TextRender.class);
|
||||
|
||||
/************************************
|
||||
** Register and initialise systems **
|
||||
*************************************/
|
||||
PhysicsSystem physicsSystem = new PhysicsSystem(gameEngine);
|
||||
gameEngine.registerSystem(PhysicsSystem.class, physicsSystem);
|
||||
|
||||
@ -54,6 +61,11 @@ public final class App {
|
||||
ReportSystem reportSystem = new ReportSystem(gameEngine);
|
||||
gameEngine.registerSystem(ReportSystem.class, reportSystem);
|
||||
|
||||
|
||||
|
||||
/************************************
|
||||
** Create known entities **
|
||||
*************************************/
|
||||
// Define a play area 100 * 100, make sure to use worldspace not screenspace (top left is 0, 100, not 0,0)
|
||||
// Bottom boundary
|
||||
Entity newEnt = gameEngine.createEntity();
|
||||
@ -86,35 +98,35 @@ public final class App {
|
||||
// One object
|
||||
newEnt = gameEngine.createEntity();
|
||||
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(62., 90.,0));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(1., 0., 0., 0., 0.f, 0., 1.));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(1., 0., 0., 0., 0.f, 0.66, 120.));
|
||||
gameEngine.addComponent(newEnt, Collider.class, new Collider(10., 10.));
|
||||
gameEngine.addComponent(newEnt, Gravity.class, new Gravity(0, -9.80665, 45., 45.)); // Terminal velocity is 45 m/s
|
||||
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
|
||||
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(10, 10, 255, 64, 64));
|
||||
|
||||
// The textobject that prints the current number of entities
|
||||
newEnt = gameEngine.createEntity();
|
||||
gameEngine.addComponent(newEnt, TextRender.class, new TextRender("", 24, Color.WHITE));
|
||||
gameEngine.addComponent(newEnt, Render.class, new Render(TextRender.class));
|
||||
gameEngine.addComponent(newEnt, Reporter.class, new Reporter());
|
||||
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(5,5,0));
|
||||
// Other objects
|
||||
|
||||
/* // Uncomment this block to enable the stress-test (runs 500 square entities, with physics etc.)
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
for (int i = 0; i < 500; i++) {
|
||||
newEnt = gameEngine.createEntity();
|
||||
double size = 1 + rand.nextDouble();
|
||||
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(10 + rand.nextDouble()* 80, 10+ rand.nextDouble()* 80,0));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(-2.5 + rand.nextDouble()* 5., 0., 0., 0., 0.f, 0., rand.nextDouble() * 20.));
|
||||
double size = 3 + rand.nextDouble() * 7;
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(-2.5 + rand.nextDouble()* 5., 0., 0., 0., 0.f, 0.3, 2. * size));
|
||||
gameEngine.addComponent(newEnt, Collider.class, new Collider(size, size));
|
||||
gameEngine.addComponent(newEnt, Gravity.class, new Gravity(0, -9.80665, 45., 45.)); // Terminal velocity is 45 m/s
|
||||
gameEngine.addComponent(newEnt, LogUpdate.class, null); // Add to object to print pos of
|
||||
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(5 + rand.nextDouble() * 90));
|
||||
}
|
||||
|
||||
//gameEngine.addComponent(newEnt, LogUpdate.class, null);
|
||||
|
||||
gameEngine.addComponent(newEnt, TimedExistence.class, new TimedExistence(25 + rand.nextDouble() * 90));
|
||||
} */
|
||||
|
||||
/************************************
|
||||
** Run init() on the systems **
|
||||
*************************************/
|
||||
physicsSystem.init();
|
||||
collisionSystem.init();
|
||||
logUpdateSystem.init();
|
||||
@ -122,32 +134,33 @@ public final class App {
|
||||
existenceSystem.init();
|
||||
reportSystem.init();
|
||||
|
||||
|
||||
/************************************
|
||||
** Prepare and enter the game loop **
|
||||
*************************************/
|
||||
double dt = 0.001;
|
||||
double frameRate = 1.0 / 144; // 1 second divided by target number of frames
|
||||
double idleTime = 0.0; // the amount of time spent sleeping
|
||||
|
||||
boolean exit = false;
|
||||
long loopStart = System.currentTimeMillis();
|
||||
long startTime = System.nanoTime();
|
||||
while (!exit){
|
||||
|
||||
// Run system updates
|
||||
collisionSystem.update();
|
||||
physicsSystem.update(dt);
|
||||
existenceSystem.update(dt);
|
||||
reportSystem.update();
|
||||
//logUpdateSystem.update();
|
||||
// The render system should be uncoupled from the physics and collision
|
||||
renderSystem.update();
|
||||
|
||||
dt = (System.nanoTime() - startTime) / 1e9; // convert nanoseconds to seconds
|
||||
startTime = System.nanoTime();
|
||||
if (dt < frameRate){
|
||||
idleTime = frameRate-dt;
|
||||
Thread.sleep((int)((frameRate-dt)*1000));
|
||||
dt = (System.nanoTime() - startTime) / 1e9;
|
||||
startTime = System.nanoTime();
|
||||
}
|
||||
dt = 0.001;
|
||||
// Setting dt here to a fixed time-step will lock the rate of the physics. The above code ensures that it is
|
||||
// executed no more than once 1/framerate seconds.
|
||||
// dt = 0.001;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,4 @@ public class Collider {
|
||||
// bottom-right, top-right is specified by position
|
||||
public double x = 0.0;
|
||||
public double y = 0.0;
|
||||
public boolean collided = false;
|
||||
|
||||
public double travelDirection = 0.0;
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
package nz.ac.massey.javaecs.examples.Systems;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.BitSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import nz.ac.massey.javaecs.ECSSystem;
|
||||
import nz.ac.massey.javaecs.Engine;
|
||||
@ -29,170 +26,98 @@ public class CollisionSystem extends ECSSystem {
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
public void updateNew() {
|
||||
Set<Entity> processed = new HashSet<>();
|
||||
// Gets all items we have collided with
|
||||
for (Entity entity : entities) {
|
||||
RidgidBody ridgidBody = (RidgidBody) gameEngine.getComponentData(entity, RidgidBody.class);
|
||||
// Only considering elements with mass just now
|
||||
if (ridgidBody.mass > 0){
|
||||
Vec2D pos = (Vec2D) gameEngine.getComponentData(entity, Vec2D.class);
|
||||
Collider collider = (Collider) gameEngine.getComponentData(entity, Collider.class);
|
||||
|
||||
// The pos of each bounding line of the entity
|
||||
// i.e.
|
||||
//
|
||||
// -------------
|
||||
// |###########|
|
||||
// |###########|
|
||||
// |###########|
|
||||
// |###########|
|
||||
// -------------
|
||||
//
|
||||
double eleft = pos.x;
|
||||
double ebottom = pos.y;
|
||||
double eright = pos.x + collider.x;
|
||||
double etop = pos.y + collider.y;
|
||||
|
||||
double evectorAngle = calcAngle(ridgidBody.xVel, ridgidBody.yVel);
|
||||
|
||||
|
||||
if (ridgidBody.mass > 0){
|
||||
// Dont't recalc for entities that aren't static
|
||||
processed.add(entity);
|
||||
}
|
||||
|
||||
for (Entity entity2 : entities) {
|
||||
if (!processed.contains(entity2)) {
|
||||
// skip entities that have already been processed
|
||||
Vec2D oPos = (Vec2D) gameEngine.getComponentData(entity2, Vec2D.class);
|
||||
RidgidBody oRidgidBody = (RidgidBody) gameEngine.getComponentData(entity2, RidgidBody.class);
|
||||
Collider oCollider = (Collider) gameEngine.getComponentData(entity2, Collider.class);
|
||||
|
||||
double oleft = oPos.x;
|
||||
double obottom = oPos.y;
|
||||
double oright = oPos.x + oCollider.x;
|
||||
double otop = oPos.y + oCollider.y;
|
||||
|
||||
// Check if a collision occured
|
||||
// Few conditions required, that is,
|
||||
// - oS2 must be greater than eS0, else they aren't close
|
||||
// - eS2 must be greater than oS0, for the same reason
|
||||
// - either
|
||||
if (!(eleft > oright || oleft > eright || etop < obottom || otop < ebottom)){
|
||||
// Collided
|
||||
// Grab the angle the other is moving, to figure the quadrant to move to
|
||||
double ovectorAngle = calcAngle(oRidgidBody.xVel, oRidgidBody.yVel);
|
||||
// Grab the vector sums
|
||||
double xVelSum = ridgidBody.xVel + oRidgidBody.xVel;
|
||||
double yVelSum = ridgidBody.yVel + oRidgidBody.yVel;
|
||||
// Collision occurred
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// This is a quick & naive physics implementation. Not all collision states are accurately calculated
|
||||
// I also expect it is highly inefficient, though I was able to run it with 500 subscribed entities
|
||||
// with no major performance issues
|
||||
// Replace this with a proper system as required
|
||||
@Override
|
||||
public void update() {
|
||||
// Parallelising this loop ()
|
||||
Set<Entity> processed = new HashSet<>();
|
||||
// Gets all items we have collided with
|
||||
for (Entity entity : entities) {
|
||||
Vec2D pos = (Vec2D) gameEngine.getComponentData(entity, Vec2D.class);
|
||||
RidgidBody ridgidBody = (RidgidBody) gameEngine.getComponentData(entity, RidgidBody.class);
|
||||
Collider collider = (Collider) gameEngine.getComponentData(entity, Collider.class);
|
||||
if (ridgidBody.mass > 0){
|
||||
if (ridgidBody.mass >= 0) {
|
||||
// Only ignore non-static colliders
|
||||
processed.add(entity);
|
||||
}
|
||||
//processed.add(entity);
|
||||
for (Entity entity2 : entities) {
|
||||
if (!processed.contains(entity2)) {
|
||||
// skip entities that have already been processed
|
||||
Vec2D otherPos = (Vec2D) gameEngine.getComponentData(entity2, Vec2D.class);
|
||||
RidgidBody otherRidgidBody = (RidgidBody) gameEngine.getComponentData(entity2, RidgidBody.class);
|
||||
RidgidBody otherRidgidBody = (RidgidBody) gameEngine.getComponentData(entity2,
|
||||
RidgidBody.class);
|
||||
Collider otherCollider = (Collider) gameEngine.getComponentData(entity2, Collider.class);
|
||||
|
||||
|
||||
if (ridgidBody.mass >= 0 || otherRidgidBody.mass >= 0) {
|
||||
// Evaluate a collision in the top-left
|
||||
boolean tlCollide = pos.x > otherPos.x && pos.x < otherPos.x + otherCollider.x
|
||||
&& pos.y > otherPos.y && pos.y < otherPos.y + otherCollider.y;
|
||||
// Evaluate a collision in the bottom-left
|
||||
boolean blCollide = pos.x > otherPos.x && pos.x < otherPos.x + otherCollider.x
|
||||
&& pos.y + collider.y > otherPos.y && pos.y + collider.y < otherPos.y + otherCollider.y;
|
||||
&& pos.y + collider.y > otherPos.y
|
||||
&& pos.y + collider.y < otherPos.y + otherCollider.y;
|
||||
// Evaluate a collision in the bottom-right
|
||||
boolean brCollide = pos.x + collider.x > otherPos.x
|
||||
&& pos.x + collider.x < otherPos.x + otherCollider.x && pos.y + collider.y > otherPos.y
|
||||
&& pos.x + collider.x < otherPos.x + otherCollider.x
|
||||
&& pos.y + collider.y > otherPos.y
|
||||
&& pos.y + collider.y < otherPos.y + otherCollider.y;
|
||||
// Evaluate a collision in the top-right
|
||||
boolean trCollide = pos.x + collider.x > otherPos.x
|
||||
&& pos.x + collider.x < otherPos.x + otherCollider.x && pos.y > otherPos.y
|
||||
&& pos.y < otherPos.y + otherCollider.y;
|
||||
if (tlCollide || blCollide || brCollide || trCollide) {
|
||||
// Determine collision nature
|
||||
if (tlCollide) {
|
||||
int k = 0;
|
||||
}
|
||||
if (trCollide) {
|
||||
int k = 0;
|
||||
}
|
||||
if (blCollide) {
|
||||
int k = 0;
|
||||
}
|
||||
if (brCollide) {
|
||||
int k = 0;
|
||||
}
|
||||
// Get the vels
|
||||
double totalXVel = Math.abs(ridgidBody.xVel) + Math.abs(otherRidgidBody.xVel);
|
||||
double totalYVel = Math.abs(ridgidBody.yVel) + Math.abs(otherRidgidBody.yVel);
|
||||
double xMult = 0;
|
||||
double yMult = 0;
|
||||
double multTot = (1 + ridgidBody.bounciness);
|
||||
double totalMass = ridgidBody.mass + otherRidgidBody.mass;
|
||||
int xMult = 0;
|
||||
int yMult = 0;
|
||||
boolean fullEdge = false;
|
||||
// left-hand collision
|
||||
if (tlCollide && blCollide) {
|
||||
fullEdge = true;
|
||||
xMult = 2;
|
||||
pos.x = otherPos.x + otherCollider.x;
|
||||
xMult = multTot;
|
||||
}
|
||||
// right-hand collision
|
||||
else if (brCollide && trCollide) {
|
||||
fullEdge = true;
|
||||
pos.x = otherPos.x - collider.x;
|
||||
xMult = -multTot;
|
||||
}
|
||||
// Bottom collision
|
||||
if (blCollide && brCollide) {
|
||||
fullEdge = true;
|
||||
yMult = -2;
|
||||
}
|
||||
// right-hand collision
|
||||
if (brCollide && trCollide) {
|
||||
fullEdge = true;
|
||||
xMult = -2;
|
||||
//pos.y = otherPos.y + otherCollider.y;
|
||||
pos.y = otherPos.y - collider.y;
|
||||
yMult = -multTot;
|
||||
}
|
||||
// top collision
|
||||
if (trCollide && tlCollide) {
|
||||
else if (trCollide && tlCollide) {
|
||||
fullEdge = true;
|
||||
yMult = 2;
|
||||
//pos.y = otherPos.y - collider.y;
|
||||
pos.y = otherPos.y + otherCollider.y;
|
||||
yMult = multTot;
|
||||
}
|
||||
if (fullEdge) {
|
||||
// Finally, actuate the calculation
|
||||
if (ridgidBody.mass < 0) {
|
||||
// entity 1 is immovable
|
||||
otherRidgidBody.xVel += totalXVel * (-1 * xMult);
|
||||
otherRidgidBody.yVel += totalYVel * (-1 * yMult);
|
||||
|
||||
Object grav = gameEngine.getComponentData(entity, Gravity.class);
|
||||
if (grav != null) {
|
||||
Gravity gravity = (Gravity) grav;
|
||||
otherRidgidBody.xAcc += gravity.x * -1.;
|
||||
otherRidgidBody.yAcc += gravity.y * -1.;
|
||||
}
|
||||
|
||||
} else if (otherRidgidBody.mass < 0) {
|
||||
// entity 2 is immovable; full acceleration applied to the current entity
|
||||
|
||||
if (otherRidgidBody.mass > 0){
|
||||
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);
|
||||
}
|
||||
else{
|
||||
ridgidBody.xVel += totalXVel * xMult;
|
||||
ridgidBody.yVel += totalYVel * yMult;
|
||||
}
|
||||
Object grav = gameEngine.getComponentData(entity, Gravity.class);
|
||||
if (grav != null) {
|
||||
// negate gravity while collided
|
||||
@ -200,51 +125,34 @@ public class CollisionSystem extends ECSSystem {
|
||||
ridgidBody.xAcc += gravity.x * -1.;
|
||||
ridgidBody.yAcc += gravity.y * -1.;
|
||||
}
|
||||
} else {
|
||||
// collision is elastic; add the forces x & y and the masses, and share between
|
||||
// objects
|
||||
/*ridgidBody.xVel += (ridgidBody.mass / totalMass) * totalXVel * xMult;
|
||||
otherRidgidBody.xVel += (otherRidgidBody.mass / totalMass) * totalXVel
|
||||
* (-1 * xMult);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ridgidBody.yVel += (ridgidBody.mass / totalMass) * totalYVel * yMult;
|
||||
otherRidgidBody.yVel += (otherRidgidBody.mass / totalMass) * totalYVel
|
||||
* (-1 * yMult);*/
|
||||
}
|
||||
}
|
||||
else{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Zero degrees is straight up,
|
||||
* 90 east
|
||||
* -90 (270) = west
|
||||
* 180 south
|
||||
* Zero degrees is straight up, 90 east -90 (270) = west 180 south
|
||||
*
|
||||
* @param xVel
|
||||
* @param yVel
|
||||
* @return
|
||||
*/
|
||||
public static double calcAngle(double xVel, double yVel){
|
||||
if (xVel == 0){ // Div zero; = +- 90
|
||||
public static double calcAngle(double xVel, double yVel) {
|
||||
if (xVel == 0) { // Div zero; = +- 90
|
||||
return yVel < 0 ? 270 : 90;
|
||||
}
|
||||
else if (xVel < 0 && yVel < 0){ // bottom-left quad (180 - 270)
|
||||
return 180. + Math.tanh(yVel/xVel);
|
||||
}
|
||||
else if (xVel < 0){ // Top left quad (270 - 360)
|
||||
return 360 - Math.tanh(yVel/xVel);
|
||||
}
|
||||
else if (yVel < 0){
|
||||
return 180 - Math.tanh(yVel/xVel);
|
||||
}
|
||||
else{
|
||||
return Math.tanh(yVel/xVel);
|
||||
} else if (xVel < 0 && yVel < 0) { // bottom-left quad (180 - 270)
|
||||
return 180. + Math.tanh(yVel / xVel);
|
||||
} else if (xVel < 0) { // Top left quad (270 - 360)
|
||||
return 360 - Math.tanh(yVel / xVel);
|
||||
} else if (yVel < 0) {
|
||||
return 180 - Math.tanh(yVel / xVel);
|
||||
} else {
|
||||
return Math.tanh(yVel / xVel);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import nz.ac.massey.javaecs.Entity;
|
||||
import nz.ac.massey.javaecs.examples.Components.BoxRender;
|
||||
import nz.ac.massey.javaecs.examples.Components.Collider;
|
||||
import nz.ac.massey.javaecs.examples.Components.Gravity;
|
||||
import nz.ac.massey.javaecs.examples.Components.LogUpdate;
|
||||
import nz.ac.massey.javaecs.examples.Components.Render;
|
||||
import nz.ac.massey.javaecs.examples.Components.RidgidBody;
|
||||
import nz.ac.massey.javaecs.examples.Components.TimedExistence;
|
||||
@ -32,13 +31,10 @@ public class ExistenceSystem extends ECSSystem{
|
||||
@Override
|
||||
public void init() {
|
||||
rand = new Random();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void update(double dt){
|
||||
@ -47,10 +43,10 @@ public class ExistenceSystem extends ECSSystem{
|
||||
BoxRender shape = (BoxRender)gameEngine.getComponentData(entity, BoxRender.class);
|
||||
TimedExistence timedExistence = (TimedExistence)gameEngine.getComponentData(entity, TimedExistence.class);
|
||||
// Get the rate the fade should occur at our current frame rate.
|
||||
double fadeRate = timedExistence.lifeTime / dt; // I.e. might say 200 frames left
|
||||
double fadeRate = timedExistence.lifeTime / dt;
|
||||
// Fade the opacity
|
||||
shape.a -= shape.a / fadeRate;
|
||||
// Clauses to keep things safe
|
||||
// Keep the alpha between 0-1
|
||||
if (shape.a < 0) shape.a = 0.;
|
||||
else if (shape.a > 1) shape.a = 1.;
|
||||
// Reduce the time the shape may continue living for
|
||||
@ -60,15 +56,18 @@ public class ExistenceSystem extends ECSSystem{
|
||||
deletions.add(entity);
|
||||
}
|
||||
}
|
||||
// As we were using the iterator to traverse entities, we could not delete them as we read them.
|
||||
while (deletions.size() > 0){
|
||||
gameEngine.destroyEntity(deletions.remove());
|
||||
}
|
||||
// Finally, randomly add an entity as required
|
||||
if (rand.nextInt(16384) < 8){
|
||||
int newRate = gameEngine.getNumEntities();
|
||||
newRate = newRate < 13 ? 7 - newRate / 2 : 1;
|
||||
if (rand.nextInt(2048) <= 1*newRate){
|
||||
Entity newEnt = gameEngine.createEntity();
|
||||
double size = 1 + rand.nextDouble() * 10;
|
||||
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(10 + rand.nextDouble()* 80, 10+ rand.nextDouble()* 80,0));
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(-2.5 + rand.nextDouble()* 5., 0., 0., 0., 0.f, 0., rand.nextDouble() * 20.));
|
||||
double size = 3 + rand.nextDouble() * 7;
|
||||
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(-2.5 + rand.nextDouble()* 5., 0., 0., 0., 0.f, 0.75, 2. * size));
|
||||
gameEngine.addComponent(newEnt, Collider.class, new Collider(size, size));
|
||||
gameEngine.addComponent(newEnt, Gravity.class, new Gravity(0, -9.80665, 45., 45.)); // Terminal velocity is 45 m/s
|
||||
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
|
||||
|
@ -40,7 +40,6 @@ public class PhysicsSystem extends ECSSystem {
|
||||
Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
|
||||
RidgidBody ridgidBody = (RidgidBody)gameEngine.getComponentData(entity, RidgidBody.class);
|
||||
Gravity gravity = (Gravity)gameEngine.getComponentData(entity, Gravity.class);
|
||||
Collider collider = (Collider)gameEngine.getComponentData(entity, Collider.class);
|
||||
// Firstly, add the result of the accelerative forces to the ridgidbody
|
||||
ridgidBody.xVel += ridgidBody.xAcc * dt;
|
||||
ridgidBody.yVel += ridgidBody.yAcc * dt;
|
||||
@ -49,7 +48,6 @@ public class PhysicsSystem extends ECSSystem {
|
||||
ridgidBody.xAcc = 0.;
|
||||
ridgidBody.yAcc = 0.;
|
||||
|
||||
if (!collider.collided){
|
||||
// Special case of gravity
|
||||
if (gravity.terminalX < 0 || Math.abs(ridgidBody.xVel) < gravity.terminalX){
|
||||
ridgidBody.xAcc += gravity.x;
|
||||
@ -57,7 +55,6 @@ public class PhysicsSystem extends ECSSystem {
|
||||
if (gravity.terminalY < 0 || Math.abs(ridgidBody.yVel) < gravity.terminalY){
|
||||
ridgidBody.yAcc += gravity.y;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Finally, move the vec2d by the new velocity
|
||||
|
@ -4,11 +4,9 @@ import java.util.BitSet;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.concurrent.Task;
|
||||
import javafx.scene.Group;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.paint.Paint;
|
||||
import javafx.scene.shape.Rectangle;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.scene.text.Text;
|
||||
@ -40,23 +38,15 @@ public class RenderSystem extends ECSSystem{
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
// Spawn a new thread
|
||||
// Spawn a new asynchronous thread (won't rejoin the main program flow)
|
||||
renderThread = new Thread(new RenderStarter());
|
||||
renderThread.start(); // this thread is asynchronous, it isn't expected to rejoin
|
||||
renderThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
/* while (JFXView.root == null){
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} */
|
||||
Group renderScene = new Group();
|
||||
for (Entity entity : entities) {
|
||||
//Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
|
||||
if (((Render)gameEngine.getComponentData(entity, Render.class)).renderType == BoxRender.class){
|
||||
BoxRender box = (BoxRender)gameEngine.getComponentData(entity, BoxRender.class);
|
||||
Vec2D vec2d = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
|
||||
@ -77,6 +67,9 @@ public class RenderSystem extends ECSSystem{
|
||||
renderScene.getChildren().add(t);
|
||||
}
|
||||
}
|
||||
|
||||
// Must be run by the UI thread, not the main thread.
|
||||
// This dispatches the operation run() to the UI.
|
||||
Platform.runLater(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
@ -87,7 +80,6 @@ public class RenderSystem extends ECSSystem{
|
||||
}
|
||||
|
||||
// Add a renderer
|
||||
|
||||
public class RenderStarter implements Runnable{
|
||||
|
||||
@Override
|
||||
@ -109,7 +101,6 @@ public class RenderSystem extends ECSSystem{
|
||||
root = new Group();
|
||||
Scene scene = new Scene(root, 1024, 1024, Color.BLACK);
|
||||
primaryStage.setScene(scene);
|
||||
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
|
@ -20,8 +20,6 @@ public class ReportSystem extends ECSSystem{
|
||||
}
|
||||
@Override
|
||||
public void init() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user