Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/PathConstraintPath.cpp
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#include <Jolt/Jolt.h>56#include <Jolt/Physics/Constraints/PathConstraintPath.h>7#include <Jolt/Core/StreamUtils.h>8#ifdef JPH_DEBUG_RENDERER9#include <Jolt/Renderer/DebugRenderer.h>10#endif // JPH_DEBUG_RENDERER1112JPH_NAMESPACE_BEGIN1314JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT(PathConstraintPath)15{16JPH_ADD_BASE_CLASS(PathConstraintPath, SerializableObject)17}1819#ifdef JPH_DEBUG_RENDERER20// Helper function to transform the results of GetPointOnPath to world space21static inline void sTransformPathPoint(RMat44Arg inTransform, Vec3Arg inPosition, RVec3 &outPosition, Vec3 &ioNormal, Vec3 &ioBinormal)22{23outPosition = inTransform * inPosition;24ioNormal = inTransform.Multiply3x3(ioNormal);25ioBinormal = inTransform.Multiply3x3(ioBinormal);26}2728// Helper function to draw a path segment29static inline void sDrawPathSegment(DebugRenderer *inRenderer, RVec3Arg inPrevPosition, RVec3Arg inPosition, Vec3Arg inNormal, Vec3Arg inBinormal)30{31inRenderer->DrawLine(inPrevPosition, inPosition, Color::sWhite);32inRenderer->DrawArrow(inPosition, inPosition + 0.1f * inNormal, Color::sRed, 0.02f);33inRenderer->DrawArrow(inPosition, inPosition + 0.1f * inBinormal, Color::sGreen, 0.02f);34}3536void PathConstraintPath::DrawPath(DebugRenderer *inRenderer, RMat44Arg inBaseTransform) const37{38// Calculate first point39Vec3 lfirst_pos, first_tangent, first_normal, first_binormal;40GetPointOnPath(0.0f, lfirst_pos, first_tangent, first_normal, first_binormal);41RVec3 first_pos;42sTransformPathPoint(inBaseTransform, lfirst_pos, first_pos, first_normal, first_binormal);4344float t_max = GetPathMaxFraction();4546// Draw the segments47RVec3 prev_pos = first_pos;48for (float t = 0.1f; t < t_max; t += 0.1f)49{50Vec3 lpos, tangent, normal, binormal;51GetPointOnPath(t, lpos, tangent, normal, binormal);52RVec3 pos;53sTransformPathPoint(inBaseTransform, lpos, pos, normal, binormal);54sDrawPathSegment(inRenderer, prev_pos, pos, normal, binormal);55prev_pos = pos;56}5758// Draw last point59Vec3 lpos, tangent, normal, binormal;60GetPointOnPath(t_max, lpos, tangent, normal, binormal);61RVec3 pos;62sTransformPathPoint(inBaseTransform, lpos, pos, normal, binormal);63sDrawPathSegment(inRenderer, prev_pos, pos, normal, binormal);64}65#endif // JPH_DEBUG_RENDERER6667void PathConstraintPath::SaveBinaryState(StreamOut &inStream) const68{69inStream.Write(GetRTTI()->GetHash());70inStream.Write(mIsLooping);71}7273void PathConstraintPath::RestoreBinaryState(StreamIn &inStream)74{75// Type hash read by sRestoreFromBinaryState76inStream.Read(mIsLooping);77}7879PathConstraintPath::PathResult PathConstraintPath::sRestoreFromBinaryState(StreamIn &inStream)80{81return StreamUtils::RestoreObject<PathConstraintPath>(inStream, &PathConstraintPath::RestoreBinaryState);82}8384JPH_NAMESPACE_END858687