From bf135095d8f148d1024e61f95b6f0a0af62bb45b Mon Sep 17 00:00:00 2001 From: hiker Date: Fri, 12 Sep 2014 08:37:03 +1000 Subject: [PATCH] Updated documentation, removed commented out code. --- src/physics/physical_object.cpp | 4 ---- src/physics/triangle_mesh.cpp | 7 ++++--- src/physics/triangle_mesh.hpp | 7 +++++++ src/tracks/terrain_info.cpp | 5 ++++- src/tracks/track_object_manager.cpp | 21 +++++++++++++++++++++ 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/physics/physical_object.cpp b/src/physics/physical_object.cpp index 0562c8456..7f199de2d 100644 --- a/src/physics/physical_object.cpp +++ b/src/physics/physical_object.cpp @@ -531,13 +531,9 @@ bool PhysicalObject::castRay(const btVector3 &from, const btVector3 &to, Log::warn("PhysicalObject", "Can only raycast against 'exact' meshes."); return false; } - Vec3 dxyz(m_init_xyz); - //bool result = m_triangle_mesh->castRay(from-dxyz, to-dxyz, hit_point, bool result = m_triangle_mesh->castRay(from, to, hit_point, material, normal, interpolate_normal); -// if(result) -// *hit_point += (btVector3)dxyz; return result; } // castRay diff --git a/src/physics/triangle_mesh.cpp b/src/physics/triangle_mesh.cpp index 90a683d5f..e6dd840ca 100644 --- a/src/physics/triangle_mesh.cpp +++ b/src/physics/triangle_mesh.cpp @@ -272,7 +272,7 @@ btVector3 TriangleMesh::getInterpolatedNormal(unsigned int index, // ---------------------------------------------------------------------------- /** Casts a ray from 'from' to 'to'. If a triangle of this mesh was hit, * xyz and material will be set. - * \param from/to The from and to position for the raycast/ + * \param from/to The from and to position for the raycast. * \param xyz The position in world where the ray hit. * \param material The material of the mesh that was hit. * \param normal The intrapolated normal at that position. @@ -302,9 +302,11 @@ bool TriangleMesh::castRay(const btVector3 &from, const btVector3 &to, trans_to.setOrigin(to); btTransform world_trans; - world_trans.setIdentity(); + // If there is a body, take the current transform from the body. if(m_body) world_trans = m_body->getWorldTransform(); + else + world_trans.setIdentity(); btCollisionWorld::ClosestRayResultCallback result(from, to); @@ -338,7 +340,6 @@ bool TriangleMesh::castRay(const btVector3 &from, const btVector3 &to, // If this is a rigid body, m_collision_object is NULL, and the // rigid body is the actual collision object. btCollisionWorld::rayTestSingle(trans_from, trans_to, -// m_body ? m_body : m_collision_object, m_collision_object ? m_collision_object : m_body, m_collision_shape, world_trans, ray_callback); diff --git a/src/physics/triangle_mesh.hpp b/src/physics/triangle_mesh.hpp index af53cc928..7bef215a9 100644 --- a/src/physics/triangle_mesh.hpp +++ b/src/physics/triangle_mesh.hpp @@ -63,9 +63,16 @@ public: btVector3 getInterpolatedNormal(unsigned int index, const btVector3 &position) const; // ------------------------------------------------------------------------ + /** In case of physical objects of shape 'exact', the physical body is + * created outside of the mesh. Since raycasts need the body's world + * transform, the body can be set using this function. This will also + * cause the body not to be freed (since it will be freed as part of + * the physical object). */ void setBody(btRigidBody *body) { assert(!m_body); + // Mark that the body should not be deleted when this object is + // deleted, since the body is managed elsewhere. m_free_body = false; m_body = body; } diff --git a/src/tracks/terrain_info.cpp b/src/tracks/terrain_info.cpp index 9ff5109c5..2347cad37 100644 --- a/src/tracks/terrain_info.cpp +++ b/src/tracks/terrain_info.cpp @@ -80,7 +80,10 @@ void TerrainInfo::update(const btTransform &trans, const Vec3 &offset) const TriangleMesh &tm = World::getWorld()->getTrack()->getTriangleMesh(); tm.castRay(from, to, &m_hit_point, &m_material, &m_normal, /*interpolate*/true); - // Now also raycast against all track objects (that are driveable). + + // Now also raycast against all track objects (that are driveable). If + // there should be a closer result (than the one against the main track + // mesh), its data will be returned. World::getWorld()->getTrack()->getTrackObjectManager() ->castRay(from, to, &m_hit_point, &m_material, &m_normal, /*interpolate*/true); diff --git a/src/tracks/track_object_manager.cpp b/src/tracks/track_object_manager.cpp index 15bc8c255..d38076d04 100644 --- a/src/tracks/track_object_manager.cpp +++ b/src/tracks/track_object_manager.cpp @@ -125,6 +125,25 @@ void TrackObjectManager::update(float dt) } // update // ---------------------------------------------------------------------------- +/** Does a raycast against all driveable objects. This way part of the track + * can be a physical object, and can e.g. be animated. A separate list of all + * driveable objects is maintained (in one case there were over 2000 bodies, + * but only one is driveable). The result of the raycast against the track + * mesh are the input parameter. It is then tested if the raycast against + * a track object gives a 'closer' result. If so, the parameters hit_point, + * normal, and material will be updated. + * \param from/to The from and to position for the raycast. + * \param xyz The position in world where the ray hit. + * \param material The material of the mesh that was hit. + * \param normal The intrapolated normal at that position. + * \param interpolate_normal If true, the returned normal is the interpolated + * based on the three normals of the triangle and the location of the + * hit point (which is more compute intensive, but results in much + * smoother results). + * \return True if a triangle was hit, false otherwise (and no output + * variable will be set. + + */ void TrackObjectManager::castRay(const btVector3 &from, const btVector3 &to, btVector3 *hit_point, const Material **material, @@ -147,6 +166,8 @@ void TrackObjectManager::castRay(const btVector3 &from, interpolate_normal)) { float new_distance = new_hit_point.distance(from); + // If the new hit is closer than the current hit, save + // the data. if (new_distance < distance) { *material = new_material;