VgaGames4 - miscellaneous man-pages

[.. upper level ..]

vg4->misc->move_and_check_collisions()

Move an object-instance checking for collisions.

SYNTAX
VG_BOOL vg4->misc->move_and_check_collisions(void *vgame, unsigned int instanceid, struct VG_RectCent *rcnt_actual, unsigned int ctag, const struct VG_RectCent *rcnt_array, int npos)

FUNCTION PARAMETERS
vgame Private structure of the game, or NULL if not existing
instanceid Instance-ID
rcnt_actual Actual rectangle of the object-instance and returning the new rectangle
ctag Collision-tag
rcnt_array Array of rectangles to move
npos Number of rectangles in rcnt_array

RETURN VALUE
Returns boolean: - VG_TRUE: OK - VG_FALSE: Object-instance is dead

DESCRIPTION
Move an object-instance checking for collisions. The object-instance will be moved step by step beginning from rcnt_actual according to the rectangles in rcnt_array, whereat at any rectangle collisions are checked using vg4->object->collision_call() as following: - call of vg4->collision->setpos() to get the collisions at the actual rectangle - call of vg4->object->collision_call() with the found collisions to call their collision-functions - acting according to the return-value of vg4->object->collision_call() - VG_COLL_RETURN_DEAD: stop iterating the rectangles in the array and return VG_FALSE - VG_COLL_RETURN_STOP: stop iterating the rectangles in the array and go one rectangle back, call vg4->collision->setpos() for the corrected rectangle and return VG_TRUE - VG_COLL_RETURN_STOP_X or VG_COLL_RETURN_STOP_Y: check again with the previous x- or y-position - VG_COLL_RETURN_CONTINUE: update rcnt_actual with the actual rectangle and continue iterating the rectangles in the array rcnt_actual will be updated to the new rectangle.

EXAMPLE
/* example of an object's run()-function */

/* private struct for this object-instance */
struct s_obj {
  struct VG_RectCent rectc;  /* rectangle of object-instance (position and width/size) */
  struct VG_Image *imgp;     /* image */
  int angle;                 /* angle of rotation = moving direction */
};

/* run()-function */
static VG_BOOL
f_run(void *vgame, struct VG_Object *objp)
{
  const int factor = 10;
  struct s_obj *objpriv;
  int npos;
  struct VG_RectCent *rcentp;

  if (objp == NULL) { return VG_TRUE; }
  objpriv = (struct s_obj *)objp->opriv;  /* get private struct */

  /* get rectangles for moving the next step */
  npos = vg4->misc->moving_one_step(&objpriv->rectc, objpriv->angle, factor, &rcentp);

  /* check for collisions for each rectangle (ctag is the collision-tag defined elsewhere) */
  if (!vg4->misc->move_and_check_collisions(vgame, objp->instanceid, &objpriv->rectc, ctag, rcentp, npos)) {
    /* object-instance is dead */
    if (rcentp != NULL) { free(rcentp); }
    return VG_TRUE;
  }
  if (rcentp != NULL) { free(rcentp); }

  return VG_TRUE;
}