Amulets & Armor  v1.02
Open Source Game
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Macros | Functions | Variables
_3D_COLLI

3D Collision Routines More...

Macros

#define COLLIDE_3D_MAX_LINES_FOR_SECTORS   100
 
#define easyabs(x)   (((x)>=0)?(x):-(x))
 
#define HIT_TYPE_LINE   0
 
#define HIT_TYPE_OBJECT   1
 
#define MAX_LINE_HITS   20
 

Functions

E_Boolean CheckLineCanStepThrough (T_word16 i, T_3dObject *p_obj)
 
E_Boolean Collide3dCheckLineOfSight (T_sword16 sightStartX, T_sword16 sightStartY, T_sword16 sightEndX, T_sword16 sightEndY)
 
E_Boolean Collide3dCheckLineOfSightWithZ (T_sword16 sightStartX, T_sword16 sightStartY, T_sword16 sightEndX, T_sword16 sightEndY, T_sword16 sightZ)
 
T_sword16 Collide3dCheckSegmentHitBox (T_word16 lineNum, T_sword16 x1, T_sword16 y1, T_sword16 x2, T_sword16 y2)
 
T_sword16 Collide3dCheckSegmentHitsSegment (T_sword16 x1, T_sword16 y1, T_sword16 x2, T_sword16 y2, T_word16 lineNum)
 
T_word16 Collide3dFindWallList (T_sword16 x1, T_sword16 y1, T_sword16 x2, T_sword16 y2, T_sword16 z, T_word16 maxWalls, T_wallListItem *p_list, T_byte8 wallTypes)
 
E_Boolean Collide3dGetSectorsInBox (T_sword32 x, T_sword32 y, T_word16 radius, T_word16 maxSectors, T_word16 *p_sectors, T_word16 *p_numSectors)
 
T_word16 Collide3dGetWallsInBox (T_sword32 x, T_sword32 y, T_word16 radius, T_word16 maxWalls, T_word16 *p_walls)
 
E_Boolean Collide3dMoveToXYZ (T_3dObject *p_obj, T_sword32 newX, T_sword32 newY, T_sword32 newZ)
 
E_Boolean Collide3dObjectToObjectCheckLineOfSight (T_3dObject *p_from, T_3dObject *p_to)
 
E_Boolean Collide3dObjectToXYCheckLineOfSight (T_3dObject *p_from, T_lineOfSightLast *p_lastSight, T_sword16 x, T_sword16 y)
 
E_Boolean Collide3dObjectToXYCheckLineOfSightWithZ (T_3dObject *p_from, T_sword16 x, T_sword16 y, T_sword16 z)
 
T_byte8 Collide3dPointOnRight (T_sword32 lineX1, T_sword32 lineY1, T_sword32 lineX2, T_sword32 lineY2, T_sword32 pointX, T_sword32 pointY)
 
T_void Collide3dSetWallDefinition (T_word16 lineFlags)
 
T_void Collide3dUpdateLineOfSightLast (T_lineOfSightLast *p_lastSight, T_3dObject *p_target)
 
T_void IAddSurroundingSector (T_word16 sector)
 
T_sword16 ICheckLineHitsLine (T_sword16 x1, T_sword16 y1, T_sword16 x2, T_sword16 y2, T_word16 lineNum)
 
E_Boolean IIsFloorAndCeilingOk (T_word16 lineNum, E_Boolean f_add, T_3dObject *p_obj)
 
T_void ILineHitInBlock (T_sword16 lastX, T_sword16 lastY, T_sword16 x1, T_sword16 y1, T_sword16 x2, T_sword16 y2, T_sword32 index, T_sword16 radius, T_3dObject *p_obj)
 
T_sword16 IMoveToXYWithStep (T_sword32 *oldX, T_sword32 *oldY, T_sword32 newx, T_sword32 newy, T_sword32 radius, T_sword32 step, T_sword32 foot, T_sword32 head, T_sword16 height, T_3dObject *p_obj)
 
