Example #1
0
void fallingSphere(Sphere & s,position2di mousemove,f32 frameDeltaTime,f32 friction){
  // This handler would accelerate the sphere downwards, ignoring all
  // mouse movements or speed in horizontal directions:
  vector3df p=s.getPosition();
  vector3df v=s.getVelocity();
  v.X=v.Z=0.f;
  v.Y-=1.f*frameDeltaTime; // falling;
  p+=v;
  s.setPosition(p);
  s.setVelocity(v);
}  
Example #2
0
//THIS NEEDS TESTING MAJORLY
void Sphere::ResolveCollision(Sphere& rhs, const float& time){
	
	//Calculate the depth of the penetration
	float penDepth = this->radius + rhs.radius - this->position.GetDistance(rhs.position);

	//Calculate the contact normal
	Vector3 conNormal = (this->position - rhs.position).GetNormalised();

	//Calculate the point of contact
	Vector3 conPoint = this->position - conNormal * (this->radius - penDepth);

	//Calculate the rough combined elasticity of the two spheres in
	//the collision. An application of the smoke and mirrors technique!
	float elasticity = (this->elasticity + rhs.elasticity) * 0.5f;
	
	//Calculate the velocities of both spheres
	Vector3 va = this->getVelocity(time);
	Vector3 vb = rhs.getVelocity(time);

	//Calculate the Vn of the collision
	float veloNormal = (va - vb).DotProduct(conNormal);

	//Calculate the impulse of the collision
	float impulse = ( -(1 + elasticity) * veloNormal ) / conNormal.DotProduct(conNormal * ( (1 / this->mass) + (1 / rhs.mass) ));
	
	//Calculate the resultant velocties of the collision
	Vector3 vaAfter = va + ((impulse/this->mass) * conNormal);
	Vector3 vbAfter = vb - ((impulse/rhs.mass) * conNormal);

	//Translate the shapes out of contact with each other
	this->translate(conNormal * (penDepth*0.5f));
	rhs.translate(0.0f - (conNormal * (penDepth*0.5f)));

	//Apply the calculated velocties to them
	this->setVelocity(vaAfter, time);
	rhs.setVelocity(vbAfter, time);
}