VgaGames4

VgaGames4 - collision man-pages

[.. upper level ..]

# vg4->collision->setpos()

Set position of an object-instance and get collisions.

SYNTAX
int vg4->collision->setpos(unsigned int ctag, unsigned int instanceid, const struct VG_Rect *nrect, struct VG_Coll **collp)
FUNCTION PARAMETERS

ctag | Collision-tag
instanceid | Instance-ID
nrect | Position of object-instance
collp | For returning allocated array of collisions (must be freed with free()) may be NULL

RETURN VALUE
Returns number of collisions in collp
DESCRIPTION
Check for collisions of an object-instance. The object-instance is set to nrect and checked at this position. collp returns not just collisions but different types of collision as defined in VG_COLL_TYPES: - entering into collision, that is: a new collision with another object-instance - collision repeated, that is: still collides with the other object-instance - leaving collision, that is: no more collision with the other object-instance Leaving a collision can also appear when the other object-instance in collp was removed, then instanceid in collp is no more valid and vg4->object->instance_getobj() would return NULL, but objid in collp is still filled with its object-ID. collp may be NULL if the object-instance shall just be set to another position, which is mainly used when setting back to the previous position because of an obstacle, or for manually testing if collisions are found at that position. The return value is the number of found collisions.
EXAMPLE

    int canz, i1;
    unsigned int tag, obj_instanceid;
    struct VG_Coll *collp;
    struct VG_Rect rect;

    [...]

    canz = vg4->collision->setpos(tag, obj_instanceid, &rect, &collp);

    for (i1 = 0; i1 < canz; i1++) {
      if (collp[i1].type == VG_COLL_TYPE_ENTRY) {  /* new collision */
        printf("Object-instance %u collides new with object-instance %u\n", obj_instanceid, collp[i1].instanceid);
      } else if (collp[i1].type == VG_COLL_TYPE_REPEAT) {  /* repeated collision */
        printf("Object-instance %u still collides with object-instance %u\n", obj_instanceid, collp[i1].instanceid);
      } else if (collp[i1].type == VG_COLL_TYPE_LEAVE) {  /* leaving collision */
        printf("Object-instance %u has left collision with object-instance %u\n", obj_instanceid, collp[i1].instanceid);
      }
    }

    if (collp != NULL) { free(collp); }