Created
February 26, 2013 21:29
-
-
Save syg/5042386 to your computer and use it in GitHub Desktop.
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
"bodyVelocityLoopified": function bodyVelocityLoopified(index) { | |
var pos = NBody.private.pos; | |
var data = NBody.private.vel; | |
var deltaTime = NBody.Constant.deltaTime; | |
var epsSqr = NBody.Constant.epsSqr; | |
var time = NBody.time; | |
var shape = data.shape[0]; | |
var newVel; | |
var newX, newY, newZ, newM; | |
var newX2, newY2, newZ2, newM2; | |
var cX = Math.cos(time / 22) * -4200; | |
var cY = Math.sin(time / 14) * 9200; | |
var cZ = Math.sin(time / 27) * 6000; | |
// pull to center | |
var maxDistance = 3400; | |
var pullStrength = .042; | |
var speedLimit = 8; | |
// zones | |
var zone = 400; | |
var repel = 100; | |
var align = 300; | |
var attract = 100; | |
if (time < 500) { | |
speedLimit = .01; | |
var attractPower = 100.9; | |
} | |
else { | |
speedLimit = .01; | |
attractPower = 10.9; | |
} | |
var zoneSqrd = zone * zone + zone * zone + zone * zone; | |
var accX = 0, accY = 0, accZ = 0; | |
var accX2 = 0, accY2 = 0, accZ2 = 0; | |
var i; | |
var debug = 0; | |
// define particle 1 center distance | |
var dirToCenterX = cX - pos.get(index)[0]; | |
var dirToCenterY = cY - pos.get(index)[1]; | |
var dirToCenterZ = cZ - pos.get(index)[2]; | |
var distanceSquaredTo = dirToCenterX * dirToCenterX + dirToCenterY * dirToCenterY + dirToCenterZ * dirToCenterZ; | |
var distToCenter = Math.sqrt(distanceSquaredTo); | |
// orient to center | |
if (distToCenter > maxDistance) { | |
var vel = (distToCenter - maxDistance) * pullStrength; | |
if (time < 200) | |
vel = .2; | |
else vel = (distToCenter - maxDistance) * pullStrength; | |
accX += (dirToCenterX / distToCenter) * vel; | |
accY += (dirToCenterY / distToCenter) * vel; | |
accZ += (dirToCenterZ / distToCenter) * vel; | |
} | |
for (i = 0; i < shape; i = i + 1) { | |
// DIRS P1 | |
var rx = pos.get(i)[0] - pos.get(index)[0]; | |
var ry = pos.get(i)[1] - pos.get(index)[1]; | |
var rz = pos.get(i)[2] - pos.get(index)[2]; | |
// make shure we are not testing the particle against its own position | |
var areSame = 0; | |
if (pos.get(i)[0] == pos.get(index)[0] && pos.get(i)[1] == pos.get(index)[1] && pos.get(i)[2] == pos.get(index)[2]) | |
areSame += 1; | |
var distSqrd = rx * rx + ry * ry + rz * rz; | |
// cant use eqals to test, only <= or >= WTF | |
if (distSqrd < zoneSqrd && areSame <= 0) { | |
var length = Math.sqrt(distSqrd); | |
var percent = distSqrd / zoneSqrd; | |
if (distSqrd < align) { //align | |
var threshDelta = align - repel; | |
var adjustedPercent = (percent - repel) / threshDelta; | |
var Q = (.5 - Math.cos(adjustedPercent * 3.14159265 * 2) * .5 + .5) * 100.9; | |
// get velocity 2 | |
var velX2 = data.get(i)[4]; | |
var velY2 = data.get(i)[5]; | |
var velZ2 = data.get(i)[6]; | |
var velLength2 = Math.sqrt(velX2 * velX2 + velY2 * velY2 + velZ2 * velZ2); | |
// normalize vel2 and multiply by factor | |
velX2 = (velX2 / velLength2) * Q; | |
velY2 = (velY2 / velLength2) * Q; | |
velZ2 = (velZ2 / velLength2) * Q; | |
// get own velocity | |
var velX = data.get(i)[0]; | |
var velY = data.get(i)[1]; | |
var velZ = data.get(i)[2]; | |
var velLength = Math.sqrt(velX * velX + velY * velY + velZ * velZ); | |
// normalize own velocity | |
velX = (velX / velLength) * Q; | |
velY = (velY / velLength) * Q; | |
velZ = (velZ / velLength) * Q; | |
accX += velX2; | |
accY += velY2; | |
accZ += velZ2; | |
accX2 += velX; | |
accY2 += velY; | |
accZ2 += velZ; | |
} else { // attract | |
var threshDelta2 = 1 - align; | |
var adjustedPercent2 = (percent - align) / threshDelta2; | |
var C = (1 - (Math.cos(adjustedPercent2 * 3.14159265 * 2) * 0.5 + 0.5)) * attractPower; | |
// normalize the distance vector | |
var dx = (rx / (length)) * C; | |
var dy = (ry / (length)) * C; | |
var dz = (rz / (length)) * C; | |
debug = 1.1; | |
accX += dx; | |
accY += dy; | |
accZ += dz; | |
accX2 -= dx; | |
accY2 -= dy; | |
accZ2 -= dz; | |
} | |
} | |
} | |
// Speed limits | |
var accSquared = accX * accX + accY * accY + accZ * accZ; | |
if (accSquared > speedLimit) { | |
accX = accX * .015; | |
accY = accY * .015; | |
accZ = accZ * .015; | |
} | |
var accSquared2 = accX2 * accX2 + accY2 * accY2 + accZ2 * accZ2; | |
if (accSquared2 > speedLimit) { | |
accX2 = accX2 * .015; | |
accY2 = accY2 * .015; | |
accZ2 = accZ2 * .015; | |
} | |
// Caclulate new velocity | |
newX = (data.get(index)[0]) + accX; | |
newY = (data.get(index)[1]) + accY; | |
newZ = (data.get(index)[2]) + accZ; | |
newX2 = (data.get(index)[3]) + accX2; | |
newY2 = (data.get(index)[4]) + accY2; | |
newZ2 = (data.get(index)[5]) + accZ2; | |
return [newX, newY, newZ, newX2, newY2, newZ2]; | |
}, | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment