Created
April 1, 2019 02:45
-
-
Save thomasdunn/5a9a0cb70e1b9a92af52639a2e189e9b to your computer and use it in GitHub Desktop.
For use in JUDO5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function main() { | |
var shipX = 50; | |
var shipY = 100; | |
var shipMass = 100; | |
var shipSize = 20; | |
var shipDirection = Pi * 1.5; | |
var shipSpeed = 1.8; | |
var earthX = 210; | |
var earthY = 140; | |
var earthMass = 2000; | |
var earthSize = 50; | |
var gravityMagnitude; | |
var gravityDirection; | |
setBackgroundColor(coral); | |
while (true) { | |
// calculate gravity | |
gravityMagnitude = computeGravityMagnitude(earthX, earthY, earthMass, shipX, shipY, shipMass); | |
gravityDirection = computeGravityDirection(earthX, earthY, shipX, shipY); | |
// add gravity to ships current velocity | |
shipSpeed = vectorAddMagnitude(gravityDirection, gravityMagnitude, shipDirection, shipSpeed); | |
shipDirection = vectorAddTheta(gravityDirection, gravityMagnitude, shipDirection, shipSpeed); | |
// move the ship | |
shipX += getXComponent(shipSpeed, shipDirection); | |
shipY -= getYComponent(shipSpeed, shipDirection); | |
// draw the next frame in the animation | |
clearDrawing(); | |
setColor(yellow); | |
fillCircle(earthX - (earthSize / 2), earthY - (earthSize / 2), earthSize); | |
setColor(blue); | |
fillCircle(shipX - (shipSize / 2), shipY - (shipSize / 2), shipSize); | |
delay(.03); | |
if (getMouseEvent()) { | |
earthX = getMouseX(); | |
earthY = getMouseY(); | |
} | |
if (shipX > getDrawingWidth()) { | |
shipX = getDrawingWidth() - 1; | |
shipDirection = flipHorizontally(shipDirection); | |
} | |
if (shipX < 0) { | |
shipX = 1; | |
shipDirection = flipHorizontally(shipDirection); | |
} | |
if (shipY > getDrawingHeight()) { | |
shipY = getDrawingHeight() - 1; | |
shipDirection = flipVertically(shipDirection); | |
} | |
if (shipY < 0) { | |
shipY = 1; | |
shipDirection = flipVertically(shipDirection); | |
} | |
} | |
} | |
// gravitational constant | |
var G = 0.01; | |
// computes the magnitude of the gravitational force of | |
// one object (stationaryObject) on another (movingObject) | |
function computeGravityMagnitude(stationaryObjectX, stationaryObjectY, | |
stationaryObjectMass, movingObjectX, | |
movingObjectY, movingObjectMass) { | |
var distance = getDistance(stationaryObjectX, stationaryObjectY, | |
movingObjectX, movingObjectY); | |
return (G * stationaryObjectMass * movingObjectMass) / (distance * distance); | |
} | |
// computes the direction of the gravitational force of | |
// one object (stationaryObject) on another (movingObject) | |
function computeGravityDirection(stationaryObjectX, stationaryObjectY, | |
movingObjectX, movingObjectY) { | |
var xComponent = stationaryObjectX - movingObjectX; | |
var yComponent = -(stationaryObjectY - movingObjectY); | |
var direction = Math.atan(yComponent / xComponent); | |
return correctTheta(direction, xComponent, yComponent); | |
} | |
// computes the distance between two povars (x1, y1) and (x2, y2) | |
function getDistance(x1, y1, x2, y2) { | |
return squareRoot(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))); | |
} | |
// add two vectors and return resultant vector's magnitude | |
function vectorAddMagnitude(v1Theta, v1Magnitude, | |
v2Theta, v2Magnitude) { | |
var v1MagnitudeX = getXComponent(v1Magnitude, v1Theta); | |
var v1MagnitudeY = getYComponent(v1Magnitude, v1Theta); | |
var v2MagnitudeX = getXComponent(v2Magnitude, v2Theta); | |
var v2MagnitudeY = getYComponent(v2Magnitude, v2Theta); | |
var newXComponent = v1MagnitudeX + v2MagnitudeX; | |
var newYComponent = v1MagnitudeY + v2MagnitudeY; | |
var newMagnitude = squareRoot( | |
(newXComponent * newXComponent) + | |
(newYComponent * newYComponent)); | |
return newMagnitude; | |
} | |
// add two vectors return resultant vector's theta | |
function vectorAddTheta(v1Theta, v1Magnitude, | |
v2Theta, v2Magnitude) { | |
var v1MagnitudeX = getXComponent(v1Magnitude, v1Theta); | |
var v1MagnitudeY = getYComponent(v1Magnitude, v1Theta); | |
var v2MagnitudeX = getXComponent(v2Magnitude, v2Theta); | |
var v2MagnitudeY = getYComponent(v2Magnitude, v2Theta); | |
var newXComponent = v1MagnitudeX + v2MagnitudeX; | |
var newYComponent = v1MagnitudeY + v2MagnitudeY; | |
var newTheta = Math.atan(newYComponent / newXComponent); | |
return correctTheta(newTheta, newXComponent, newYComponent); | |
} | |
// returns the X-component of the vector given by magnitude and theta | |
function getXComponent(magnitude, theta) { | |
return magnitude * cos(theta); | |
} | |
// returns the Y-component of the vector given by magnitude and theta | |
function getYComponent(magnitude, theta) { | |
return magnitude * sin(theta); | |
} | |
// Since there are two thetas for any given tangent (180 degrees off of one | |
// another), make sure we are using the correct one when doing vector addition | |
function correctTheta(theta, xComponent, yComponent) | |
{ | |
// keep theta in the 0 .. 2 Pi range | |
theta = theta % (2 * Pi); | |
// first quadrant | |
if (xComponent > 0 && yComponent > 0) { | |
if (theta < (Pi / 2) && theta > 0) { | |
return theta; | |
} | |
else { | |
return theta + Pi; | |
} | |
} | |
// second quadrant | |
else if (xComponent < 0 && yComponent > 0) { | |
if (theta < Pi && theta > (Pi / 2)) { | |
return theta; | |
} | |
else { | |
return theta + Pi; | |
} | |
} | |
// third quadrant | |
else if (xComponent < 0 && yComponent < 0) { | |
if (theta < (3 * Pi / 2) && theta > Pi) { | |
return theta; | |
} | |
else { | |
return theta + Pi; | |
} | |
} | |
return theta; | |
} | |
// flips theta over the horizontal axis | |
function flipVertically(theta) { | |
return theta + 2 * (Pi - theta); | |
} | |
// flips theta over the vertical axis | |
function flipHorizontally(theta) { | |
return (theta + Pi) - (2 * theta); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment