Ok, time for another question! Haha I looked through all the old posts on ray casting, gravity, and collision but could not find a good answer to my question. I am using a ray cast to determine whether my player object is about to make contact with the floor. If not, the player object falls. If so, the player object stops and "rests" on the floor (rather, it hovers just above the floor). Here is the code I am using to check objects:
int Gravity::updateGravityObjects () {
// first thing, make sure this instance is enabled
if (grav_enabled) {
// start processing objects.
for (int i = 0; i < obj_count; i++) {
// check that the object is enabled
if (enabled[i]) {
// get the old x, y, and z values for the object
float old_x = dbObjectPositionX (object[i]);
float old_y = dbObjectPositionY (object[i]);
float old_z = dbObjectPositionZ (object[i]);
// cast a ray from the object to the ground
int objectHit = SC_RayCast (0, old_x, old_y, old_z, old_x, old_y-2.0f, old_z, 0);
// if an object was hit, check if it was in the list. If not, move the object
// down fall_rate units
if (objectHit) {
for (int j = 0; j < stop_obj_count; j++) {
// check if this object was the one that was hit
if (objectHit == stop_object[j]) {
fall_rate[i] = 0.0f;
return objectHit;
}
}
}
else {
if (fall_rate[i] > 0) {
dbPositionObject (object[i], old_x, old_y-fall_rate[i], old_z);
if (fall_rate[i] < MAX_RATE)
fall_rate[i] += ACCEL_CONSTANT;
}
else {
fall_rate[i] = ACCEL_CONSTANT;
}
}
}
}
return 0;
}
}
The ray cast does not seem to be working at all. I just continue to fall, straight through the floor of my level. Does anyone have any idea why the ray cast may not work? This code is not complete yet so I understand if there are bugs people may notice!!