T_byte8 IOnRightOfLine (T_sword16 x, T_sword16 y, T_word16 line)
 
T_void IUpdateSectorHeights (T_word16 sector)
 
E_Boolean LineHit (T_sword16 lastX, T_sword16 lastY, T_sword16 x1, T_sword16 y1, T_sword16 x2, T_sword16 y2, T_sword16 radius, T_3dObject *p_obj)
 
E_Boolean MoveTo (T_sword32 oldX, T_sword32 oldY, T_sword32 newX, T_sword32 newY, T_sword32 distance, T_3dObject *p_obj)
 
E_Boolean MoveToFast (T_sword32 *oldX, T_sword32 *oldY, T_sword32 newX, T_sword32 newY, T_sword32 distance, T_sword32 radius, T_sword32 foot, T_sword32 head, T_sword16 height, T_3dObject *p_obj)
 
T_sword32 Mult32By32AndDiv32 (T_sword32 a, T_sword32 b, T_sword32 c)
 
T_sword32 Mult32x32AndCompare (T_sword32 a, T_sword32 b, T_sword32 c, T_sword32 d)
 
T_void ProjectXYOntoLine (T_sword32 *pointX, T_sword32 *pointY, T_sword32 slopeX, T_sword32 slopeY)
 
T_void View3dAllowDip (T_void)
 
T_void View3dDisallowDip (T_void)
 
T_word16 View3dFindLineHits (T_sword16 x1, T_sword16 y1, T_sword16 x2, T_sword16 y2, T_word16 maxHits, T_sword16 *lines)
 
T_void View3dGetCeilingBelow (T_word16 *below)
 
T_void View3dGetFloorAbove (T_word16 *above)
 
T_void View3dGetFloorAndCeilingHeight (T_sword16 *floor, T_sword16 *ceiling)
 
T_word16View3dGetSurroundingSectors (T_word16 *numSectors)
 
E_Boolean View3dIsAllowDip (T_void)
 
T_sword16 View3dMoveTo (T_sword32 *oldX, T_sword32 *oldY, T_word16 angle, T_sword32 step, T_sword32 radius, T_sword32 foot, T_sword32 head, T_sword16 height, T_3dObject *p_obj)
 
T_sword16 View3dMoveToFast (T_sword32 *oldX, T_sword32 *oldY, T_word16 angle, T_sword32 step, T_sword32 radius, T_sword32 foot, T_sword32 head, T_sword32 height, T_3dObject *p_obj)
 
T_sword16 View3dMoveToXY (T_sword32 *oldX, T_sword32 *oldY, T_sword32 newx, T_sword32 newy, T_sword32 radius, T_sword32 foot, T_sword32 head, T_sword16 height, T_3dObject *p_obj)
 
T_sword16 View3dMoveToXYFast (T_sword32 *oldX, T_sword32 *oldY, T_sword32 newx, T_sword32 newy, T_sword32 radius, T_sword32 foot, T_sword32 head, T_sword16 height, T_3dObject *p_obj)
 
E_Boolean View3dObjectHitFast (T_sword16 x, T_sword16 y, T_word16 radius, T_sword16 lastX, T_sword16 lastY, T_sword16 zBottom, T_sword16 zTop, T_sword16 height, T_3dObject *p_movingObject)
 
T_void View3dSetExceptObject (T_word16 exceptId)
 
T_void View3dSetExceptObjectByPtr (T_objMoveStruct *p_objMove)
 

Variables

T_word16 G_numHits = 0
 

Detailed Description

3D Collision Routines

See Also
http://www.amuletsandarmor.com/AALicense.txt

Macro Definition Documentation

#define COLLIDE_3D_MAX_LINES_FOR_SECTORS   100

Collide3dGetSectorsInBox determines all the sectors that are in a square radius box. In addition, it returns a boolean telling if it found the box touching a void area.

