VgaGames4

VgaGames4 - object man-pages

[.. upper level ..]

# vg4->object->collision_add()

Add a collision-function for two object-IDs.

SYNTAX
void vg4->object->collision_add(const char *objid1, const char *objid2, int (*collfn)(void *, unsigned int, struct VG_Coll *))
FUNCTION PARAMETERS

objid1 | Object-ID for moving object-instance
objid2 | Object-ID for hit object-instance
collfn | Collision-function: int (*collfn)(void *vgame, unsigned int instanceid, struct VG_Coll *collb) - parameters: - vgame: private structure of the game, or NULL if not existing - instanceid: instance-ID of the moving object-instance - collb: collision-struct (from the array got from vg4->collision->setpos()) - return value: should return one from VG_COLL_RETURNS or NULL = remove collision-function

DESCRIPTION
Add a collision-function for two object-IDs. To check for collisions of an object-instance with others, vg4->collision->setpos() is used. It returns an array of collisions. For each collision a collision-function for the appropriate two objects should be called. vg4->object->collision_add() is used to register a collision-function for two objects, which can be called with vg4->object->collision_call(). The collision-function is registered for moving object-instance objid1 and hitting object-instance objid2. But it will also be added inverse, that is for objid2 hitting objid1, if there is no other function already.
EXAMPLE

    /* collision-function for OBJECT1 and OBJECT2 and inverse */
    int
    collfn(void *vgame, unsigned int instanceid, struct VG_Coll *collb)
    {
      struct VG_Object *objp1, *objp2;
      int retw = VG_COLL_RETURN_CONTINUE;

      /* get object-instances */
      objp1 = vg4->object->instance_getobj(instanceid);
      objp2 = vg4->object->instance_getobj(collb->instanceid);  // or objp2 = collb->objid;

      /* return if it is not a new collision */
      if (collb->type != VG_COLL_TYPE_ENTRY) { return VG_COLL_RETURN_CONTINUE; }

      if (strcmp(objp1->objid, "OBJECT1") == 0 && strcmp(objp2->objid, "OBJECT2") == 0) {
        /* OBJECT1 is moving, hitting OBJECT2
         * OBJECT1 shall be destroyed, OBJECT2 shall remain alive
         */
        [...]
        /* return DEAD */
        retw = VG_COLL_RETURN_DEAD;

      } else if (strcmp(objp1->objid, "OBJECT2") == 0 && strcmp(objp2->objid, "OBJECT1") == 0) {
        /* OBJECT2 is moving, hitting OBJECT1
         * OBJECT1 shall be destroyed, OBJECT2 shall remain alive
         */
        [...]
        /* return CONTINUE */
        retw = VG_COLL_RETURN_CONTINUE;
      }

      return retw;
    }

    void
    insert_collfn(void)
    {
      vg4->object->collision_add("OBJECT1", "OBJECT2", collfn);
    }

SEE ALSO
vg4->object->collision_call() vg4->collision->setpos()