Commit 37ef6a1d authored by amandaghassaei's avatar amandaghassaei

friction in gpu

parent 49887e53
......@@ -169,15 +169,15 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
var gravityVect = this.get("gravityVector").clone().normalize().multiplyScalar(this.get("gravity"));
var groundHeight = this.get("groundHeight");
var friction = this.get("friction");
emSimLattice.setConstants(dt, gravityVect, groundHeight);
emSimLattice.setConstants(dt, gravityVect, groundHeight, friction);
three.startAnimationLoop(function(){
for (var i=0;i<renderRate-1;i++){
self.time += dt;
emSimLattice.iter(dt, self.time, gravityVect, groundHeight, false);
emSimLattice.iter(dt, self.time, gravityVect, groundHeight, friction, false);
}
self.time += dt;
emSimLattice.iter(dt, self.time, gravityVect, groundHeight, true);
emSimLattice.iter(dt, self.time, gravityVect, groundHeight, friction, true);
//if (self._getViewMode() == "translation"){
// self.calcTranslation();
//}
......
......@@ -452,16 +452,17 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
return !state;
},
setConstants: function(dt, gravity, groundHeight){
setConstants: function(dt, gravity, groundHeight, friction){
gpuMath.setProgram("velocityCalc");
gpuMath.setUniformForProgram("velocityCalc", "u_dt", dt, "1f");
gpuMath.setUniformForProgram("velocityCalc", "u_gravity", [gravity.x, gravity.y, gravity.z], "3f");
gpuMath.setUniformForProgram("velocityCalc", "u_groundHeight", groundHeight, "1f");
gpuMath.setUniformForProgram("velocityCalc", "u_friction", friction, "1f");
gpuMath.setProgram("positionCalc");
gpuMath.setUniformForProgram("positionCalc", "u_dt", dt, "1f");
},
iter: function(dt, time, gravity, groundHeight, shouldRender){
iter: function(dt, time, gravity, groundHeight, friction, shouldRender){
gpuMath.step("quaternionCalc", ["u_lastTranslation", "u_lastQuaternion", "u_fixed", "u_neighborsXMapping",
"u_neighborsYMapping", "u_compositeKs"], "u_quaternion", "u_wires", "u_wiresMeta", time);
......@@ -623,7 +624,17 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
//simple collision detection
var zPosition = this.originalPosition[rgbaIndex+2]+translation[2]*multiplier-groundHeight;
var collisionK = 1;
if (zPosition<0) force[2] += -zPosition*collisionK-velocity[2]*collisionK/10;
if (zPosition<0) {
var normalForce = -zPosition*collisionK-velocity[2]*collisionK/10;
force[2] += normalForce;
if (friction) {
var mu = 10;
if (velocity[0] > 0) force[0] -= mu * normalForce;
else if (velocity[0] < 0) force[0] += mu * normalForce;
if (velocity[1] > 0) force[1] -= mu * normalForce;
else if (velocity[1] < 0) force[1] += mu * normalForce;
}
}
var acceleration = [force[0]/mass, force[1]/mass, force[2]/mass];
velocity = [velocity[0] + acceleration[0]*dt, velocity[1] + acceleration[1]*dt, velocity[2] + acceleration[2]*dt];
......
......@@ -10,6 +10,7 @@ uniform vec3 u_latticePitch;
uniform float u_wiresMetaLength;
uniform float u_time;
uniform float u_groundHeight;
uniform float u_friction;
uniform sampler2D u_lastVelocity;
......@@ -125,7 +126,17 @@ void main(){
//simple collision
float zPosition = texture2D(u_originalPosition, scaledFragCoord).z + lastTranslation.z*u_multiplier - u_groundHeight;
float collisionK = 1.0;
if (zPosition < 0.0) force.z += -zPosition*collisionK-lastVelocity.z*collisionK/10.0;
if (zPosition < 0.0) {
float normalForce = -zPosition*collisionK-lastVelocity.z*collisionK/10.0;
force.z += normalForce;
if (u_friction > 0.5){
float mu = 10.0;
if (lastVelocity.x > 0.0) force.x -= mu * normalForce;
else if (lastVelocity.x < 0.0) force.x += mu * normalForce;
if (lastVelocity.y > 0.0) force.y -= mu * normalForce;
else if (lastVelocity.y < 0.0) force.y += mu * normalForce;
}
}
for (float i=0.0;i<2.0;i+=1.0){
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment