mirror of
https://git.coom.tech/drummyfish/raycastlib.git
synced 2024-11-23 20:49:57 +01:00
Add angleToDirection
This commit is contained in:
parent
b973df588b
commit
d37870efb1
2 changed files with 23 additions and 8 deletions
23
raycastlib.h
23
raycastlib.h
|
@ -11,6 +11,8 @@
|
|||
- X axis goes right.
|
||||
- Y axis goes up.
|
||||
- Each game square is UNITS_PER_SQUARE * UNITS_PER_SQUARE.
|
||||
- Angles are in Units, 0 means pointing right (x+) and positively rotates
|
||||
clockwise, a full angle has UNITS_PER_SQUARE Units.
|
||||
*/
|
||||
|
||||
#define UNITS_PER_SQUARE 1024
|
||||
|
@ -21,21 +23,21 @@ typedef int32_t Unit; /**< Smallest spatial unit, there is UNITS_PER_SQUARE
|
|||
#define logVector2D(v)\
|
||||
printf("[%d,%d]\n",v.x,v.y);
|
||||
|
||||
#define logRay(r)\
|
||||
#define logRay(r){\
|
||||
printf("ray:\n");\
|
||||
printf(" start: ");\
|
||||
logVector2D(r.start);\
|
||||
printf(" dir: ");\
|
||||
logVector2D(r.direction);\
|
||||
logVector2D(r.direction);}\
|
||||
|
||||
#define logHitResult(h)\
|
||||
#define logHitResult(h){\
|
||||
printf("hit:\n");\
|
||||
printf(" sqaure: ");\
|
||||
logVector2D(h.square);\
|
||||
printf(" pos: ");\
|
||||
logVector2D(h.position);\
|
||||
printf(" dist: %d", h.distance);\
|
||||
printf(" texcoord: %d", h.textureCoord);\
|
||||
printf(" texcoord: %d", h.textureCoord);}\
|
||||
|
||||
/// Position in 2D space.
|
||||
typedef struct
|
||||
|
@ -83,6 +85,7 @@ void castRayMultiHit(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t),
|
|||
uint16_t maxSteps, HitResult *hitResults, uint16_t *hitResultsLen,
|
||||
uint16_t maxHits);
|
||||
|
||||
Vector2D angleToDirection(Unit angle);
|
||||
Unit cosInt(Unit input);
|
||||
Unit sinInt(Unit input);
|
||||
uint16_t sqrtInt(uint32_t value);
|
||||
|
@ -128,7 +131,17 @@ Unit cosInt(Unit input)
|
|||
|
||||
Unit sinInt(Unit input)
|
||||
{
|
||||
return cosInt(input - UNITS_PER_SQUARE / 2);
|
||||
return cosInt(input - UNITS_PER_SQUARE / 4);
|
||||
}
|
||||
|
||||
Vector2D angleToDirection(Unit angle)
|
||||
{
|
||||
Vector2D result;
|
||||
|
||||
result.x = cosInt(angle);
|
||||
result.y = -1 * sinInt(angle);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint16_t sqrtInt(uint32_t value)
|
||||
|
|
8
test.c
8
test.c
|
@ -80,10 +80,12 @@ int main()
|
|||
{
|
||||
Unit v = sinInt(i);
|
||||
|
||||
for (int j = 0; j < (v + UNITS_PER_SQUARE) / 64; ++j)
|
||||
printf(".");
|
||||
logVector2D(angleToDirection(i));
|
||||
|
||||
printf("\n");
|
||||
//for (int j = 0; j < (v + UNITS_PER_SQUARE) / 64; ++j)
|
||||
// printf(".");
|
||||
|
||||
//printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue