Added display of normals at the vertices of the triangles the kart is driving on

to the debug display, which is useful to find why interpolated normals are wrong.
This commit is contained in:
hiker 2015-04-15 07:44:50 +10:00
parent 8f090525fb
commit e8b8bbdd3b
4 changed files with 38 additions and 10 deletions

View File

@ -25,6 +25,8 @@ virtual ~btVehicleRaycaster()
btVector3 m_hitPointInWorld;
btVector3 m_hitNormalInWorld;
btScalar m_distFraction;
/** If a triangle mesh, the index of the triangle, otherwise -1. */
int m_triangle_index;
};
virtual void* castRay(const btVector3& from,const btVector3& to, btVehicleRaycasterResult& result) = 0;

View File

@ -48,6 +48,7 @@ struct btWheelInfo
btVector3 m_wheelAxleWS; // axle in worldspace
bool m_isInContact;
void* m_groundObject; //could be general void* ptr
int m_triangle_index; // if a triangle mesh was hit, the index of the triangle.
};
RaycastInfo m_raycastInfo;

View File

@ -23,6 +23,9 @@
#include "BulletDynamics/ConstraintSolver/btContactConstraint.h"
#include "karts/kart.hpp"
#include "modes/world.hpp"
#include "physics/triangle_mesh.hpp"
#include "tracks/track.hpp"
#define ROLLING_INFLUENCE_FIX
@ -260,6 +263,7 @@ btScalar btKart::rayCast(unsigned int index)
wheel.m_raycastInfo.m_contactNormalWS.normalize();
wheel.m_raycastInfo.m_isInContact = true;
///@todo for driving on dynamic/movable objects!;
wheel.m_raycastInfo.m_triangle_index = rayResults.m_triangle_index;;
wheel.m_raycastInfo.m_groundObject = &getFixedBody();
wheel.m_raycastInfo.m_suspensionLength = depth - wheel.m_wheelsRadius;
@ -975,7 +979,8 @@ void btKart::debugDraw(btIDebugDraw* debugDrawer)
for (int v=0;v<getNumWheels();v++)
{
btVector3 wheelColor(0,1,1);
if (getWheelInfo(v).m_raycastInfo.m_isInContact)
const btWheelInfo &w = getWheelInfo(v);
if (w.m_raycastInfo.m_isInContact)
{
wheelColor.setValue(0,0,1);
} else
@ -983,23 +988,40 @@ void btKart::debugDraw(btIDebugDraw* debugDrawer)
wheelColor.setValue(1,0,1);
}
btVector3 wheelPosWS = getWheelInfo(v).m_worldTransform.getOrigin();
btVector3 wheelPosWS = w.m_worldTransform.getOrigin();
btVector3 axle = btVector3(
getWheelInfo(v).m_worldTransform.getBasis()[0][getRightAxis()],
getWheelInfo(v).m_worldTransform.getBasis()[1][getRightAxis()],
getWheelInfo(v).m_worldTransform.getBasis()[2][getRightAxis()]);
w.m_worldTransform.getBasis()[0][getRightAxis()],
w.m_worldTransform.getBasis()[1][getRightAxis()],
w.m_worldTransform.getBasis()[2][getRightAxis()]);
//debug wheels (cylinders)
debugDrawer->drawLine(wheelPosWS,wheelPosWS+axle,wheelColor);
debugDrawer->drawLine(wheelPosWS,
getWheelInfo(v).m_raycastInfo.m_contactPointWS,
w.m_raycastInfo.m_contactPointWS,
wheelColor);
// Draw the (interpolated) normal of the ground at the wheel position
debugDrawer->drawLine(getWheelInfo(v).m_raycastInfo.m_contactPointWS,
getWheelInfo(v).m_raycastInfo.m_contactPointWS+
getWheelInfo(v).m_raycastInfo.m_contactNormalWS,
btVector3(1.0f, 1.0f, 1.0f));
btVector3 white(1.0f, 1.0f, 1.0f);
debugDrawer->drawLine(w.m_raycastInfo.m_contactPointWS,
w.m_raycastInfo.m_contactPointWS+
w.m_raycastInfo.m_contactNormalWS,
white);
int n = w.m_raycastInfo.m_triangle_index;
if (n > -1)
{
const TriangleMesh &tm = World::getWorld()->getTrack()->getTriangleMesh();
btVector3 *p1, *p2, *p3;
tm.getTriangle(n, &p1, &p2, &p3);
const btVector3 *n1, *n2, *n3;
tm.getNormals(n, &n1, &n2, &n3);
// Draw the normals at the vertices
debugDrawer->drawLine(*p1, *p1 + *n1, white);
debugDrawer->drawLine(*p2, *p2 + *n2, white);
debugDrawer->drawLine(*p3, *p3 + *n3, white);
// Also draw the triangle in white, it can make it easier
// to identify which triangle a wheel is on
debugDrawer->drawTriangle(*p1, *p2, *p3, white, 1.0f);
}
} // for i < getNumWheels
btVector3 yellow(1.0f, 1.0f, 0.0f);
@ -1070,6 +1092,7 @@ btScalar btKart::rayCast(btWheelInfo& wheel, const btVector3& ray)
wheel.m_raycastInfo.m_contactPointWS = rayResults.m_hitPointInWorld;
wheel.m_raycastInfo.m_contactNormalWS = rayResults.m_hitNormalInWorld;
wheel.m_raycastInfo.m_isInContact = true;
wheel.m_raycastInfo.m_triangle_index = rayResults.m_triangle_index;
}
return depth;

View File

@ -70,6 +70,7 @@ void* btKartRaycaster::castRay(const btVector3& from, const btVector3& to,
result.m_hitNormalInWorld = rayCallback.m_hitNormalWorld;
result.m_hitNormalInWorld.normalize();
result.m_distFraction = rayCallback.m_closestHitFraction;
result.m_triangle_index = -1;
const TriangleMesh &tm =
World::getWorld()->getTrack()->getTriangleMesh();
if(m_smooth_normals &&
@ -79,6 +80,7 @@ void* btKartRaycaster::castRay(const btVector3& from, const btVector3& to,
#ifdef DEBUG_NORMALS
btVector3 n=result.m_hitNormalInWorld;
#endif
result.m_triangle_index = rayCallback.getTriangleIndex();
result.m_hitNormalInWorld =
tm.getInterpolatedNormal(rayCallback.getTriangleIndex(),
result.m_hitPointInWorld);