Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Trigonometry.h
Go to the documentation of this file.
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
8
9// Note that this file exists because std::sin etc. are not platform independent and will lead to non-deterministic simulation
10
12JPH_INLINE float Sin(float inX)
13{
14 Vec4 s, c;
15 Vec4::sReplicate(inX).SinCos(s, c);
16 return s.GetX();
17}
18
20JPH_INLINE float Cos(float inX)
21{
22 Vec4 s, c;
23 Vec4::sReplicate(inX).SinCos(s, c);
24 return c.GetX();
25}
26
28JPH_INLINE float Tan(float inX)
29{
30 return Vec4::sReplicate(inX).Tan().GetX();
31}
32
35JPH_INLINE float ASin(float inX)
36{
37 return Vec4::sReplicate(inX).ASin().GetX();
38}
39
42JPH_INLINE float ACos(float inX)
43{
44 return Vec4::sReplicate(inX).ACos().GetX();
45}
46
48JPH_INLINE float ACosApproximate(float inX)
49{
50 // See: https://www.johndcook.com/blog/2022/09/06/inverse-cosine-near-1/
51 // See also: https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/
52 // Taylor of cos(x) = 1 - x^2 / 2 + ...
53 // Substitute x = sqrt(2 y) we get: cos(sqrt(2 y)) = 1 - y
54 // Substitute z = 1 - y we get: cos(sqrt(2 (1 - z))) = z <=> acos(z) = sqrt(2 (1 - z))
55 // To avoid the discontinuity at 1, instead of using the Taylor expansion of acos(x) we use acos(x) / sqrt(2 (1 - x)) = 1 + (1 - x) / 12 + ...
56 // Since the approximation was made at 1, it has quite a large error at 0 meaning that if we want to extend to the
57 // range [-1, 1] by mirroring the range [0, 1], the value at 0+ is not the same as 0-.
58 // So we observe that the form of the Taylor expansion is f(x) = sqrt(1 - x) * (a + b x) and we fit the function so that f(0) = pi / 2
59 // this gives us a = pi / 2. f(1) = 0 regardless of b. We search for a constant b that minimizes the error in the range [0, 1].
60 float abs_x = min(abs(inX), 1.0f); // Ensure that we don't get a value larger than 1
61 float val = sqrt(1.0f - abs_x) * (JPH_PI / 2 - 0.175394f * abs_x);
62
63 // Our approximation is valid in the range [0, 1], extend it to the range [-1, 1]
64 return inX < 0? JPH_PI - val : val;
65}
66
68JPH_INLINE float ATan(float inX)
69{
70 return Vec4::sReplicate(inX).ATan().GetX();
71}
72
74JPH_INLINE float ATan2(float inY, float inX)
75{
77}
78
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
JPH_INLINE float Tan(float inX)
Tangent of x (input in radians)
Definition: Trigonometry.h:28
JPH_INLINE float ACosApproximate(float inX)
An approximation of ACos, max error is 4.2e-3 over the entire range [-1, 1], is approximately 2....
Definition: Trigonometry.h:48
JPH_INLINE float Cos(float inX)
Cosine of x (input in radians)
Definition: Trigonometry.h:20
JPH_INLINE float ACos(float inX)
Definition: Trigonometry.h:42
JPH_INLINE float ATan2(float inY, float inX)
Arc tangent of y / x using the signs of the arguments to determine the correct quadrant (returns valu...
Definition: Trigonometry.h:74
JPH_NAMESPACE_BEGIN JPH_INLINE float Sin(float inX)
Sine of x (input in radians)
Definition: Trigonometry.h:12
JPH_INLINE float ASin(float inX)
Definition: Trigonometry.h:35
JPH_INLINE float ATan(float inX)
Arc tangent of x (returns value in the range [-PI / 2, PI / 2])
Definition: Trigonometry.h:68
Definition: Vec4.h:14
Vec4 ATan() const
Calculate the arc tangent for each element of this vector (returns value in the range [-PI / 2,...
Definition: Vec4.inl:911
static Vec4 sATan2(Vec4Arg inY, Vec4Arg inX)
Calculate the arc tangent of y / x using the signs of the arguments to determine the correct quadrant...
Definition: Vec4.inl:945
Vec4 ASin() const
Definition: Vec4.inl:870
Vec4 Tan() const
Calculate the tangent for each element of this vector (input in radians)
Definition: Vec4.inl:837
JPH_INLINE float GetX() const
Get individual components.
Definition: Vec4.h:113
Vec4 ACos() const
Definition: Vec4.inl:905
static JPH_INLINE Vec4 sReplicate(float inV)
Replicate inV across all components.
Definition: Vec4.inl:74
void SinCos(Vec4 &outSin, Vec4 &outCos) const
Calculate the sine and cosine for each element of this vector (input in radians)
Definition: Vec4.inl:775