Added rotation and a world scale slider
This commit is contained in:
@ -2,7 +2,7 @@ let images = {};
|
||||
let simulator = [];
|
||||
let maxSpeed = 1;
|
||||
let backgroundImage;
|
||||
|
||||
let worldScale;
|
||||
let LEFT = [-1,0];
|
||||
|
||||
const entityTypes = {
|
||||
@ -24,16 +24,19 @@ function getMass(type){
|
||||
}
|
||||
}
|
||||
|
||||
function createEntity(x, y, vx, vy, type){
|
||||
function createEntity(x, y, vx, vy, ax, ay, rot, rotv, type){
|
||||
let entity = {
|
||||
x: x,
|
||||
y: y,
|
||||
ax: 0.0,
|
||||
ay: 0.0,
|
||||
ax: ax,
|
||||
ay: ay,
|
||||
vx: vx,
|
||||
vy: vy,
|
||||
rot: 0,
|
||||
rot: rot,
|
||||
rotv: rotv,
|
||||
mass: getMass(type),
|
||||
forcex: 0.0,
|
||||
forcey: 0.0,
|
||||
type: type,
|
||||
}
|
||||
simulator.push(entity);
|
||||
@ -57,9 +60,30 @@ function physicsUpdate(entity){
|
||||
entity.vy += entity.ay;
|
||||
entity.x += entity.vx;
|
||||
entity.y += entity.vy;
|
||||
entity.rot += entity.rotv;
|
||||
|
||||
}
|
||||
|
||||
function onHit(entity){
|
||||
function onHit(entity1, entity2){
|
||||
if(entity1.type == entityTypes.METERORITE_LARGE){
|
||||
createEntity(entity1.x, entity1.y, entity2.vx-entity1.vx, entity2.vy-entity1.vy, 0.0,0.0,entity2.rot-entity1.rot, entity2.rotv-entity1.rotv, entityTypes.METERORITE_SMALL);
|
||||
}if(entity1.type == entityTypes.COMET){
|
||||
|
||||
}else{
|
||||
Explode(entity1);
|
||||
}
|
||||
|
||||
if(entity2.type == entityTypes.METERORITE_LARGE){
|
||||
createEntity(entity2.x, entity2.y, entity1.vx-entity2.vx, entity1.vy-entity2.vy, 0.0,0.0,entity2.rot-entity1.rot, entity2.rotv-entity1.rotv, entityTypes.METERORITE_SMALL);
|
||||
}if(entity1.type == entityTypes.COMET){
|
||||
|
||||
}else{
|
||||
Explode(entity2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function Explode(entity){
|
||||
|
||||
}
|
||||
|
||||
@ -76,8 +100,13 @@ function drawBackground(refImage){
|
||||
function drawEntity(entity){
|
||||
push();
|
||||
imageMode(CENTER);
|
||||
translate(entity.x, entity.y, 0);
|
||||
rotate((Math.atan2(entity.vx, entity.vy) - Math.atan2(-1,0)) * 180/Math.PI);
|
||||
translate(entity.x*(worldScale.value()/100), entity.y*(worldScale.value()/100), 0);
|
||||
if (entity.type == entityTypes.COMET){
|
||||
rotate((Math.atan2(entity.vx, entity.vy) - Math.atan2(-1,0)) * 180/Math.PI);
|
||||
|
||||
}else{
|
||||
rotate(entity.rot);
|
||||
}
|
||||
image(images[entity.type], 0,0);
|
||||
pop();
|
||||
}
|
||||
@ -96,15 +125,20 @@ function setup(){
|
||||
describe("A space physics simulator");
|
||||
|
||||
canvas.position(0,0);
|
||||
|
||||
worldScale = createSlider();
|
||||
//Load Entities
|
||||
let entityCount = 50;
|
||||
let rotationSpeed = 100.0;
|
||||
for(let i = 0; i <= entityCount; i+=1){
|
||||
createEntity(
|
||||
Math.floor(Math.random() * windowWidth),
|
||||
Math.floor(Math.random() * windowHeight),
|
||||
(Math.random() * maxSpeed * 2)-maxSpeed,
|
||||
(Math.random() * maxSpeed * 2)-maxSpeed,
|
||||
Math.floor(Math.random() * windowWidth), //X Location
|
||||
Math.floor(Math.random() * windowHeight), //Y Location
|
||||
(Math.random() * maxSpeed * 2)-maxSpeed, //X Velocity
|
||||
(Math.random() * maxSpeed * 2)-maxSpeed, //Y Velocity
|
||||
0.0, //X Acceleration
|
||||
0.0, //Y Acceleration
|
||||
0.0, //Current Rotation
|
||||
(Math.random() * 2 *(Math.PI / rotationSpeed))-(Math.PI / rotationSpeed), //Rotation Velocity
|
||||
getRandomType(),
|
||||
);
|
||||
}
|
||||
@ -115,6 +149,7 @@ function mouseClicked(){
|
||||
simulator.forEach((entity) =>{
|
||||
|
||||
})
|
||||
console.log(worldScale.value());
|
||||
}
|
||||
|
||||
function draw(){
|
||||
@ -132,20 +167,24 @@ function draw(){
|
||||
|
||||
//Window Wrap
|
||||
if (Settings.windowWrap){
|
||||
if (entity.x > windowWidth){
|
||||
if (entity.x * (worldScale.value() /100) > windowWidth){
|
||||
entity.x = 0;
|
||||
}
|
||||
if (entity.x < 0){
|
||||
entity.x = windowWidth;
|
||||
entity.x = windowWidth / (worldScale.value() /100);
|
||||
}
|
||||
if (entity.y > windowHeight){
|
||||
if (entity.y * (worldScale.value() /100) > windowHeight){
|
||||
entity.y = 0;
|
||||
}
|
||||
if (entity.y < 0){
|
||||
entity.y = windowHeight;
|
||||
entity.y = windowHeight / (worldScale.value() /100);
|
||||
}
|
||||
}
|
||||
//Draw Entity
|
||||
drawEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
function windowResized() {
|
||||
resizeCanvas(windowWidth, windowHeight);
|
||||
}
|
||||
Reference in New Issue
Block a user