Floating Objects
This commit is contained in:
1 parent
d27c003f83
commit
3e09a0009c
4 files changed
+54
-54
No files matched your search
Binary file not shown.
@@ -46,7 +46,7 @@ function newCanvas() {
|
||||
|
||||
let containerDragStarted = false;
|
||||
let ballDragStarted = false;
|
||||
let ballBeingDragged;
|
||||
let floatingObjectBeingDragged;
|
||||
let containerBeingDragged;
|
||||
let viewPortBeingDragged = false;
|
||||
|
||||
@@ -241,9 +241,9 @@ function newCanvas() {
|
||||
|
||||
/* We check first if the mouse is over a ball/ */
|
||||
|
||||
ballBeingDragged = thisObject.floatingSpace.isInside(event.pageX, event.pageY);
|
||||
floatingObjectBeingDragged = thisObject.floatingSpace.isInside(event.pageX, event.pageY);
|
||||
|
||||
if (ballBeingDragged >= 0) {
|
||||
if (floatingObjectBeingDragged >= 0) {
|
||||
ballDragStarted = true;
|
||||
return;
|
||||
}
|
||||
@@ -338,7 +338,7 @@ function newCanvas() {
|
||||
|
||||
if (ballDragStarted === true) {
|
||||
|
||||
if (thisObject.floatingSpace.isInsideBall(ballBeingDragged, event.pageX, event.pageY) === false) {
|
||||
if (thisObject.floatingSpace.isInsideBall(floatingObjectBeingDragged, event.pageX, event.pageY) === false) {
|
||||
|
||||
/* This means that the user stop moving the mouse and the ball ball out of the pointer.
|
||||
In this case we cancell the drag operation . */
|
||||
@@ -414,7 +414,7 @@ function newCanvas() {
|
||||
|
||||
if (ballDragStarted) {
|
||||
|
||||
let ball = thisObject.floatingSpace.balls[ballBeingDragged];
|
||||
let ball = thisObject.floatingSpace.floatingObjects[floatingObjectBeingDragged];
|
||||
|
||||
ball.currentPosition.x = dragVector.upX;
|
||||
ball.currentPosition.y = dragVector.upY;
|
||||
|
||||
+15
-15
@@ -3,7 +3,7 @@ function newFloatingObject() {
|
||||
|
||||
var thisObject = {
|
||||
|
||||
input: undefined, // This object contains information needed to draw the ball, but also referenced from outside, so as to have control of the basics of the ball.
|
||||
input: undefined, // This object contains information needed to draw the floating object, but also referenced from outside, so as to have control of the basics of the floating object.
|
||||
physicsEnabled: false,
|
||||
|
||||
initializeMass: initializeMass,
|
||||
@@ -12,17 +12,17 @@ function newFloatingObject() {
|
||||
|
||||
imageId: undefined,
|
||||
|
||||
currentPosition: 0, // Current x,y position of the ball at the ball's layer, where there is no displacement or zoom. This position is always changing towards the target position.
|
||||
currentSpeed: 0, // This is the current speed of the ball.
|
||||
currentRadius: 0, // This is the current radius of the ball, including its zoom applied.
|
||||
currentMass: 0, // This is the current mass of the ball, including its zoom applied.
|
||||
currentPosition: 0, // Current x,y position of the floating object at the floating object's layer, where there is no displacement or zoom. This position is always changing towards the target position.
|
||||
currentSpeed: 0, // This is the current speed of the floating object.
|
||||
currentRadius: 0, // This is the current radius of the floating object, including its zoom applied.
|
||||
currentMass: 0, // This is the current mass of the floating object, including its zoom applied.
|
||||
|
||||
friction: 0, // This is a factor that will ultimatelly desacelerate the ball.
|
||||
friction: 0, // This is a factor that will ultimatelly desacelerate the floating object.
|
||||
|
||||
rawMass: 0, // This is the mass value without zoom.
|
||||
rawRadius: 0, // This is the radius of this ball without zoom.
|
||||
rawRadius: 0, // This is the radius of this floating object without zoom.
|
||||
|
||||
targetRadius: 0, // This is the target radius of the ball with zoom applied. It should be animated until reaching this value.
|
||||
targetRadius: 0, // This is the target radius of the floating object with zoom applied. It should be animated until reaching this value.
|
||||
|
||||
fillStyle: '',
|
||||
|
||||
@@ -31,16 +31,16 @@ function newFloatingObject() {
|
||||
radomizeCurrentPosition: radomizeCurrentPosition,
|
||||
radomizeCurrentSpeed: radomizeCurrentSpeed,
|
||||
|
||||
drawBackground: drawBallBackgrond, // Function to draw the ball elements on the canvas layer.
|
||||
drawForeground: drawBallForeground, // Function to draw the ball elements on the balls layer.
|
||||
drawBackground: drawBackgrond, // Function to draw the floating object elements on the canvas layer.
|
||||
drawForeground: drawForeground, // Function to draw the floating object elements on the floating objects layer.
|
||||
|
||||
updateMass: updateMass, // Function to update the mass when the zoom level changed.
|
||||
updateRadius: updateRadius, // Function to update the radius when the zoom level changed.
|
||||
|
||||
linkedObject: undefined, // This is a reference to the object that this ball is representing.
|
||||
linkedObjectType: "", // Since there might be balls for different types of objects, here we store the type of object we are linking to.
|
||||
linkedObject: undefined, // This is a reference to the object that this floating object is representing.
|
||||
linkedObjectType: "", // Since there might be floating objects for different types of objects, here we store the type of object we are linking to.
|
||||
|
||||
container: undefined // This is a pointer to the object where the ball belongs to.
|
||||
container: undefined // This is a pointer to the object where the floating object belongs to.
|
||||
};
|
||||
|
||||
return thisObject;
|
||||
@@ -132,7 +132,7 @@ function newFloatingObject() {
|
||||
|
||||
}
|
||||
|
||||
function drawBallBackgrond() {
|
||||
function drawBackgrond() {
|
||||
|
||||
if (thisObject.currentRadius > 1) {
|
||||
|
||||
@@ -164,7 +164,7 @@ function newFloatingObject() {
|
||||
}
|
||||
}
|
||||
|
||||
function drawBallForeground() {
|
||||
function drawForeground() {
|
||||
|
||||
if (thisObject.currentRadius > 5) {
|
||||
|
||||
|
||||
+34
-34
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
The Floating Space is the place where floating elements like balls, live and are rendered.
|
||||
The Floating Space is the place where floating elements like floatingObjects, live and are rendered.
|
||||
This space has its own physics which helps with the animation of these objects and also preventing them to overlap.
|
||||
*/
|
||||
|
||||
function newFloatingSpace() {
|
||||
|
||||
let thisObject = {
|
||||
balls: undefined, // This is the array of balls being displayed
|
||||
floatingObjects: undefined, // This is the array of floatingObjects being displayed
|
||||
createNewBall: createNewBall,
|
||||
destroyBall: destroyBall,
|
||||
createBubbleSet: createBubbleSet,
|
||||
@@ -24,14 +24,14 @@ function newFloatingSpace() {
|
||||
|
||||
function initialize() {
|
||||
|
||||
thisObject.balls = []; // We initialize this in order to start the calculations in a clean way.
|
||||
thisObject.floatingObjects = []; // We initialize this in order to start the calculations in a clean way.
|
||||
|
||||
}
|
||||
|
||||
|
||||
function createNewBall(pInput, pContainer) {
|
||||
|
||||
/* This function is used to create new balls */
|
||||
/* This function is used to create new floatingObjects */
|
||||
|
||||
var ball = newFloatingObject();
|
||||
|
||||
@@ -67,12 +67,12 @@ function newFloatingSpace() {
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < thisObject.balls.length; i++) {
|
||||
for (let i = 0; i < thisObject.floatingObjects.length; i++) {
|
||||
|
||||
let ball = thisObject.balls[i];
|
||||
let ball = thisObject.floatingObjects[i];
|
||||
|
||||
if (ball.handle === pBallHandle) {
|
||||
thisObject.balls.splice(i, 1); // Delete item from array.
|
||||
thisObject.floatingObjects.splice(i, 1); // Delete item from array.
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -113,11 +113,11 @@ function newFloatingSpace() {
|
||||
|
||||
function physicsLoop() {
|
||||
|
||||
/* This function makes all the calculations to apply phisycs on all balls in this space. */
|
||||
/* This function makes all the calculations to apply phisycs on all floatingObjects in this space. */
|
||||
|
||||
for (let i = 0; i < thisObject.balls.length; i++) {
|
||||
for (let i = 0; i < thisObject.floatingObjects.length; i++) {
|
||||
|
||||
let ball = thisObject.balls[i];
|
||||
let ball = thisObject.floatingObjects[i];
|
||||
|
||||
/* Change position based on speed */
|
||||
|
||||
@@ -181,16 +181,16 @@ function newFloatingSpace() {
|
||||
|
||||
/* Collision Control */
|
||||
|
||||
for (let k = i + 1; k < thisObject.balls.length; k++) {
|
||||
for (let k = i + 1; k < thisObject.floatingObjects.length; k++) {
|
||||
|
||||
if (colliding(thisObject.balls[i], thisObject.balls[k])) {
|
||||
if (colliding(thisObject.floatingObjects[i], thisObject.floatingObjects[k])) {
|
||||
|
||||
resolveCollision(thisObject.balls[k], thisObject.balls[i]);
|
||||
resolveCollision(thisObject.floatingObjects[k], thisObject.floatingObjects[i]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* Calculate repulsion force produced by all other balls */
|
||||
/* Calculate repulsion force produced by all other floatingObjects */
|
||||
|
||||
repulsionForce(i);
|
||||
|
||||
@@ -198,15 +198,15 @@ function newFloatingSpace() {
|
||||
|
||||
}
|
||||
|
||||
/* We draw all the thisObject.balls. */
|
||||
/* We draw all the thisObject.floatingObjects. */
|
||||
|
||||
for (let i = 0; i < thisObject.balls.length; i++) {
|
||||
let ball = thisObject.balls[i];
|
||||
for (let i = 0; i < thisObject.floatingObjects.length; i++) {
|
||||
let ball = thisObject.floatingObjects[i];
|
||||
ball.drawBackground();
|
||||
}
|
||||
|
||||
for (let i = 0; i < thisObject.balls.length; i++) {
|
||||
let ball = thisObject.balls[thisObject.balls.length - i - 1];
|
||||
for (let i = 0; i < thisObject.floatingObjects.length; i++) {
|
||||
let ball = thisObject.floatingObjects[thisObject.floatingObjects.length - i - 1];
|
||||
ball.drawForeground();
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ function newFloatingSpace() {
|
||||
ball.radomizeCurrentPosition(ball.input.position);
|
||||
ball.radomizeCurrentSpeed();
|
||||
|
||||
thisObject.balls.push(ball);
|
||||
thisObject.floatingObjects.push(ball);
|
||||
createdBalls.splice(i, 1); // Delete item from array.
|
||||
return; // Only one at the time.
|
||||
|
||||
@@ -240,7 +240,7 @@ function newFloatingSpace() {
|
||||
|
||||
var d = Math.sqrt(Math.pow(ball.input.position.x - ball.currentPosition.x, 2) + Math.pow(ball.input.position.y - ball.currentPosition.y, 2)); // ... we calculate the distance ...
|
||||
|
||||
var force = coulomb * d * d / ball.currentMass; // In this case the mass of the ball affects the gravity force that it receives, that gives priority to target position to bigger balls.
|
||||
var force = coulomb * d * d / ball.currentMass; // In this case the mass of the ball affects the gravity force that it receives, that gives priority to target position to bigger floatingObjects.
|
||||
|
||||
if (force < minForce) { // We need this attraction force to overcome the friction we imposed to the system.
|
||||
force = minForce;
|
||||
@@ -280,23 +280,23 @@ function newFloatingSpace() {
|
||||
|
||||
function repulsionForce(currentBall) {
|
||||
|
||||
/* We generate a repulsion force between balls, that prevents them to be collisioning so often. */
|
||||
/* We generate a repulsion force between floatingObjects, that prevents them to be collisioning so often. */
|
||||
|
||||
const coulomb = 2;
|
||||
|
||||
var ball1 = thisObject.balls[currentBall];
|
||||
var ball1 = thisObject.floatingObjects[currentBall];
|
||||
|
||||
for (var i = 0; i < thisObject.balls.length; i++) { // The force to be applied is considering all other balls...
|
||||
for (var i = 0; i < thisObject.floatingObjects.length; i++) { // The force to be applied is considering all other floatingObjects...
|
||||
|
||||
if (i !== currentBall) { // ... except for the current one.
|
||||
|
||||
var ball2 = thisObject.balls[i]; // So, for each ball...
|
||||
var ball2 = thisObject.floatingObjects[i]; // So, for each ball...
|
||||
|
||||
var d = Math.sqrt(Math.pow(ball2.currentPosition.x - ball1.currentPosition.x, 2) + Math.pow(ball2.currentPosition.y - ball1.currentPosition.y, 2)); // ... we calculate the distance ...
|
||||
|
||||
var force = coulomb * ball2.currentMass / (d * d); // ... and with it the repulsion force.
|
||||
|
||||
/* We need to put a hard limit to this force, in order to to eject very little balls to the infinite and beyond. */
|
||||
/* We need to put a hard limit to this force, in order to to eject very little floatingObjects to the infinite and beyond. */
|
||||
|
||||
if (force > 1) {
|
||||
force = 1;
|
||||
@@ -339,7 +339,7 @@ function newFloatingSpace() {
|
||||
}
|
||||
|
||||
function colliding(ball1, ball2) {
|
||||
/* This function detects weather 2 balls collide with each other. */
|
||||
/* This function detects weather 2 floatingObjects collide with each other. */
|
||||
|
||||
var r1 = ball1.currentRadius;
|
||||
var r2 = ball2.currentRadius;
|
||||
@@ -363,10 +363,10 @@ function newFloatingSpace() {
|
||||
|
||||
function isInside(x, y) {
|
||||
|
||||
/* This function detects weather the point x,y is inside any of the balls. */
|
||||
/* This function detects weather the point x,y is inside any of the floatingObjects. */
|
||||
|
||||
for (var i = 0; i < thisObject.balls.length; i++) {
|
||||
var ball = thisObject.balls[i];
|
||||
for (var i = 0; i < thisObject.floatingObjects.length; i++) {
|
||||
var ball = thisObject.floatingObjects[i];
|
||||
var distance = Math.sqrt(Math.pow(ball.currentPosition.x - x, 2) + Math.pow(ball.currentPosition.y - y, 2));
|
||||
|
||||
if (distance < ball.currentRadius) {
|
||||
@@ -379,10 +379,10 @@ function newFloatingSpace() {
|
||||
|
||||
function isInsideBall(ballIndex, x, y) {
|
||||
|
||||
/* This function detects weather the point x,y is inside one particular balls. */
|
||||
/* This function detects weather the point x,y is inside one particular floatingObjects. */
|
||||
|
||||
|
||||
var ball = thisObject.balls[ballIndex];
|
||||
var ball = thisObject.floatingObjects[ballIndex];
|
||||
var distance = Math.sqrt(Math.pow(ball.currentPosition.x - x, 2) + Math.pow(ball.currentPosition.y - y, 2));
|
||||
|
||||
if (distance < ball.currentRadius) {
|
||||
@@ -399,7 +399,7 @@ function newFloatingSpace() {
|
||||
|
||||
function resolveCollision(ball1, ball2) {
|
||||
|
||||
/* This function changes speed and position of balls that are in collision */
|
||||
/* This function changes speed and position of floatingObjects that are in collision */
|
||||
|
||||
var collisionision_angle = Math.atan2((ball2.currentPosition.y - ball1.currentPosition.y), (ball2.currentPosition.x - ball1.currentPosition.x));
|
||||
|
||||
@@ -446,7 +446,7 @@ function newFloatingSpace() {
|
||||
|
||||
var d = Math.sqrt(Math.pow(ball2.currentPosition.x - ball1.currentPosition.x, 2) + Math.pow(ball2.currentPosition.y - ball1.currentPosition.y, 2));
|
||||
|
||||
// minimum translation distance to push balls apart after intersecting
|
||||
// minimum translation distance to push floatingObjects apart after intersecting
|
||||
var scalar = (((ball1.currentRadius + ball2.currentRadius) - d) / d);
|
||||
|
||||
var minTD = {
|
||||
|
||||
Reference in new issue
Block a user