Parameters
y– Center of box
radius– Radius of box
maxSectors– Max sectors allowed to be found
p_sectors– Where to put the sectors
p_numSectors– Pointer to store num sectors found
Returns
Found void area
#define easyabs (   x)    (((x)>=0)?(x):-(x))
#define HIT_TYPE_LINE   0
#define HIT_TYPE_OBJECT   1
#define MAX_LINE_HITS   20

Function Documentation

E_Boolean CheckLineCanStepThrough ( T_word16  i,
T_3dObject p_obj 
)
E_Boolean Collide3dCheckLineOfSight ( T_sword16  sightStartX,
T_sword16  sightStartY,
T_sword16  sightEndX,
T_sword16  sightEndY 
)
E_Boolean Collide3dCheckLineOfSightWithZ ( T_sword16  sightStartX,
T_sword16  sightStartY,
T_sword16  sightEndX,
T_sword16  sightEndY,
T_sword16  sightZ 
)
T_sword16 Collide3dCheckSegmentHitBox ( T_word16  lineNum,
T_sword16  x1,
T_sword16  y1,
T_sword16  x2,
T_sword16  y2 
)

Collide3dCheckSegmentHitBox determines quickly if a line hits a box.

Parameters
lineNum– Wall to check floors and ceilings.
x1– Left point of box
y1– Upper point of box
x2– Right of box
y2– Lower of box
Returns
TRUE=collision, else FALSE
T_sword16 Collide3dCheckSegmentHitsSegment ( T_sword16  x1,
T_sword16  y1,
T_sword16  x2,
T_sword16  y2,
T_word16  lineNum 
)
T_word16 Collide3dFindWallList ( T_sword16  x1,
T_sword16  y1,
T_sword16  x2,
T_sword16  y2,
T_sword16  z,
T_word16  maxWalls,
T_wallListItem p_list,
T_byte8  wallTypes 
)
E_Boolean Collide3dGetSectorsInBox ( T_sword32  x,
T_sword32  y,
T_word16  radius,
T_word16  maxSectors,
T_word16 p_sectors,
T_word16 p_numSectors 
)
T_word16 Collide3dGetWallsInBox ( T_sword32  x,
T_sword32  y,
T_word16  radius,
T_word16  maxWalls,
T_word16 p_walls 
)

Collide3dGetLinesInBox takes the given square (coordinate and square radius) and determines all walls that collide with this box.

Parameters
x– X Center of box
y– Y Center of box
radius– Radius of box
maxWalls– Max walls allowed to find
p_walls– Where to put the walls
Returns
Number walls/lines found
E_Boolean Collide3dMoveToXYZ ( T_3dObject p_obj,
T_sword32  newX,
T_sword32  newY,
T_sword32  newZ 
)

Collide3dMoveToXYZ takes an object and moves to the given XYZ.

Parameters
p_obj– Object to move
newX– X Target end-point
newY– Y Target end-point
newZ– Z Target end-point
Returns
TRUE=collision
E_Boolean Collide3dObjectToObjectCheckLineOfSight ( T_3dObject p_from,
T_3dObject p_to 
)
E_Boolean Collide3dObjectToXYCheckLineOfSight ( T_3dObject p_from,
T_lineOfSightLast p_lastSight,
T_sword16  x,
T_sword16  y 
)
E_Boolean Collide3dObjectToXYCheckLineOfSightWithZ ( T_3dObject p_from,
T_sword16  x,
T_sword16  y,
T_sword16  z 
)
T_byte8 Collide3dPointOnRight ( T_sword32  lineX1,
T_sword32  lineY1,
T_sword32  lineX2,
T_sword32  lineY2,
T_sword32  pointX,
T_sword32  pointY 
)
T_void Collide3dSetWallDefinition ( T_word16  lineFlags)

