naev 0.12.5
collision.c File Reference

Deals with 2d collisions. More...

#include "collision.h"
#include "array.h"
#include "log.h"
#include "physics.h"
Include dependency graph for collision.c:

Go to the source code of this file.

Macros

#define FX(A, B, C, x)
#define FY(A, B, C, y)

Functions

static int PointInPolygon (const CollPolyView *at, const vec2 *ap, float x, float y)
 Checks whether or not a point is inside a polygon.
static int LineOnPolygon (const CollPolyView *at, const vec2 *ap, float x1, float y1, float x2, float y2, vec2 *crash)
 Checks whether or not a line intersects a polygon.
void poly_load (CollPoly *polygon, xmlNodePtr base, const char *name)
 Loads a polygon from an xml node.
void poly_free (CollPoly *poly)
int CollideSprite (const glTexture *at, const int asx, const int asy, const vec2 *ap, const glTexture *bt, const int bsx, const int bsy, const vec2 *bp, vec2 *crash)
 Checks whether or not two sprites collide.
int CollideSpritePolygon (const CollPolyView *at, const vec2 *ap, const glTexture *bt, int bsx, int bsy, const vec2 *bp, vec2 *crash)
 Checks whether or not a sprite collides with a polygon.
int CollidePolygon (const CollPolyView *at, const vec2 *ap, const CollPolyView *bt, const vec2 *bp, vec2 *crash)
 Checks whether or not two polygons collide. /!\ The function is not symmetric: the points of polygon 2 are tested against the polygon 1. Consequently, it works better if polygon 2 is small.
void poly_rotate (CollPolyView *rpolygon, const CollPolyView *ipolygon, float theta)
 Rotates a polygon.
const CollPolyViewpoly_view (const CollPoly *poly, double dir)
int CollideLineLine (double s1x, double s1y, double e1x, double e1y, double s2x, double s2y, double e2x, double e2y, vec2 *crash)
 Checks to see if two lines collide.
int CollideLineSprite (const vec2 *ap, double ad, double al, const glTexture *bt, const int bsx, const int bsy, const vec2 *bp, vec2 crash[2])
 Checks to see if a line collides with a sprite.
int CollideLinePolygon (const vec2 *ap, double ad, double al, const CollPolyView *bt, const vec2 *bp, vec2 crash[2])
 Checks to see if a line collides with a polygon.
int CollideCirclePolygon (const vec2 *ap, double ar, const CollPolyView *bt, const vec2 *bp, vec2 crash[2])
 Checks to see if a circle collides with a polygon.
int CollideCircleSprite (const vec2 *ap, double ar, const glTexture *bt, const int bsx, const int bsy, const vec2 *bp, vec2 *crash)
 Checks whether or not a sprite collides with a polygon.
static int linePointOnSegment (double d1, double x1, double y1, double x2, double y2, double x, double y)
int CollideLineCircle (const vec2 *p1, const vec2 *p2, const vec2 *cc, double cr, vec2 crash[2])
 Checks to see if a line collides with a circle.
int CollideCircleCircle (const vec2 *p1, double r1, const vec2 *p2, double r2, vec2 crash[2])
 Computes the collision between two circles.
double CollideCircleIntersection (const vec2 *p1, double r1, const vec2 *p2, double r2)
 Calculates the area of intersection between two circles.

Detailed Description

Deals with 2d collisions.

Definition in file collision.c.

Macro Definition Documentation

◆ FX

#define FX ( A,
B,
C,
x )
Value:
( -( A * x + C ) / B )
static cholmod_common C
Definition safelanes.c:106

Definition at line 1018 of file collision.c.

◆ FY

#define FY ( A,
B,
C,
y )
Value:
( -( B * y + C ) / A )

Definition at line 1019 of file collision.c.

Function Documentation

◆ CollideCircleCircle()

int CollideCircleCircle ( const vec2 * p1,
double r1,
const vec2 * p2,
double r2,
vec2 crash[2] )

Computes the collision between two circles.

Definition at line 1142 of file collision.c.

◆ CollideCircleIntersection()

double CollideCircleIntersection ( const vec2 * p1,
double r1,
const vec2 * p2,
double r2 )

Calculates the area of intersection between two circles.

Definition at line 1159 of file collision.c.

◆ CollideCirclePolygon()

int CollideCirclePolygon ( const vec2 * ap,
double ar,
const CollPolyView * bt,
const vec2 * bp,
vec2 crash[2] )

Checks to see if a circle collides with a polygon.

First collisions are detected on all the walls of the polygon's rectangle. Then the collisions are tested on every line of the polygon.

Parameters
[in]apOrigin of the circle.
[in]arRadius of the circle.
[in]btPolygon b.
[in]bpPosition in space of polygon b.
[out]crashPosition of the collision.
Returns
1 on collision, 0 else.
See also
CollideCirclePolygon

Definition at line 862 of file collision.c.

◆ CollideCircleSprite()

int CollideCircleSprite ( const vec2 * ap,
double ar,
const glTexture * bt,
const int bsx,
const int bsy,
const vec2 * bp,
vec2 * crash )

Checks whether or not a sprite collides with a polygon.

Parameters
[in]apPosition in space of circle a.
[in]arRadius of circle a.
[in]btTexture b.
[in]bsxPosition of x of sprite b.
[in]bsyPosition of y of sprite b.
[in]bpPosition in space of sprite b.
[out]crashActual position of the collision (only set on collision).
Returns
1 on collision, 0 else.

Definition at line 941 of file collision.c.

◆ CollideLineCircle()

int CollideLineCircle ( const vec2 * p1,
const vec2 * p2,
const vec2 * cc,
double cr,
vec2 crash[2] )

Checks to see if a line collides with a circle.

Parameters
[in]p1Point 1 of the line segment.
[in]p2Point 2 of the line segment.
[in]ccCenter of the circle.
[in]crRadius of the circle.
[out]crashPosition of the collision.
Returns
1 on collision, 0 else.

Definition at line 1030 of file collision.c.

◆ CollideLineLine()

int CollideLineLine ( double s1x,
double s1y,
double e1x,
double e1y,
double s2x,
double s2y,
double e2x,
double e2y,
vec2 * crash )

Checks to see if two lines collide.

Parameters
[in]s1xX start point of line 1.
[in]s1yY start point of line 1.
[in]e1xX end point of line 1.
[in]e1yY end point of line 1.
[in]s2xX start point of line 2.
[in]s2yY start point of line 2.
[in]e2xX end point of line 2.
[in]e2yY end point of line 2.
[out]crashPosition of the collision.
Returns
3 if lines are coincident, 2 if lines are parallel, 1 if lines just collide on a point, or 0 if they don't.

Definition at line 526 of file collision.c.

◆ CollideLinePolygon()

int CollideLinePolygon ( const vec2 * ap,
double ad,
double al,
const CollPolyView * bt,
const vec2 * bp,
vec2 crash[2] )

Checks to see if a line collides with a polygon.

First collisions are detected on all the walls of the polygon's rectangle. Then the collisions are tested on every line of the polygon.

Parameters
[in]apOrigin of the line.
[in]adDirection of the line.
[in]alLength of the line.
[in]btPolygon b.
[in]bpPosition in space of polygon b.
[out]crashPosition of the collision.
Returns
1 on collision, 0 else.
See also
CollideLinePolygon

Definition at line 729 of file collision.c.

◆ CollideLineSprite()

int CollideLineSprite ( const vec2 * ap,
double ad,
double al,
const glTexture * bt,
const int bsx,
const int bsy,
const vec2 * bp,
vec2 crash[2] )

Checks to see if a line collides with a sprite.

First collisions are detected on all the walls of the sprite's rectangle. Then the collisions are tested by pixel perfectness until collisions are actually found with the ship itself.

Parameters
[in]apOrigin of the line.
[in]adDirection of the line.
[in]alLength of the line.
[in]btTexture b.
[in]bsxPosition of x of sprite b.
[in]bsyPosition of y of sprite b.
[in]bpPosition in space of sprite b.
[out]crashPosition of the collision.
Returns
1 on collision, 0 else.
See also
CollideSprite

Definition at line 578 of file collision.c.

◆ CollidePolygon()

