A rocket following the tracks height. Not Homing Missile.
Posted
by confusedEj
on Stack Overflow
See other posts from Stack Overflow
or by confusedEj
Published on 2010-03-12T23:37:06Z
Indexed on
2010/03/12
23:37 UTC
Read the original article
Hit count: 325
What I am trying to create is a rocket that will hug the track in a straight direction. ie) The rocket travels in a straight direction and can orientate based on its local x axis. This is so it can go up/down ramps and never hit the ground.
Currently I am using PhysX opengl and C++.
This is the method I'm trying right now: 1. Ray cast from ahead of the missile (ray casting downwards) 2. If the ray cast is less then the expected ray cast length, then I have to orientate up. 3. If the ray cast is more then the expected ray cast length, then I have to orientate down.
Now the problem, I am having is that my missile is orientating at an arbitary angle (I'm giving it 1 degrees.) Though I think this is a bad approach because the amount of frames in the game is not as much as I would think there would be. So the rocket would run into a ramp.
My main question is: is there a better way of approaching this and how?
NxVec3 frontRayLoc = m_rocketConfig->getValueForKey<NxVec3>("r_frontRayCastLocation");
float threshhold = m_rocketConfig->getValueForKey<float>("r_angleThreshhold");
float predRayCastHeight = m_rocketConfig->getValueForKey<float>("r_predRayCastHeight");
NxVec3 rayGlobalPos_1 = m_actor->getGlobalPosition() + m_actor->getGlobalOrientation() * frontRayLoc;
NxVec3 dir = m_actor->getGlobalOrientation() * NxVec3(0,-1.0,0);
NxReal dist1 = castRay(rayGlobalPos_1, dir);
// Get the percentage difference
float actualFrontHeight = abs(1 - (dist1/predRayCastHeight));
// See if the percentage difference is greater then threshold
// Also check if we are being shot off track
if ((actualFrontHeight > threshhold) && (dist1 != m_rayMaxDist)){
// Dip Down
if (dist1 > predRayCastHeight){
printf("DOWN - Distance 1: %f\n", dist1);
// Get axis of rotation
NxVec3 newAxis = m_actor->getGlobalOrientation() * NxVec3(1.0,0,0.0);
// Rotate based on that axis
m_orientateAngle = -1.0 * m_orientateAngle; // For rotating clockwise
NxQuat newOrientation(m_orientateAngle, newAxis);
NxMat33 orientation(newOrientation);
m_orientation = m_orientation * orientation;
// Orientate the linear velocity to keep speed of rocket and direct away from road
NxVec3 linVel = m_actor->getLinearVelocity();
m_actor->setLinearVelocity(m_orientation * linVel);
}
// Go Up
else if (dist1 < predRayCastHeight){
printf("UP - Distance 1: %f\n", dist1);
// Get axis of rotation
NxVec3 newAxis = m_actor->getGlobalOrientation() * NxVec3(1.0,0,0.0);
// Rotate around axis
NxQuat newOrientation(m_orientateAngle, newAxis);
m_actor->setGlobalOrientationQuat(newOrientation);
NxMat33 orientation(newOrientation);
m_orientation = m_orientation * orientation;
// Orientate the linear velocity to keep speed of rocket and direct away from road
NxVec3 linVel = m_actor->getLinearVelocity();
m_actor->setLinearVelocity(m_orientation*linVel);
}
m_actor->setGlobalOrientation(m_orientation);
}
Thanks for the support :)
© Stack Overflow or respective owner