in a line are actually used for determine a collision. Currently, only 2 types of definitions are used. A) LINE_IS_IMPASSIBLE <== used for the player and normal objects (including missiles) B) LINE_IS_IMPASSIBLE | LINE_IS_CREATURE_IMPASSIBLE <== used for normal creatures

T_void Collide3dUpdateLineOfSightLast ( T_lineOfSightLast p_lastSight,
T_3dObject p_target 
)
T_void IAddSurroundingSector ( T_word16  sector)

TARGET_UNIX This routine adds a sector to the surrounding sector list if it is not already in the sector list.

Parameters
sector– Sector to add
T_sword16 ICheckLineHitsLine ( T_sword16  x1,
T_sword16  y1,
T_sword16  x2,
T_sword16  y2,
T_word16  lineNum 
)

ICheckLineHitsLine takes in the coordinates of a line and determines if it hits the given line number on the map. If they do hit, the distance to that line is returned.

Parameters
x1– X First point of line to check
y1– X First point of line to check
x2– Y Second point of line to check
y2– Y Second point of line to check
lineNum– Line to check collision with
Returns
Distance along line of where hit occurs or -1 if no hit.

!! DIVIDE ZERO ERROR HERE

E_Boolean IIsFloorAndCeilingOk ( T_word16  lineNum,
E_Boolean  f_add,
T_3dObject p_obj 
)

Given a line/wall on the map, determine if the floor and ceiling of both sides of the line are in agreement with what is allowed for going over edges. In addition, note if we wish to add the sectors to the surrounding sector list.

Parameters
lineNum– Wall to check floors and ceilings.
f_add– Flag where TRUE=add to sector list
p_obj– Source object being checked
Returns
FALSE = colliding with an edge.
T_void ILineHitInBlock ( T_sword16  lastX,
T_sword16  lastY,
T_sword16  x1,
T_sword16  y1,
T_sword16  x2,
T_sword16  y2,
T_sword32  index,
T_sword16  radius,
T_3dObject p_obj 
)

ILineHitInBlock checks all the lines/walls in a block map to determine if there is a collision. At the same time it upkeeps a list of sectors this square is over and what are the highest and lowest sector of that square.

Parameters
lastX– Old X location for angular information
lastY– Old Y location for angular information
x1– Left corner of square
y1– Upper corner of square
x2– Right corner of square
y2– Lower corner of square
index– Index to list of walls to consider
radius– Radius of the given box
p_obj– Source object being checked
T_sword16 IMoveToXYWithStep ( T_sword32 oldX,
T_sword32 oldY,
T_sword32  newX,
T_sword32  newY,
T_sword32  radius,
T_sword32  step,
T_sword32  foot,
T_sword32  head,
T_sword16  height,
T_3dObject p_obj 
)

IMoveToXYWithStep moves the player to given new location and must also be given the amount of distance is being traveled.

NOTE: G_currentHeight MUST be set in order for this routine to work correctly.

Parameters
oldX– X Start location and place to put final ending location.
oldY– Y Start location and place to put final ending location.
newX– End X location to move to
newY– End Y location to move to
radius– Radius of collision detection.
step– Amount to step along direction
foot– Foot level
head– Head level
height– Height of this object
p_obj– Object doing the movement
T_byte8 IOnRightOfLine ( T_sword16  x,
T_sword16  y,
T_word16  line 
)
T_void IUpdateSectorHeights ( T_word16  sector)

IUpdateSectorHeights is used to update the group of variables that tell what are the heightest and lowest sectors in a square and what their values are.

Parameters
sector– sector to update
E_Boolean LineHit ( T_sword16  lastX,
T_sword16  lastY,
T_sword16  x1,
T_sword16  y1,
T_sword16  x2,
T_sword16  y2,
T_sword16  radius,
T_3dObject p_obj 
)

LineHit is used to determine if any line collides with the given box. It also works to determine the sectors, their heights, etc. in the area.

