Skip to content
0.2.0-dev Download ZIP
85 lines ·2.8 KB Raw
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#include <Jolt/Jolt.h>
6
7#include <Jolt/Physics/Constraints/PathConstraintPath.h>
8#include <Jolt/Core/StreamUtils.h>
9#ifdef JPH_DEBUG_RENDERER
10 #include <Jolt/Renderer/DebugRenderer.h>
11#endif // JPH_DEBUG_RENDERER
12
13JPH_NAMESPACE_BEGIN
14
15JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT(PathConstraintPath)
16{
17 JPH_ADD_BASE_CLASS(PathConstraintPath, SerializableObject)
18}
19
20#ifdef JPH_DEBUG_RENDERER
21// Helper function to transform the results of GetPointOnPath to world space
22static inline void sTransformPathPoint(RMat44Arg inTransform, Vec3Arg inPosition, RVec3 &outPosition, Vec3 &ioNormal, Vec3 &ioBinormal)
23{
24 outPosition = inTransform * inPosition;
25 ioNormal = inTransform.Multiply3x3(ioNormal);
26 ioBinormal = inTransform.Multiply3x3(ioBinormal);
27}
28
29// Helper function to draw a path segment
30static inline void sDrawPathSegment(DebugRenderer *inRenderer, RVec3Arg inPrevPosition, RVec3Arg inPosition, Vec3Arg inNormal, Vec3Arg inBinormal)
31{
32 inRenderer->DrawLine(inPrevPosition, inPosition, Color::sWhite);
33 inRenderer->DrawArrow(inPosition, inPosition + 0.1f * inNormal, Color::sRed, 0.02f);
34 inRenderer->DrawArrow(inPosition, inPosition + 0.1f * inBinormal, Color::sGreen, 0.02f);
35}
36
37void PathConstraintPath::DrawPath(DebugRenderer *inRenderer, RMat44Arg inBaseTransform) const
38{
39 // Calculate first point
40 Vec3 lfirst_pos, first_tangent, first_normal, first_binormal;
41 GetPointOnPath(0.0f, lfirst_pos, first_tangent, first_normal, first_binormal);
42 RVec3 first_pos;
43 sTransformPathPoint(inBaseTransform, lfirst_pos, first_pos, first_normal, first_binormal);
44
45 float t_max = GetPathMaxFraction();
46
47 // Draw the segments
48 RVec3 prev_pos = first_pos;
49 for (float t = 0.1f; t < t_max; t += 0.1f)
50 {
51 Vec3 lpos, tangent, normal, binormal;
52 GetPointOnPath(t, lpos, tangent, normal, binormal);
53 RVec3 pos;
54 sTransformPathPoint(inBaseTransform, lpos, pos, normal, binormal);
55 sDrawPathSegment(inRenderer, prev_pos, pos, normal, binormal);
56 prev_pos = pos;
57 }
58
59 // Draw last point
60 Vec3 lpos, tangent, normal, binormal;
61 GetPointOnPath(t_max, lpos, tangent, normal, binormal);
62 RVec3 pos;
63 sTransformPathPoint(inBaseTransform, lpos, pos, normal, binormal);
64 sDrawPathSegment(inRenderer, prev_pos, pos, normal, binormal);
65}
66#endif // JPH_DEBUG_RENDERER
67
68void PathConstraintPath::SaveBinaryState(StreamOut &inStream) const
69{
70 inStream.Write(GetRTTI()->GetHash());
71 inStream.Write(mIsLooping);
72}
73
74void PathConstraintPath::RestoreBinaryState(StreamIn &inStream)
75{
76 // Type hash read by sRestoreFromBinaryState
77 inStream.Read(mIsLooping);
78}
79
80PathConstraintPath::PathResult PathConstraintPath::sRestoreFromBinaryState(StreamIn &inStream)
81{
82 return StreamUtils::RestoreObject<PathConstraintPath>(inStream, &PathConstraintPath::RestoreBinaryState);
83}
84
85JPH_NAMESPACE_END