int CollidePolygon ( const CollPolyView * at,
const vec2 * ap,
const CollPolyView * bt,
const vec2 * bp,
vec2 * crash )

Checks whether or not two polygons collide. /!\ The function is not symmetric: the points of polygon 2 are tested against the polygon 1. Consequently, it works better if polygon 2 is small.

Parameters
[in]atPolygon a.
[in]apPosition in space of polygon a.
[in]btPolygon b.
[in]bpPosition in space of polygon b.
[out]crashActual position of the collision (only set on collision).
Returns
1 on collision, 0 else.

Definition at line 311 of file collision.c.

◆ CollideSprite()

int CollideSprite ( const glTexture * at,
const int asx,
const int asy,
const vec2 * ap,
const glTexture * bt,
const int bsx,
const int bsy,
const vec2 * bp,
vec2 * crash )

Checks whether or not two sprites collide.

This function does pixel perfect checks. If the collision actually occurs, crash is set to store the real position of the collision.

Parameters
[in]atTexture a.
[in]asxPosition of x of sprite a.
[in]asyPosition of y of sprite a.
[in]apPosition in space of sprite a.
[in]btTexture b.
[in]bsxPosition of x of sprite b.
[in]bsyPosition of y of sprite b.
[in]bpPosition in space of sprite b.
[out]crashActual position of the collision (only set on collision).
Returns
1 on collision, 0 else.

Definition at line 148 of file collision.c.

◆ CollideSpritePolygon()

int CollideSpritePolygon ( const CollPolyView * at,
const vec2 * ap,
const glTexture * bt,
int bsx,
int bsy,
const vec2 * bp,
vec2 * crash )

Checks whether or not a sprite collides with a polygon.

Parameters
[in]atPolygon a.
[in]apPosition in space of polygon a.
[in]btTexture b.
[in]bsxPosition of x of sprite b.
[in]bsyPosition of y of sprite b.
[in]bpPosition in space of sprite b.
[out]crashActual position of the collision (only set on collision).
Returns
1 on collision, 0 else.

Definition at line 233 of file collision.c.

◆ LineOnPolygon()

int LineOnPolygon ( const CollPolyView * at,
const vec2 * ap,
float x1,
float y1,
float x2,
float y2,
vec2 * crash )
static

Checks whether or not a line intersects a polygon.

Parameters
[in]atPolygon a.
[in]apPosition in space of polygon a.
[in]x1Coordinate of point 1.
[in]y1Coordinate of point 1.
[in]x2Coordinate of point 2.
[in]y2Coordinate of point 2.
[out]crashcoordinates of the intersection.
Returns
1 on collision, 0 else.

Definition at line 486 of file collision.c.

◆ linePointOnSegment()

int linePointOnSegment ( double d1,
double x1,
double y1,
double x2,
double y2,
double x,
double y )
static

Definition at line 1008 of file collision.c.

◆ PointInPolygon()

int PointInPolygon ( const CollPolyView * at,
const vec2 * ap,
float x,
float y )
static

Checks whether or not a point is inside a polygon.

Parameters
[in]atPolygon a.
[in]apPosition in space of polygon a.
[in]xCoordinate of point.
[in]yCoordinate of point.
Returns
1 on collision, 0 else.

Definition at line 440 of file collision.c.

◆ poly_free()

void poly_free ( CollPoly * poly)

Definition at line 120 of file collision.c.

◆ poly_load()

void poly_load ( CollPoly * polygon,
xmlNodePtr base,
const char * name )

Loads a polygon from an xml node.

Parameters
[out]polygonPolygon.
[in]baseXML node to parse.
[in]nameName to give the polygon for debugging purposes.

Definition at line 37 of file collision.c.

◆ poly_rotate()

void poly_rotate ( CollPolyView * rpolygon,
const CollPolyView * ipolygon,
float theta )

Rotates a polygon.

Parameters
[out]rpolygonRotated polygon.
[in]ipolygonInput polygon.
[in]thetaRotation angle (radian).

Definition at line 385 of file collision.c.

◆ poly_view()

const CollPolyView * poly_view ( const CollPoly * poly,
double dir )

Definition at line 414 of file collision.c.