NOTE: Due to the way this routine works (find the four corners) and the size of the map blocks, do NOT call this routine with a box bigger than 256x256.

Parameters
lastX– Old X location for angular information
lastY– Old Y location for angular information
x1– Left corner of square
y1– Upper corner of square
x2– Right corner of square
y2– Lower corner of square
radius– Radius of the given box
p_obj– Source object
Returns
TRUE=collision, else FALSE
E_Boolean MoveTo ( T_sword32  oldX,
T_sword32  oldY,
T_sword32  newX,
T_sword32  newY,
T_sword32  distance,
T_3dObject p_obj 
)

MoveTo takes a given object and tries to move it to another location. If a collision is detected, a flag is returned and the global variables are filled with additional information.

Parameters
oldX– Start X point
oldY– Start Y point
newX– Target X end-point
newY– Target Y end-point
distance– How far along this is (status)
p_obj– Object being moved
Returns
TRUE=collision
E_Boolean MoveToFast ( T_sword32 oldX,
T_sword32 oldY,
T_sword32  newX,
T_sword32  newY,
T_sword32  distance,
T_sword32  radius,
T_sword32  foot,
T_sword32  head,
T_sword16  height,
T_3dObject p_movingObject 
)

MoveToFast takes a given object and tries to move it to another location. If a collision is detected, a flag is returned and the global variables are filled with additional information.

Parameters
oldX– X Start point, and returned final point
oldY– Y Start point, and returned final point
newX– X Target end-point
newY– Y Target end-point
distance– How far along this is (status)
radius– Size of object
foot– Foot height (foot + climbHeight)
head– top of head
height– Height of object (fully)
p_movingObject– Object being moved
Returns
TRUE=collision
T_sword32 Mult32By32AndDiv32 ( T_sword32  a,
T_sword32  b,
T_sword32  c 
)
T_sword32 Mult32x32AndCompare ( T_sword32  a,
T_sword32  b,
T_sword32  c,
T_sword32  d 
)
T_void ProjectXYOntoLine ( T_sword32 pointX,
T_sword32 pointY,
T_sword32  slopeX,
T_sword32  slopeY 
)

ProjectXYOntoLine does the calculation necessary to do a linear projection of a vector going through the origin (the point) along a given line through the origin (the slope). The new point is returned by reference.

NOTE: Large values can cause overflow. Always be careful.

Parameters
pointX– X Point to project
pointY– Y Point to project
slopeX– X Slope to project along
slopeY– Y Slope to project along
T_void View3dAllowDip ( T_void  )

Allow the next collisions to consider water dipping.

T_void View3dDisallowDip ( T_void  )

Disallow the next collisions to consider water dipping.

T_word16 View3dFindLineHits ( T_sword16  x1,
T_sword16  y1,
T_sword16  x2,
T_sword16  y2,
T_word16  maxHits,
T_sword16 lines 
)

View3dFindLineHits takes the given line coordinates and array and determines a list of lines that intersect. Instead of returning intersection coordinates, distances to that line is returned.

Parameters
x1– First X point of line to check
y1– First Y point of line to check
x2– Second X point of line to check
y2– Second Y point of line to check
maxHits– Maximum number of array points
lines– List of lines and distances found. The array alternates between line number and then distance.
Returns
Number of lines hit.
T_void View3dGetCeilingBelow ( T_word16 below)

View3dGetCeilingBelow returns the lowest ceiilng that the collisional move moved over.

Parameters
below– Returned ceiling
T_void View3dGetFloorAbove ( T_word16 above)

View3dGetFloorAbove returns the highest floor that the collisional move moved over.

Parameters
above– Returned floor
T_void View3dGetFloorAndCeilingHeight ( T_sword16 floor,
T_sword16 ceiling 
)

View3dGetFloorAndCeilingHeight is used to get the highest and lowest floor and ceiling of the last collisional move.

Parameters
floor– Returned floor height
ceiling– Returned ceiling height
T_word16* View3dGetSurroundingSectors ( T_word16 numSectors)

This routine returns a pointer to a list of sectors and the number sectors in that list.

NOTE: The data at the returned pointer is NOT to be modified, just copied.

Parameters
numSectors– Returned number of sectors in list.
Returns
Pointer to list of sectors
E_Boolean View3dIsAllowDip ( T_void  )
T_sword16 View3dMoveTo ( T_sword32 oldX,
T_sword32 oldY,
T_word16  angle,
T_sword32  step,
T_sword32  radius,
T_sword32  foot,
T_sword32  head,
T_sword16  height,
T_3dObject p_obj 
)

View3dMoveTo is used to move the player from the current point to a new point.

Parameters
oldX– X Start location and place to put final ending location.
oldY– Y Start location and place to put final ending location.
angle– What angle to move to
step– Amount to step along direction
radius– Radius of collision detection.
foot– Foot height of object moving
head– Head height of object moving
height– Current height.
p_obj– Pointer to object moving
T_sword16 View3dMoveToFast ( T_sword32 oldX,
T_sword32 oldY,
T_word16  angle,
T_sword32  step,
T_sword32  radius,
T_sword32  foot,
T_sword32  head,
T_sword32  height,
T_3dObject p_obj 
)

View3dMoveToFast is the same as View3dMoveTo but does not try to slide and thus is a faster version.

T_sword16 View3dMoveToXY ( T_sword32 oldX,
T_sword32 oldY,
T_sword32  newx,
T_sword32  newy,
T_sword32  radius,
T_sword32  foot,
T_sword32  head,
T_sword16  height,
T_3dObject p_obj 
)

View3dMoveTo is used to move the player from the current point to a new point.

Parameters
oldX– Start X location and place to put final ending location.
oldY– Start Y location and place to put final ending location.
newx– New X location to move to
newy– New Y location to move to
radius– Radius of collision detection.
foot– Foot height of object
head– Head height of object
height– Current height.
p_obj– Object moving
T_sword16 View3dMoveToXYFast ( T_sword32 oldX,
T_sword32 oldY,
T_sword32  newx,
T_sword32  newy,
T_sword32  radius,
T_sword32  foot,
T_sword32  head,
T_sword16  height,
T_3dObject p_obj 
)

View3dMoveToXYFast is used to move from a point to another point using the fast version of the algorithms.

Parameters
oldX– Start X location and place to put final ending location.
oldY– Start Y location and place to put final ending location.
newx– New X location to move to
newy– New Y location to move to
foot– Foot height of object
head– Head height of object
radius– Radius of collision detection.
height– Current height.
p_obj– Object moving
E_Boolean View3dObjectHitFast ( T_sword16  x,
T_sword16  y,
T_word16  radius,
T_sword16  lastX,
T_sword16  lastY,
T_sword16  zBottom,
T_sword16  zTop,
T_sword16  height,
T_3dObject p_movingObject 
)

View3dObjectHitFast is used to determine if an object will collide with another object or volume.

Parameters
x– Center X location to check
y– Center Y location to check
radius– Radius around point to check
lastX– Last X location for angular info.
lastY– Last Y location for angular info.
zBottom– Bottom of the object
zTop– Top of the object
height– Height of full object
p_movingObject– Object being moved
Returns
TRUE=object found, else FALSE
T_void View3dSetExceptObject ( T_word16  exceptId)

View3dSetExceptObject declares what object is to be consider non- collidable

Parameters
exceptId– ID of object to exclude
T_void View3dSetExceptObjectByPtr ( T_objMoveStruct p_objMove)

View3dSetExceptObjectByPtr declares what object is to be consider non- collidable

Parameters
p_objMove– Direct pointer to object's move struc to exclude.

Variable Documentation

T_word16 G_numHits = 0