Moderators: Sascha Willems, walaber
Ogre::Vector3 PhysicsManager::computeThrowVelocity(Vector3 &start, Vector3 &stop)
{
//prepare values for angle computation
// the distance between the vectors
Real staightDistance = start.distance(stop);
// height on y
Real height = start.y - stop.y;
// distance on local x
Real distance = Math::Sqrt(staightDistance*staightDistance - height*height);
Real speed = getMaxThrowSpeed();
Real gravity = Math::Abs(SettingsManager::getSingleton().getGravity());
// compute angles from horizontal
Radian angle = Math::ATan((Math::Sqr(speed)+Math::Sqrt(Math::Pow(speed,4) - gravity*(gravity*Math::Sqr(distance)+2*height*Math::Sqr(speed))))/(gravity*distance));
//Radian angle = Math::ATan((Math::Sqr(speed)-Math::Sqrt(Math::Pow(speed,4) - gravity*(gravity*Math::Sqr(distance)+2*height*Math::Sqr(speed))))/(gravity*distance));
// compute velocity vector
Vector3 velocity = start - stop;
velocity.y = velocity.x * Math::Tan(angle);
velocity.normalise();
velocity = velocity * speed;
return velocity;
}
inline float GetLaunchAngle (float vel, float grav, float diffH, float diffV)
{
vel *= vel;
float r = sqrt (vel*vel - grav * (grav * diffH*diffH + 2.0*diffV*vel));
float s = vel + r; // two solutions: vel - r would take the lower angle
float t = s / (grav*diffH);
return atan(t);
}
inline sVec3 GetProjectileToTargetVelocity (sVec3 target, sVec3 launchPos, float launchVel)
{
// assuming gravity vec (0, GRAVITY_Y, 0)
sVec3 diff = target - launchPos;
float diffV = diff[1];
float diffH = sqrt (diff[0] * diff[0] + diff[2] * diff[2]);
// todo: handle out of reach and numerical special cases
float angle = GetLaunchAngle (launchVel, fabs((float)GRAVITY_Y), diffH, diffV);
// construct velocity vector to apply to projectile...
sVec3 vv(diff[0], 0, diff[2]); vv /= diffH; // project to ground plane and normalize
vv *= cos(angle);
vv[1] += sin(angle);
return vv * launchVel;
}
sVec3 vv(diff[0], 0, diff[2]); vv /= diffH;
vv *= cos(angle);
vv[1] += sin(angle);
velocity.y = velocity.x * Math::Tan(angle);
velocity.y = distance * Math::Tan(angle);
Users browsing this forum: No registered users and 3 guests