mirror of
https://git.coom.tech/drummyfish/raycastlib.git
synced 2024-11-21 20:29:59 +01:00
Add sin and cos
This commit is contained in:
parent
126f38e5c0
commit
b973df588b
2 changed files with 50 additions and 1 deletions
35
raycastlib.h
35
raycastlib.h
|
@ -11,7 +11,6 @@
|
|||
- X axis goes right.
|
||||
- Y axis goes up.
|
||||
- Each game square is UNITS_PER_SQUARE * UNITS_PER_SQUARE.
|
||||
|
||||
*/
|
||||
|
||||
#define UNITS_PER_SQUARE 1024
|
||||
|
@ -84,6 +83,11 @@ void castRayMultiHit(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t),
|
|||
uint16_t maxSteps, HitResult *hitResults, uint16_t *hitResultsLen,
|
||||
uint16_t maxHits);
|
||||
|
||||
Unit cosInt(Unit input);
|
||||
Unit sinInt(Unit input);
|
||||
uint16_t sqrtInt(uint32_t value);
|
||||
Unit dist(Vector2D p1, Vector2D p2);
|
||||
|
||||
//=============================================================================
|
||||
// privates
|
||||
|
||||
|
@ -98,6 +102,35 @@ Unit clamp(Unit value, Unit valueMin, Unit valueMax)
|
|||
return value;
|
||||
}
|
||||
|
||||
// Bhaskara's cosine approximation formula
|
||||
#define trigHelper(x) (UNITS_PER_SQUARE *\
|
||||
(UNITS_PER_SQUARE / 2 * UNITS_PER_SQUARE / 2 - 4 * (x) * (x)) /\
|
||||
(UNITS_PER_SQUARE / 2 * UNITS_PER_SQUARE / 2 + (x) * (x)))
|
||||
|
||||
Unit cosInt(Unit input)
|
||||
{
|
||||
input = input % UNITS_PER_SQUARE;
|
||||
|
||||
if (input < 0)
|
||||
input = UNITS_PER_SQUARE + input;
|
||||
|
||||
if (input < UNITS_PER_SQUARE / 4)
|
||||
return trigHelper(input);
|
||||
else if (input < UNITS_PER_SQUARE / 2)
|
||||
return -1 * trigHelper(UNITS_PER_SQUARE / 2 - input);
|
||||
else if (input < 3 * UNITS_PER_SQUARE / 4)
|
||||
return -1 * trigHelper(input - UNITS_PER_SQUARE / 2);
|
||||
else
|
||||
return trigHelper(UNITS_PER_SQUARE - input);
|
||||
}
|
||||
|
||||
#undef trigHelper
|
||||
|
||||
Unit sinInt(Unit input)
|
||||
{
|
||||
return cosInt(input - UNITS_PER_SQUARE / 2);
|
||||
}
|
||||
|
||||
uint16_t sqrtInt(uint32_t value)
|
||||
{
|
||||
uint32_t result = 0;
|
||||
|
|
16
test.c
16
test.c
|
@ -1,3 +1,9 @@
|
|||
/**
|
||||
Tests for raycastlib.
|
||||
|
||||
license: CC0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "raycastlib.h"
|
||||
|
||||
|
@ -70,5 +76,15 @@ int main()
|
|||
16))
|
||||
return 1;
|
||||
|
||||
for (Unit i = -UNITS_PER_SQUARE; i <= UNITS_PER_SQUARE; i += 64)
|
||||
{
|
||||
Unit v = sinInt(i);
|
||||
|
||||
for (int j = 0; j < (v + UNITS_PER_SQUARE) / 64; ++j)
|
||||
printf(".");
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue