2020-09-13 17:27:05 +00:00
|
|
|
/*
|
|
|
|
** binaryangle.h
|
|
|
|
**
|
|
|
|
** type safe representations of high precision angle and horizon values.
|
|
|
|
** Angle uses natural 32 bit overflow to clamp to one rotation.
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2020 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2020-07-22 19:16:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <math.h>
|
2021-10-30 08:35:00 +00:00
|
|
|
#include "basics.h"
|
2020-07-22 19:16:29 +00:00
|
|
|
#include "m_fixed.h"
|
|
|
|
#include "xs_Float.h" // needed for reliably overflowing float->int conversions.
|
2020-10-10 13:10:53 +00:00
|
|
|
#include "serializer.h"
|
2020-11-22 20:54:39 +00:00
|
|
|
#include "math/cmath.h"
|
2020-07-22 19:16:29 +00:00
|
|
|
|
2020-10-10 13:10:53 +00:00
|
|
|
class FSerializer;
|
|
|
|
|
2020-10-07 12:13:21 +00:00
|
|
|
enum
|
|
|
|
{
|
2020-11-22 10:45:16 +00:00
|
|
|
BAMBITS = 21,
|
|
|
|
BAMUNIT = 1 << BAMBITS,
|
2021-10-30 06:21:27 +00:00
|
|
|
SINTABLEBITS = 30,
|
|
|
|
SINTABLEUNIT = 1 << SINTABLEBITS,
|
|
|
|
BUILDSINBITS = 14,
|
|
|
|
BUILDSINSHIFT = SINTABLEBITS - BUILDSINBITS,
|
2020-10-07 12:13:21 +00:00
|
|
|
};
|
2020-07-23 14:45:58 +00:00
|
|
|
|
2020-11-22 03:40:32 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Constants used for Build sine/cosine functions.
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
constexpr double BAngRadian = pi::pi() * (1. / 1024.);
|
2021-03-31 23:42:22 +00:00
|
|
|
constexpr double BAngToDegree = 360. / 2048.;
|
2020-11-22 03:40:32 +00:00
|
|
|
|
2021-10-30 06:21:27 +00:00
|
|
|
extern int sintable[2048];
|
2020-11-22 03:40:32 +00:00
|
|
|
|
2021-10-30 03:50:24 +00:00
|
|
|
inline constexpr double sinscale(const int shift)
|
|
|
|
{
|
2021-10-30 06:21:27 +00:00
|
|
|
return shift >= -BUILDSINBITS ? uint64_t(1) << (BUILDSINBITS + shift) : 1. / (uint64_t(1) << abs(BUILDSINBITS + shift));
|
2021-10-30 03:50:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 03:40:32 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Build sine inline functions.
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2021-10-30 06:21:27 +00:00
|
|
|
inline int bsin(const int ang, int shift = 0)
|
2020-11-22 03:40:32 +00:00
|
|
|
{
|
2021-10-30 06:21:27 +00:00
|
|
|
return (shift -= BUILDSINSHIFT) < 0 ? sintable[ang & 2047] >> abs(shift) : sintable[ang & 2047] << shift;
|
2020-11-22 03:40:32 +00:00
|
|
|
}
|
2021-04-30 14:21:37 +00:00
|
|
|
inline double bsinf(const double ang, const int shift = 0)
|
2020-11-22 03:40:32 +00:00
|
|
|
{
|
2021-10-30 03:50:24 +00:00
|
|
|
return g_sin(ang * BAngRadian) * sinscale(shift);
|
2020-11-22 03:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Build cosine inline functions.
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2021-10-30 06:21:27 +00:00
|
|
|
inline int bcos(const int ang, int shift = 0)
|
2020-11-22 03:40:32 +00:00
|
|
|
{
|
2021-10-30 06:21:27 +00:00
|
|
|
return (shift -= BUILDSINSHIFT) < 0 ? sintable[(ang + 512) & 2047] >> abs(shift) : sintable[(ang + 512) & 2047] << shift;
|
2020-11-22 03:40:32 +00:00
|
|
|
}
|
2021-04-30 14:21:37 +00:00
|
|
|
inline double bcosf(const double ang, const int shift = 0)
|
2020-11-22 03:40:32 +00:00
|
|
|
{
|
2021-10-30 03:50:24 +00:00
|
|
|
return g_cos(ang * BAngRadian) * sinscale(shift);
|
2020-11-22 03:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
class binangle
|
2020-10-07 12:13:21 +00:00
|
|
|
{
|
2020-11-22 12:56:54 +00:00
|
|
|
uint32_t value;
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
constexpr binangle(uint32_t v) : value(v) {}
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
friend constexpr binangle bamang(uint32_t v);
|
|
|
|
friend constexpr binangle q16ang(uint32_t v);
|
|
|
|
friend constexpr binangle buildang(uint32_t v);
|
2021-11-01 19:25:38 +00:00
|
|
|
friend binangle buildfang(double v);
|
|
|
|
friend binangle radang(double v);
|
|
|
|
friend binangle degang(double v);
|
2020-10-10 13:10:53 +00:00
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
friend FSerializer &Serialize(FSerializer &arc, const char *key, binangle &obj, binangle *defval);
|
2021-10-30 03:49:53 +00:00
|
|
|
|
|
|
|
constexpr int32_t tosigned() const { return value > INT32_MAX ? int64_t(value) - UINT32_MAX : value; }
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-10-07 12:13:21 +00:00
|
|
|
public:
|
2020-11-22 12:56:54 +00:00
|
|
|
binangle() = default;
|
|
|
|
binangle(const binangle &other) = default;
|
2021-11-14 11:30:18 +00:00
|
|
|
binangle& operator=(const binangle& other) = default;
|
2020-10-07 12:13:21 +00:00
|
|
|
// This class intentionally makes no allowances for implicit type conversions because those would render it ineffective.
|
2021-04-14 22:25:08 +00:00
|
|
|
constexpr short asbuild() const { return value >> BAMBITS; }
|
2021-11-02 22:31:09 +00:00
|
|
|
constexpr double asbuildf() const { return value * (1. / +BAMUNIT); }
|
2020-10-07 12:13:21 +00:00
|
|
|
constexpr fixed_t asq16() const { return value >> 5; }
|
2021-04-11 05:43:11 +00:00
|
|
|
constexpr uint32_t asbam() const { return value; }
|
2020-10-07 12:13:21 +00:00
|
|
|
constexpr double asrad() const { return value * (pi::pi() / 0x80000000u); }
|
|
|
|
constexpr double asdeg() const { return AngleToFloat(value); }
|
2021-04-14 22:25:08 +00:00
|
|
|
constexpr short signedbuild() const { return tosigned() >> BAMBITS; }
|
2021-11-02 22:31:09 +00:00
|
|
|
constexpr double signedbuildf() const { return tosigned() * (1. / +BAMUNIT); }
|
2021-04-11 05:43:11 +00:00
|
|
|
constexpr fixed_t signedq16() const { return tosigned() >> 5; }
|
|
|
|
constexpr int32_t signedbam() const { return tosigned(); }
|
2021-04-11 07:02:44 +00:00
|
|
|
constexpr double signedrad() const { return tosigned() * (pi::pi() / 0x80000000u); }
|
|
|
|
constexpr double signeddeg() const { return AngleToFloat(tosigned()); }
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-11-22 21:40:18 +00:00
|
|
|
double fsin() const { return g_sin(asrad()); }
|
|
|
|
double fcos() const { return g_cos(asrad()); }
|
|
|
|
double ftan() const { return g_tan(asrad()); }
|
2020-11-22 03:40:32 +00:00
|
|
|
int bsin(const int8_t& shift = 0) const { return ::bsin(asbuild(), shift); }
|
|
|
|
int bcos(const int8_t& shift = 0) const { return ::bcos(asbuild(), shift); }
|
2020-10-07 12:13:21 +00:00
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
constexpr bool operator== (binangle other) const
|
2020-07-23 15:01:37 +00:00
|
|
|
{
|
2020-10-07 12:13:21 +00:00
|
|
|
return value == other.value;
|
2020-07-23 15:01:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
constexpr bool operator!= (binangle other) const
|
2020-10-07 12:13:21 +00:00
|
|
|
{
|
|
|
|
return value != other.value;
|
|
|
|
}
|
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
constexpr binangle &operator+= (binangle other)
|
2020-10-07 12:13:21 +00:00
|
|
|
{
|
|
|
|
value += other.value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
constexpr binangle &operator-= (binangle other)
|
2020-10-07 12:13:21 +00:00
|
|
|
{
|
|
|
|
value -= other.value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
constexpr binangle operator+ (binangle other) const
|
2020-10-07 12:13:21 +00:00
|
|
|
{
|
2020-11-22 12:56:54 +00:00
|
|
|
return binangle(value + other.value);
|
2020-10-07 12:13:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
constexpr binangle operator- (binangle other) const
|
2020-10-07 12:13:21 +00:00
|
|
|
{
|
2020-11-22 12:56:54 +00:00
|
|
|
return binangle(value - other.value);
|
2020-10-07 12:13:21 +00:00
|
|
|
}
|
2020-07-22 19:16:29 +00:00
|
|
|
|
2022-01-27 19:19:53 +00:00
|
|
|
constexpr binangle operator- () const
|
|
|
|
{
|
|
|
|
return binangle(0 - value);
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr binangle &operator<<= (const uint8_t shift)
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
2021-11-01 11:13:39 +00:00
|
|
|
value = tosigned() << shift;
|
2020-11-22 10:46:17 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr binangle &operator>>= (const uint8_t shift)
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
2021-11-01 11:13:39 +00:00
|
|
|
value = tosigned() >> shift;
|
2020-11-22 10:46:17 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr binangle operator<< (const uint8_t shift) const
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
2021-11-01 11:13:39 +00:00
|
|
|
return binangle(tosigned() << shift);
|
2020-11-22 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr binangle operator>> (const uint8_t shift) const
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
2021-11-01 11:13:39 +00:00
|
|
|
return binangle(tosigned() >> shift);
|
2020-11-22 10:46:17 +00:00
|
|
|
}
|
2020-07-23 14:45:58 +00:00
|
|
|
};
|
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
inline constexpr binangle bamang(uint32_t v) { return binangle(v); }
|
|
|
|
inline constexpr binangle q16ang(uint32_t v) { return binangle(v << 5); }
|
|
|
|
inline constexpr binangle buildang(uint32_t v) { return binangle(v << BAMBITS); }
|
2021-11-05 22:38:51 +00:00
|
|
|
inline binangle buildfang(double v) { return binangle(xs_ToFixed(BAMBITS, v)); }
|
2021-11-01 19:25:38 +00:00
|
|
|
inline binangle radang(double v) { return binangle(xs_CRoundToUInt(v * (0x80000000u / pi::pi()))); }
|
|
|
|
inline binangle degang(double v) { return binangle(FloatToAngle(v)); }
|
2020-11-22 03:40:32 +00:00
|
|
|
|
2020-11-22 12:56:54 +00:00
|
|
|
inline FSerializer &Serialize(FSerializer &arc, const char *key, binangle &obj, binangle *defval)
|
2020-11-22 03:40:32 +00:00
|
|
|
{
|
|
|
|
return Serialize(arc, key, obj.value, defval ? &defval->value : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-06 20:34:20 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2021-04-10 00:06:30 +00:00
|
|
|
// Functions for use with fixedhoriz and friendly functions.
|
2020-10-06 20:34:20 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2021-04-10 00:06:30 +00:00
|
|
|
inline double HorizToPitch(double horiz) { return atan2(horiz, 128) * (180. / pi::pi()); }
|
|
|
|
inline double HorizToPitch(fixed_t q16horiz) { return atan2(q16horiz, IntToFixed(128)) * (180. / pi::pi()); }
|
2021-12-06 06:31:00 +00:00
|
|
|
inline fixed_t PitchToHoriz(double pitch) { return xs_CRoundToInt(clamp<double>(IntToFixed(128) * tan(pitch * (pi::pi() / 180.)), INT32_MIN, INT32_MAX)); }
|
|
|
|
inline int32_t PitchToBAM(double pitch) { return xs_CRoundToInt(clamp<double>(pitch * (0x80000000u / 90.), INT32_MIN, INT32_MAX)); }
|
|
|
|
inline constexpr double BAMToPitch(int32_t bam) { return bam * (90. / 0x80000000u); }
|
2020-10-06 20:34:20 +00:00
|
|
|
|
|
|
|
|
2020-11-22 03:40:32 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2020-07-23 14:45:58 +00:00
|
|
|
class fixedhoriz
|
|
|
|
{
|
2020-10-06 20:34:20 +00:00
|
|
|
fixed_t value;
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-10-06 20:34:20 +00:00
|
|
|
constexpr fixedhoriz(fixed_t v) : value(v) {}
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-10-06 20:34:20 +00:00
|
|
|
friend constexpr fixedhoriz q16horiz(fixed_t v);
|
2020-07-23 14:45:58 +00:00
|
|
|
friend constexpr fixedhoriz buildhoriz(int v);
|
2021-11-01 19:25:38 +00:00
|
|
|
friend fixedhoriz buildfhoriz(double v);
|
2020-10-06 20:34:20 +00:00
|
|
|
friend fixedhoriz pitchhoriz(double v);
|
|
|
|
friend fixedhoriz bamhoriz(int32_t v);
|
2020-10-10 13:10:53 +00:00
|
|
|
|
|
|
|
friend FSerializer &Serialize(FSerializer &arc, const char *key, fixedhoriz &obj, fixedhoriz *defval);
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-07-23 14:45:58 +00:00
|
|
|
public:
|
|
|
|
fixedhoriz() = default;
|
|
|
|
fixedhoriz(const fixedhoriz &other) = default;
|
|
|
|
|
|
|
|
// This class intentionally makes no allowances for implicit type conversions because those would render it ineffective.
|
2020-10-06 20:34:20 +00:00
|
|
|
constexpr short asbuild() const { return FixedToInt(value); }
|
2020-11-22 13:17:07 +00:00
|
|
|
constexpr double asbuildf() const { return FixedToFloat(value); }
|
2020-07-23 14:45:58 +00:00
|
|
|
constexpr fixed_t asq16() const { return value; }
|
2020-10-06 20:34:20 +00:00
|
|
|
double aspitch() const { return HorizToPitch(value); }
|
|
|
|
int32_t asbam() const { return PitchToBAM(aspitch()); }
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-07-23 14:45:58 +00:00
|
|
|
bool operator< (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value < other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator> (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value > other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<= (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value <= other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator>= (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value >= other.value;
|
|
|
|
}
|
|
|
|
constexpr bool operator== (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value == other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool operator!= (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value != other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr fixedhoriz &operator+= (fixedhoriz other)
|
|
|
|
{
|
|
|
|
value += other.value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr fixedhoriz &operator-= (fixedhoriz other)
|
|
|
|
{
|
|
|
|
value -= other.value;
|
|
|
|
return *this;
|
|
|
|
}
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-07-23 14:45:58 +00:00
|
|
|
constexpr fixedhoriz operator- () const
|
|
|
|
{
|
|
|
|
return fixedhoriz(-value);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr fixedhoriz operator+ (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return fixedhoriz(value + other.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr fixedhoriz operator- (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return fixedhoriz(value - other.value);
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr fixedhoriz &operator<<= (const uint8_t shift)
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
|
|
|
value <<= shift;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr fixedhoriz &operator>>= (const uint8_t shift)
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
|
|
|
value >>= shift;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr fixedhoriz operator<< (const uint8_t shift) const
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
|
|
|
return fixedhoriz(value << shift);
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr fixedhoriz operator>> (const uint8_t shift) const
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
|
|
|
return fixedhoriz(value >> shift);
|
|
|
|
}
|
|
|
|
|
2020-07-22 19:16:29 +00:00
|
|
|
};
|
|
|
|
|
2020-10-06 20:34:20 +00:00
|
|
|
inline constexpr fixedhoriz q16horiz(fixed_t v) { return fixedhoriz(v); }
|
2020-09-01 13:00:35 +00:00
|
|
|
inline constexpr fixedhoriz buildhoriz(int v) { return fixedhoriz(IntToFixed(v)); }
|
2021-11-01 19:25:38 +00:00
|
|
|
inline fixedhoriz buildfhoriz(double v) { return fixedhoriz(FloatToFixed(v)); }
|
2020-10-06 20:34:20 +00:00
|
|
|
inline fixedhoriz pitchhoriz(double v) { return fixedhoriz(PitchToHoriz(v)); }
|
|
|
|
inline fixedhoriz bamhoriz(int32_t v) { return pitchhoriz(BAMToPitch(v)); }
|
2020-07-23 14:45:58 +00:00
|
|
|
|
2020-11-22 03:40:32 +00:00
|
|
|
inline FSerializer &Serialize(FSerializer &arc, const char *key, fixedhoriz &obj, fixedhoriz *defval)
|
2020-10-11 04:34:18 +00:00
|
|
|
{
|
|
|
|
return Serialize(arc, key, obj.value, defval ? &defval->value : nullptr);
|
|
|
|
}
|
|
|
|
|
2020-11-22 03:40:32 +00:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2021-04-10 10:54:29 +00:00
|
|
|
// High precision vector angle function, mainly for the renderer.
|
2020-11-22 03:40:32 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-01-30 12:33:47 +00:00
|
|
|
inline binangle bvectangbam(double x, double y)
|
2020-11-22 03:40:32 +00:00
|
|
|
{
|
2021-04-10 10:54:29 +00:00
|
|
|
return radang(atan2(y, x));
|
2020-10-11 04:34:18 +00:00
|
|
|
}
|
2021-04-11 06:45:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Interpolation functions for use throughout games.
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2021-05-11 23:00:12 +00:00
|
|
|
inline constexpr int32_t interpolatedvalue(int32_t oval, int32_t val, double const smoothratio, int const scale = 16)
|
|
|
|
{
|
|
|
|
return oval + MulScale(val - oval, int(smoothratio), scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline constexpr int32_t interpolatedvalue(int32_t oval, int32_t val, int const smoothratio, int const scale = 16)
|
2021-04-11 06:45:19 +00:00
|
|
|
{
|
|
|
|
return oval + MulScale(val - oval, smoothratio, scale);
|
|
|
|
}
|
|
|
|
|
2021-05-11 23:00:12 +00:00
|
|
|
inline constexpr double interpolatedvaluef(double oval, double val, double const smoothratio, int const scale = 16)
|
2021-04-11 06:45:19 +00:00
|
|
|
{
|
|
|
|
return oval + MulScaleF(val - oval, smoothratio, scale);
|
|
|
|
}
|
|
|
|
|
2021-05-11 23:00:12 +00:00
|
|
|
inline constexpr int32_t interpolatedangle(int32_t oang, int32_t ang, double const smoothratio, int const scale = 16)
|
|
|
|
{
|
|
|
|
return oang + MulScale(((ang + 1024 - oang) & 2047) - 1024, int(smoothratio), scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline constexpr int32_t interpolatedangle(int32_t oang, int32_t ang, int const smoothratio, int const scale = 16)
|
2021-04-11 06:45:19 +00:00
|
|
|
{
|
|
|
|
return oang + MulScale(((ang + 1024 - oang) & 2047) - 1024, smoothratio, scale);
|
|
|
|
}
|
|
|
|
|
2021-05-11 23:00:12 +00:00
|
|
|
inline constexpr binangle interpolatedangle(binangle oang, binangle ang, double const smoothratio, int const scale = 16)
|
|
|
|
{
|
|
|
|
return bamang(oang.asbam() + MulScale(((ang.asbam() + 0x80000000 - oang.asbam()) & 0xFFFFFFFF) - 0x80000000, int(smoothratio), scale));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline constexpr binangle interpolatedangle(binangle oang, binangle ang, int const smoothratio, int const scale = 16)
|
2021-04-11 06:45:19 +00:00
|
|
|
{
|
|
|
|
return bamang(oang.asbam() + MulScale(((ang.asbam() + 0x80000000 - oang.asbam()) & 0xFFFFFFFF) - 0x80000000, smoothratio, scale));
|
|
|
|
}
|
2021-11-06 03:00:41 +00:00
|
|
|
|
|
|
|
inline constexpr fixedhoriz interpolatedhorizon(fixedhoriz oval, fixedhoriz val, double const smoothratio, int const scale = 16)
|
|
|
|
{
|
|
|
|
return q16horiz(oval.asq16() + MulScale((val - oval).asq16(), int(smoothratio), scale));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline constexpr fixedhoriz interpolatedhorizon(fixedhoriz oval, fixedhoriz val, int const smoothratio, int const scale = 16)
|
|
|
|
{
|
|
|
|
return q16horiz(oval.asq16() + MulScale((val - oval).asq16(), smoothratio, scale));
|
|
|
|
}
|