// Prueba para simulacion de particulas...

// incluir este archivo mediante:
// include("Data/Scenes/particule.snf");

box_path = "../Models/cube.obj";
quad_path = "../Models/quad.obj";
box_geometry = Model(box_path);
quad_geometry = Model(quad_path);

t = translate(0, 0.5, 0);

scene = 
	Group(
		Group(
			t,
			box_geometry
		),
		Group(
			translate(0, -0.5, 0),
			rotate(-90, 1, 0, 0),
			scale(4, 4, 4),
			quad_geometry
		),
		Group(
			translate(1, 4, 1),
			Light()
		)
	);

camera = Camera();
camera.setActive();
camera.setPos(0, 10, 8);
/*
//a = 0.0;
a = 0.2;
g = 0.01;
dir = 1;
isMoving = true;
// 
def SimulateGravity() {
	y = t.getY();
	t.setY(y + a);
//	if (dir == -1)
	
	//else if (dir == 1) a += g;
	if (collisionDetection(box_geometry, quad_geometry)) {
		a *= -0.70;
		if (abs(a) < 0.1) { isMoving = false; a = 0;}
		t.setY(a);
		dir = 1;
		// a = 0; g = 0;
	}
	if (isMoving) a -= g;
}
*/

g = 0.01;
d = (0, 1, 0);
a = 0.2;
move = true;

def SimulateGravity() {
  t.setX(t.getX() + d.get(0) * a);
  t.setY(t.getY() + d.get(1) * a);
  t.setZ(t.getZ() + d.get(2) * a);
  
  //if (abs(a) < 0.01) d.set(1, -d.get(1));
  
  if (collisionDetection(box_geometry, quad_geometry)) {
	a *= -0.6;
	//t.setY(a);
	t.setY(0.00001);
	if (abs(a) < 0.05) { a = 0; move = false; }
	//d.set(1, -d.get(1));
  }
  if (move) a -= g;
}

Timer(20, -1, SimulateGravity(), SimulateGravity());

///////////////////////////////////////////////////////////
// Some mathematical functions...

def Dot(v1, v2) {
  return v1.get(0) * v2.get(0) +
         v1.get(1) * v2.get(1) +
         v1.get(2) + v2.get(2);
}

def refract(I, N, irr) {
  IdotN = dot(I, N);
  k = sqrt(1 - irr * irr * (1 - IdotN * IdotN));
  resul = (0, 0, 0);
  result.set(0, I.get(0) * irr - N.get(0) * (irr * IdotN + k));
  result.set(1, I.get(1) * irr - N.get(1) * (irr * IdotN + k));
  result.set(2, I.get(2) * irr - N.get(2) * (irr * IdotN + k));
  return result;
}
