Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/ScaleHelpers.h
9913 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Physics/PhysicsSettings.h>
8
9
JPH_NAMESPACE_BEGIN
10
11
/// Helper functions to get properties of a scaling vector
12
namespace ScaleHelpers
13
{
14
/// Minimum valid scale value. This is used to prevent division by zero when scaling a shape with a zero scale.
15
static constexpr float cMinScale = 1.0e-6f;
16
17
/// The tolerance used to check if components of the scale vector are the same
18
static constexpr float cScaleToleranceSq = 1.0e-8f;
19
20
/// Test if a scale is identity
21
inline bool IsNotScaled(Vec3Arg inScale) { return inScale.IsClose(Vec3::sOne(), cScaleToleranceSq); }
22
23
/// Test if a scale is uniform
24
inline bool IsUniformScale(Vec3Arg inScale) { return inScale.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>().IsClose(inScale, cScaleToleranceSq); }
25
26
/// Test if a scale is uniform in XZ
27
inline bool IsUniformScaleXZ(Vec3Arg inScale) { return inScale.Swizzle<SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_X>().IsClose(inScale, ScaleHelpers::cScaleToleranceSq); }
28
29
/// Scale the convex radius of an object
30
inline float ScaleConvexRadius(float inConvexRadius, Vec3Arg inScale) { return min(inConvexRadius * inScale.Abs().ReduceMin(), cDefaultConvexRadius); }
31
32
/// Test if a scale flips an object inside out (which requires flipping all normals and polygon windings)
33
inline bool IsInsideOut(Vec3Arg inScale) { return (CountBits(Vec3::sLess(inScale, Vec3::sZero()).GetTrues() & 0x7) & 1) != 0; }
34
35
/// Test if any of the components of the scale have a value below cMinScale
36
inline bool IsZeroScale(Vec3Arg inScale) { return Vec3::sLess(inScale.Abs(), Vec3::sReplicate(cMinScale)).TestAnyXYZTrue(); }
37
38
/// Ensure that the scale for each component is at least cMinScale
39
inline Vec3 MakeNonZeroScale(Vec3Arg inScale) { return inScale.GetSign() * Vec3::sMax(inScale.Abs(), Vec3::sReplicate(cMinScale)); }
40
41
/// Get the average scale if inScale, used to make the scale uniform when a shape doesn't support non-uniform scale
42
inline Vec3 MakeUniformScale(Vec3Arg inScale) { return Vec3::sReplicate((inScale.GetX() + inScale.GetY() + inScale.GetZ()) / 3.0f); }
43
44
/// Average the scale in XZ, used to make the scale uniform when a shape doesn't support non-uniform scale in the XZ plane
45
inline Vec3 MakeUniformScaleXZ(Vec3Arg inScale) { return 0.5f * (inScale + inScale.Swizzle<SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_X>()); }
46
47
/// Checks in scale can be rotated to child shape
48
/// @param inRotation Rotation of child shape
49
/// @param inScale Scale in local space of parent shape
50
/// @return True if the scale is valid (no shearing introduced)
51
inline bool CanScaleBeRotated(QuatArg inRotation, Vec3Arg inScale)
52
{
53
// inScale is a scale in local space of the shape, so the transform for the shape (ignoring translation) is: T = Mat44::sScale(inScale) * mRotation.
54
// when we pass the scale to the child it needs to be local to the child, so we want T = mRotation * Mat44::sScale(ChildScale).
55
// Solving for ChildScale: ChildScale = mRotation^-1 * Mat44::sScale(inScale) * mRotation = mRotation^T * Mat44::sScale(inScale) * mRotation
56
// If any of the off diagonal elements are non-zero, it means the scale / rotation is not compatible.
57
Mat44 r = Mat44::sRotation(inRotation);
58
Mat44 child_scale = r.Multiply3x3LeftTransposed(r.PostScaled(inScale));
59
60
// Get the columns, but zero the diagonal
61
Vec4 zero = Vec4::sZero();
62
Vec4 c0 = Vec4::sSelect(child_scale.GetColumn4(0), zero, UVec4(0xffffffff, 0, 0, 0)).Abs();
63
Vec4 c1 = Vec4::sSelect(child_scale.GetColumn4(1), zero, UVec4(0, 0xffffffff, 0, 0)).Abs();
64
Vec4 c2 = Vec4::sSelect(child_scale.GetColumn4(2), zero, UVec4(0, 0, 0xffffffff, 0)).Abs();
65
66
// Check if all elements are less than epsilon
67
Vec4 epsilon = Vec4::sReplicate(1.0e-6f);
68
return UVec4::sAnd(UVec4::sAnd(Vec4::sLess(c0, epsilon), Vec4::sLess(c1, epsilon)), Vec4::sLess(c2, epsilon)).TestAllTrue();
69
}
70
71
/// Adjust scale for rotated child shape
72
/// @param inRotation Rotation of child shape
73
/// @param inScale Scale in local space of parent shape
74
/// @return Rotated scale
75
inline Vec3 RotateScale(QuatArg inRotation, Vec3Arg inScale)
76
{
77
// Get the diagonal of mRotation^T * Mat44::sScale(inScale) * mRotation (see comment at CanScaleBeRotated)
78
Mat44 r = Mat44::sRotation(inRotation);
79
return r.Multiply3x3LeftTransposed(r.PostScaled(inScale)).GetDiagonal3();
80
}
81
}
82
83
JPH_NAMESPACE_END
84
85