2018-08-21 13:49:45 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
author: Miloslav "drummyfish" Ciz
|
|
|
|
license: CC0
|
|
|
|
|
|
|
|
- Game field's bottom left corner is at [0,0].
|
|
|
|
- X axis goes right.
|
|
|
|
- Y axis goes up.
|
|
|
|
- Each game square is UNITS_PER_SQUARE * UNITS_PER_SQUARE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define UNITS_PER_SQUARE 1024
|
|
|
|
|
|
|
|
typedef int64_t Unit; /**< Smallest spatial unit, there is UNITS_PER_SQUARE
|
|
|
|
units in a square's length. */
|
|
|
|
|
|
|
|
/// Position in 2D space.
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
Unit y;
|
|
|
|
Unit x;
|
|
|
|
} Vector2D;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
Vector2D start;
|
|
|
|
Vector2D direction;
|
|
|
|
} Ray;
|
|
|
|
|
2018-08-22 08:21:38 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
Vector2D square; ///< Collided square coordinates.
|
|
|
|
Vector2D position; ///< Exact collision position in Units.
|
|
|
|
Unit distance; /**< Euclidean distance to the hit position, or -1 if
|
|
|
|
no collision happened. */
|
|
|
|
} HitResult;
|
|
|
|
|
|
|
|
HitResult castRay(Ray ray, int (*collisionFunc)(int, int),
|
|
|
|
unsigned int maxSteps);
|
2018-08-21 13:49:45 +02:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
// privates
|
|
|
|
|
|
|
|
#define logVector2D(v) printf("[%d,%d]\n",v.x,v.y)
|
|
|
|
#define logRay(r) printf("{\n");\
|
|
|
|
logVector2D(r.start);\
|
|
|
|
logVector2D(r.direction);\
|
|
|
|
printf("}\n");
|
|
|
|
|
|
|
|
int8_t pointIsLeftOfRay(Vector2D point, Ray ray)
|
|
|
|
{
|
|
|
|
int dX = point.x - ray.start.x;
|
|
|
|
int dY = point.y - ray.start.y;
|
|
|
|
return (ray.direction.x * dY - ray.direction.y * dX) > 0;
|
|
|
|
// ^ Z component of cross-product
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-22 08:21:38 +02:00
|
|
|
Casts a ray within a single square, to collide with the square borders.
|
2018-08-21 13:49:45 +02:00
|
|
|
*/
|
2018-08-22 08:21:38 +02:00
|
|
|
void castRaySquare(Ray localRay, Vector2D *nextCellOffset,
|
2018-08-21 13:49:45 +02:00
|
|
|
Vector2D *collisionPointOffset)
|
|
|
|
{
|
|
|
|
nextCellOffset->x = 0;
|
|
|
|
nextCellOffset->y = 0;
|
|
|
|
|
2018-08-22 08:21:38 +02:00
|
|
|
Ray criticalLine = localRay;
|
2018-08-21 13:49:45 +02:00
|
|
|
|
|
|
|
#define helper(c1,c2,n)\
|
|
|
|
{\
|
|
|
|
nextCellOffset->c1 = n;\
|
2018-08-22 08:21:38 +02:00
|
|
|
collisionPointOffset->c1 = criticalLine.start.c1 - localRay.start.c1;\
|
2018-08-21 13:49:45 +02:00
|
|
|
collisionPointOffset->c2 =\
|
2018-08-22 08:21:38 +02:00
|
|
|
(collisionPointOffset->c1 * localRay.direction.c2) /\
|
|
|
|
(localRay.direction.c1 == 0 ? 1 : localRay.direction.c1);\
|
2018-08-21 13:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define helper2(n1,n2,c)\
|
2018-08-22 08:21:38 +02:00
|
|
|
if (pointIsLeftOfRay(localRay.start,criticalLine) == c)\
|
2018-08-21 13:49:45 +02:00
|
|
|
helper(y,x,n1)\
|
|
|
|
else\
|
|
|
|
helper(x,y,n2)
|
|
|
|
|
2018-08-22 08:21:38 +02:00
|
|
|
if (localRay.direction.x > 0)
|
2018-08-21 13:49:45 +02:00
|
|
|
{
|
2018-08-22 08:21:38 +02:00
|
|
|
criticalLine.start.x = UNITS_PER_SQUARE;
|
2018-08-21 13:49:45 +02:00
|
|
|
|
2018-08-22 08:21:38 +02:00
|
|
|
if (localRay.direction.y > 0)
|
2018-08-21 13:49:45 +02:00
|
|
|
{
|
|
|
|
// top right
|
2018-08-22 08:21:38 +02:00
|
|
|
criticalLine.start.y = UNITS_PER_SQUARE;
|
2018-08-21 13:49:45 +02:00
|
|
|
helper2(1,1,1)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// bottom right
|
2018-08-22 08:21:38 +02:00
|
|
|
criticalLine.start.y = -1;
|
2018-08-21 13:49:45 +02:00
|
|
|
helper2(-1,1,0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-22 08:21:38 +02:00
|
|
|
criticalLine.start.x = -1;
|
2018-08-21 13:49:45 +02:00
|
|
|
|
2018-08-22 08:21:38 +02:00
|
|
|
if (localRay.direction.y > 0)
|
2018-08-21 13:49:45 +02:00
|
|
|
{
|
|
|
|
// top left
|
2018-08-22 08:21:38 +02:00
|
|
|
criticalLine.start.y = UNITS_PER_SQUARE;
|
2018-08-21 13:49:45 +02:00
|
|
|
helper2(1,-1,0)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// bottom left
|
2018-08-22 08:21:38 +02:00
|
|
|
criticalLine.start.y = -1;
|
2018-08-21 13:49:45 +02:00
|
|
|
helper2(-1,-1,1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef helper2
|
|
|
|
#undef helper
|
|
|
|
}
|
|
|
|
|
2018-08-22 08:21:38 +02:00
|
|
|
HitResult castRay(Ray ray, int (*collisionFunc)(int, int),
|
|
|
|
unsigned int maxSteps)
|
|
|
|
{
|
|
|
|
HitResult result;
|
|
|
|
|
|
|
|
result.distance = -1;
|
|
|
|
result.square.x = ray.start.x / UNITS_PER_SQUARE;
|
|
|
|
result.square.y = ray.start.y / UNITS_PER_SQUARE;
|
|
|
|
result.position = ray.start;
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < maxSteps; ++i)
|
|
|
|
{
|
|
|
|
ray.start.x = result.position.x % UNITS_PER_SQUARE;
|
|
|
|
ray.start.y = result.position.y % UNITS_PER_SQUARE;
|
|
|
|
|
|
|
|
Vector2D no, co;
|
|
|
|
|
|
|
|
castRaySquare(ray,&no,&co);
|
|
|
|
|
|
|
|
result.square.x += no.x;
|
|
|
|
result.square.y += no.y;
|
|
|
|
|
|
|
|
result.position.x += co.x;
|
|
|
|
result.position.y += co.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int aaa(int x, int y)
|
|
|
|
{
|
|
|
|
return (x < 0 || y < 0 || x > 10 || y > 10) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2018-08-21 13:49:45 +02:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
Ray r;
|
|
|
|
|
2018-08-22 08:21:38 +02:00
|
|
|
r.start.x = 4 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2;
|
|
|
|
r.start.y = 4 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2;
|
|
|
|
r.direction.x = 100;
|
|
|
|
r.direction.y = 50;
|
2018-08-21 13:49:45 +02:00
|
|
|
|
|
|
|
logRay(r);
|
|
|
|
|
2018-08-22 08:21:38 +02:00
|
|
|
castRay(r,aaa,10);
|
2018-08-21 13:49:45 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|