Moved the 'visual' raycasts (for skidmarks) out of the update loop

into the updateGraphics call tree.
This commit is contained in:
hiker
2018-06-06 08:13:53 +10:00
parent 9ea8dae693
commit 80ab0b6cda
4 changed files with 75 additions and 68 deletions

View File

@@ -105,10 +105,12 @@ void SkidMarks::update(float dt, bool force_skid_marks,
// Get raycast information
// -----------------------
const btKart *vehicle = m_kart.getVehicle();
const Vec3& raycast_right = vehicle->getVisualContactPoint(2);
const Vec3& raycast_left = vehicle->getVisualContactPoint(3);
btKart *vehicle = m_kart.getVehicle();
Vec3 raycast_right;
Vec3 raycast_left;
vehicle->getVisualContactPoint(m_kart.getSkidding()->getVisualSkidRotation(),
&raycast_left, &raycast_right);
Vec3 delta = raycast_right - raycast_left;
// The kart is making skid marks when it's:

View File

@@ -1491,7 +1491,8 @@ void Kart::update(int ticks)
Log::verbose("physicsafter", "%s t %f %d xyz(9-11) %f %f %f %f %f %f "
"v(16-18) %f %f %f steerf(20) %f maxangle(22) %f speed(24) %f "
"steering(26-27) %f %f clock(29) %lf skidstate(31) %d factor(33) %f "
"maxspeed(35) %f engf(37) %f braketick(39) %d brakes(41) %d",
"maxspeed(35) %f engf(37) %f braketick(39) %d brakes(41) %d heading(43) %f "
"noderot(45) %f suslen %f",
getIdent().c_str(),
World::getWorld()->getTime(), World::getWorld()->getTimeTicks(),
getXYZ().getX(), getXYZ().getY(), getXYZ().getZ(),
@@ -1510,7 +1511,10 @@ void Kart::update(int ticks)
m_max_speed->getCurrentMaxSpeed(),
m_max_speed->getCurrentAdditionalEngineForce(), // 37
m_brake_ticks, //39
m_controls.getButtonsCompressed()
m_controls.getButtonsCompressed(), //41
getHeading(), //43
m_node->getAbsoluteTransformation().getRotationDegrees().Y, //45
m_vehicle->getWheelInfo(0).m_raycastInfo.m_suspensionLength
);
#endif
// After the physics step was done, the position of the wheels (as stored
@@ -1602,7 +1606,8 @@ void Kart::update(int ticks)
#ifdef DEBUG
if(UserConfigParams::m_material_debug)
{
Log::info("Kart","%s\tfraction %f\ttime %f.",
Log::info("Kart","World %d %s\tfraction %f\ttime %d.",
World::getWorld()->getTimeTicks(),
material->getTexFname().c_str(),
material->getMaxSpeedFraction(),
material->getSlowDownTicks() );
@@ -2423,7 +2428,6 @@ void Kart::updatePhysics(int ticks)
m_skidding->update(ticks, isOnGround(), m_controls.getSteer(),
m_controls.getSkidControl());
m_vehicle->setVisualRotation(m_skidding->getVisualSkidRotation());
if( ( m_skidding->getSkidState() == Skidding::SKID_ACCUMULATE_LEFT ||
m_skidding->getSkidState() == Skidding::SKID_ACCUMULATE_RIGHT ) &&
!m_skidding->isJumping() )

View File

@@ -90,7 +90,6 @@ btWheelInfo& btKart::addWheel(const btVector3& connectionPointCS,
ci.m_maxSuspensionForce = tuning.m_maxSuspensionForce;
m_wheelInfo.push_back( btWheelInfo(ci));
m_visual_contact_point.push_back(btVector3());
btWheelInfo& wheel = m_wheelInfo[getNumWheels()-1];
@@ -124,7 +123,6 @@ void btKart::reset()
m_time_additional_impulse = 0;
m_additional_rotation = btVector3(0,0,0);
m_time_additional_rotation = 0;
m_visual_rotation = 0;
m_max_speed = -1.0f;
m_min_speed = 0.0f;
@@ -219,7 +217,7 @@ void btKart::updateWheelTransformsWS(btWheelInfo& wheel,
*/
void btKart::updateAllWheelTransformsWS()
{
for (unsigned int i = 0; i < m_wheelInfo.size(); i++)
for (unsigned int i = 0; i < (unsigned int)m_wheelInfo.size(); i++)
{
btWheelInfo &wheel = m_wheelInfo[i];
updateWheelTransformsWS(wheel, false, 1.0f);
@@ -332,44 +330,74 @@ btScalar btKart::rayCast(unsigned int index, float fraction)
wheel.m_clippedInvContactDotSuspension = btScalar(1.0);
}
#define USE_VISUAL
#ifndef USE_VISUAL
m_visual_contact_point[index] = rayResults.m_hitPointInWorld;
#else
if(index==2 || index==3)
{
btTransform chassisTrans = getChassisWorldTransform();
if (getRigidBody()->getMotionState())
{
getRigidBody()->getMotionState()->getWorldTransform(chassisTrans);
}
btQuaternion q(m_visual_rotation, 0, 0);
btQuaternion rot_new = chassisTrans.getRotation() * q;
chassisTrans.setRotation(rot_new);
btVector3 pos = m_kart->getKartModel()->getWheelGraphicsPosition(index);
pos.setZ(pos.getZ()*0.9f);
btVector3 source = chassisTrans( pos );
btVector3 target = source + rayvector;
btVehicleRaycaster::btVehicleRaycasterResult rayResults;
void* object = m_vehicleRaycaster->castRay(source,target,rayResults);
m_visual_contact_point[index] = rayResults.m_hitPointInWorld;
m_visual_contact_point[index-2] = source;
m_visual_wheels_touch_ground &= (object!=NULL);
}
#endif
if(m_chassisBody->getBroadphaseHandle())
{
m_chassisBody->getBroadphaseHandle()->m_collisionFilterGroup
= old_group;
}
return depth;
} // rayCast
// ----------------------------------------------------------------------------
/** Returns the contact point of a visual wheel.
* \param n Index of the wheel, must be 2 or 3 since only the two rear
* wheels define the visual position
*/
void btKart::getVisualContactPoint(float visual_rotation,
btVector3 *left, btVector3 *right)
{
btAssert(m_vehicleRaycaster);
m_visual_wheels_touch_ground = true;
short int old_group = 0;
for (int index = 2; index <= 3; index++)
{
// Map index 0-1 to wheel 2-3 (which are the rear wheels)
btWheelInfo &wheel = m_wheelInfo[index];
updateWheelTransformsWS(wheel, false);
if (m_chassisBody->getBroadphaseHandle())
{
old_group = m_chassisBody->getBroadphaseHandle()
->m_collisionFilterGroup;
m_chassisBody->getBroadphaseHandle()->m_collisionFilterGroup = 0;
}
btScalar max_susp_len = wheel.getSuspensionRestLength()
+ wheel.m_maxSuspensionTravel;
// Do a slightly longer raycast to see if the kart might soon hit the
// ground and some 'cushioning' is needed to avoid that the chassis
// hits the ground.
btScalar raylen = max_susp_len + 0.5f;
btVector3 rayvector = wheel.m_raycastInfo.m_wheelDirectionWS * (raylen);
btTransform chassisTrans = getChassisWorldTransform();
if (getRigidBody()->getMotionState())
{
getRigidBody()->getMotionState()->getWorldTransform(chassisTrans);
}
btQuaternion q(visual_rotation, 0, 0);
btQuaternion rot_new = chassisTrans.getRotation() * q;
chassisTrans.setRotation(rot_new);
btVector3 pos = m_kart->getKartModel()->getWheelGraphicsPosition(index);
pos.setZ(pos.getZ()*0.9f);
btVector3 source = chassisTrans(pos);
btVector3 target = source + rayvector;
btVehicleRaycaster::btVehicleRaycasterResult rayResults;
void* object = m_vehicleRaycaster->castRay(source, target, rayResults);
if(index == 2) *left = rayResults.m_hitPointInWorld;
else *right = rayResults.m_hitPointInWorld;
m_visual_wheels_touch_ground &= (object != NULL);
} // for index in [2,3]
if (m_chassisBody->getBroadphaseHandle())
{
m_chassisBody->getBroadphaseHandle()->m_collisionFilterGroup = old_group;
}
} // getVisualContactPoint
// ----------------------------------------------------------------------------
const btTransform& btKart::getChassisWorldTransform() const
{
@@ -970,9 +998,6 @@ void btKart::debugDraw(btIDebugDraw* debugDrawer)
}
} // for i < getNumWheels
btVector3 yellow(1.0f, 1.0f, 0.0f);
debugDrawer->drawLine(m_visual_contact_point[0], m_visual_contact_point[2], yellow);
debugDrawer->drawLine(m_visual_contact_point[1], m_visual_contact_point[3], yellow);
} // debugDraw

View File

@@ -105,11 +105,6 @@ private:
* properties. */
Kart *m_kart;
/** A visual rotation applied to the kart (for skidding).
* The physics use this to provide proper wheel contact points
* for skid marks. */
float m_visual_rotation;
/** Minimum speed for the kart. Used e.g. for zippers. Setting this value
* will potentially instantaneously accelerate the kart to the minimum
* speed requested (in the next physics step). */
@@ -122,9 +117,6 @@ private:
/** True if the visual wheels touch the ground. */
bool m_visual_wheels_touch_ground;
/** Contact point of the visual wheel position. */
btAlignedObjectArray<btVector3> m_visual_contact_point;
btAlignedObjectArray<btWheelInfo> m_wheelInfo;
void defaultInit();
@@ -175,23 +167,15 @@ public:
void instantSpeedIncreaseTo(btScalar speed);
void adjustSpeed(btScalar min_speed, btScalar max_speed);
void updateAllWheelPositions();
// ------------------------------------------------------------------------
void getVisualContactPoint(float visual_rotation,
btVector3 *left, btVector3 *right);
// ------------------------------------------------------------------------
/** Returns true if both rear visual wheels touch the ground. */
bool visualWheelsTouchGround() const
{
return m_visual_wheels_touch_ground;
} // visualWheelsTouchGround
// ------------------------------------------------------------------------
/** Returns the contact point of a visual wheel.
* \param n Index of the wheel, must be 2 or 3 since only the two rear
* wheels define the visual position
*/
const btVector3& getVisualContactPoint(int n) const
{
assert(n>=2 && n<=3);
return m_visual_contact_point[n];
} // getVisualContactPoint
// ------------------------------------------------------------------------
/** btActionInterface interface. */
virtual void updateAction(btCollisionWorld* collisionWorld,
btScalar step)
@@ -247,14 +231,6 @@ public:
/** Returns the time an additional impulse is activated. */
float getCentralImpulseTime() const { return m_time_additional_impulse; }
// ------------------------------------------------------------------------
/** Sets a visual rotation to be applied, which the physics use to provide
* the location where the graphical wheels touch the ground (for
* skidmarks). */
void setVisualRotation(float angle)
{
m_visual_rotation = angle;
} // setVisualRotation
// ------------------------------------------------------------------------
/** Sets a rotation that is applied over a certain amount of time (to avoid
* a too rapid changes in the kart).
* \param t Time for the rotation to be applied.