|
#include "StdAfx.h"
#include "BounceBalls.h"
#include "Ball.h"
#include "Situation.h"
#include <iostream>
CBounceBalls::CBounceBalls(CBall *b0, CBall *b1)
{
this->b0 = b0;
this->b1 = b1;
}
CBounceBalls::~CBounceBalls(void)
{
}
void CBounceBalls::PredictCollisionBetweenBalls(double time)
{
const CSituation &s0( b0->situation );
CSituation s1( b1->situation );
s1.SetBaseTime( s0.GetBaseTime() );
osg::Vec3d v = s1.GetVelocity() - s0.GetVelocity();
osg::Vec3d s = s1.GetBasePosition() - s0.GetBasePosition();
double s2 = s * s; // dot product
double v2 = v * v; // "
double sv = s * v; // "
double r = b0->radius + b1->radius;
double r2 = r * r;
double sqr = sv*sv - v2 * (s2 - r2 );
if( sqr > 0.00001 )
{
double t = s0.GetBaseTime() + ( - sqrt( sqr ) - sv ) / v2;
if( t > time )
{
ChangeTime( t );
Enable( true );
//std::cout << "Next collision predicted at " << Time() << std::endl;
}
else
// Collided in the past
Enable( false );
}
else
// Dont collide
Enable( false );
}
CSimulator *CBounceBalls::Simulator()
{
return b0->situation.Simulator();
}
void CBounceBalls::Expire()
{
CSituation &s0( b0->situation );
CSituation &s1( b1->situation );
//std::cout << "Energies at " << Time() << " are " << s0.v0.length2() + s1.v0.length2() << std::endl;
s0.SetBaseTime( Time() );
s1.SetBaseTime( Time() );
osg::Vec3d action = s1.GetBasePosition() - s0.GetBasePosition();
action /= action.length();
double speed = (s1.GetVelocity() - s0.GetVelocity()) * action;
osg::Vec3d impulse = action * speed;
s0.IncreaseVelocity( impulse );
s1.IncreaseVelocity( -impulse );
//std::cout << "+Energies at " << Time() << " are " << s0.v0.length2() + s1.v0.length2() << std::endl;
//osg::Vec3d ds = s0.Position(Time()) - s1.Position(Time());
//double d = sqrt( ds * ds );
//std::cout << "Distance at " << Time() << " is " << d << std::endl;
b0->PredictChanges();
b1->PredictChanges();
//Enable(false);
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I develop rail signalling simulation technology at EDM where I use C++ and C#, but also love to dabble in functional programming languages such as Clean and Cat.