object::object() { /* data */ mTrans = matrixAlloc(); mOrigin = new float[3]; mOriginEx = new float[3]; mAngle = new float[3]; mScale = new float[3]; matrixIdent(mTrans); vectorNull(mOrigin); vectorNull(mAngle); vectorUnit(mScale); /* physics */ mPosVel = new float[3]; mAngVel = new float[3]; vectorNull(mPosVel); vectorNull(mAngVel); mPosAcc = new float[3]; mAngAcc = new float[3]; vectorNull(mPosAcc); vectorNull(mAngAcc); mMovSpdMax = BIG_NUMBER; mRotSpdMax = BIG_NUMBER; mStyle = 0; mCollide = true; mTicks = -1; mTarget = NULL; /* attributes */ mParent = NULL; mChanged = true; }
//=========================================================================== // Transforms vertices + lights to Camera Space for this frame void Scene::objectToCameraSpace(cameraData* viewCam) { matrix rotMatrix; matrixIdent(&rotMatrix); invRotMatrixFromVec3(&rotMatrix, &viewCam->dir, &viewCam->up); renderVertices.clear(); for (int i = 0; i < modelCount; i++) { model* currentModel = &sceneModels[i]; for (int j = 0; j < currentModel->vertexCount; j++) { int n = currentModel->vertexIndex + j; renderVertices.push_back(sceneVertices[n]); // Transform verts from model space to world space matrixVec3Rot(&renderVertices[n], ¤tModel->rotation, &sceneVertices[n]); renderVertices[n] = renderVertices[n] + currentModel->position; // Transform verts from world space to camera space renderVertices[n] = renderVertices[n] - viewCam->position; matrixVec3Rot(&renderVertices[n], &rotMatrix, &renderVertices[n]); } } // Transform Lights from world space to camera space vec3Sub(&renderLight.position, &sceneLight.position, &viewCam->position); matrixVec3Rot(&renderLight.position, &rotMatrix, &renderLight.position); vec3Cpy(&renderLight.color, &sceneLight.color); }
static void vehicleReset(Chassis* chassis, float mass, const vec3* offset) { Chassis* c = chassis; //================ // Reset chassis //================ c->mass = mass; c->inertia = 2.f/5.f * SQR(1.f) * c->mass; matrixIdent(&c->pose); c->pose.v[3].v3 = *offset; veczero(&c->vel); veczero(&c->angVel); c->steer = 1.0f; //================== // Reset suspension //================== vec3 offset0 = {+1.f, +1.4f+0.6f, 0.f}; vec3 offset1 = {-1.f, +1.4f+0.6f, 0.f}; vec3 offset2 = {+1.f, -1.4f+0.6f, 0.f}; vec3 offset3 = {-1.f, -1.4f+0.6f, 0.f}; suspensionReset(c->suspension[0], &offset0); suspensionReset(c->suspension[1], &offset1); suspensionReset(c->suspension[2], &offset2); suspensionReset(c->suspension[3], &offset3); Wheel* w0 = c->suspension[0]->wheel; Wheel* w1 = c->suspension[1]->wheel; Wheel* w2 = c->suspension[2]->wheel; Wheel* w3 = c->suspension[3]->wheel; w0->maxSteer = DEG2RAD(35.f); w1->maxSteer = DEG2RAD(35.f); w2->maxSteer = DEG2RAD(0.f); w3->maxSteer = DEG2RAD(0.f); w0->driven = true; w1->driven = true; w2->driven = false; w3->driven = false; // Set the correct wheel position for (int i=0; i<numWheels; i++) { Suspension* s = c->suspension[i]; Wheel* w = s->wheel; vec3mtx33mulvec3(&s->worldOffset, &c->pose, &s->offset); vec3mtx43mulvec3(&s->worldDefaultPos, &c->pose, &s->offset); w->pos = s->worldDefaultPos; } }
// Make a matrix to rotate around the Y axis // Positive angle = clockwise rotation looking down void matrixYAxisRot(matrix* m, float angle) { float sinTh = sinf(angle); float cosTh = cosf(angle); matrixIdent(m); m->row[0].x = cosTh; m->row[1].x = 0.f; m->row[2].x = sinTh; m->row[0].y = 0.f; m->row[1].y = 1.f; m->row[2].y = 0.f; m->row[0].z = -sinTh; m->row[1].z = 0.f; m->row[2].z = cosTh; }
//=========================================================================== model* Scene::addModel(vec3* position, int vertexIndex, int vertexCount, int polyIndex, int polyCount) { manta_assert(modelCount < MAX_SCENE_MODELS, "Error, attempting to add too many Models!\n"); model* newModel = &sceneModels[modelCount++]; newModel->vertexIndex = vertexIndex; newModel->vertexCount = vertexCount; newModel->polyCount = polyCount; newModel->polyIndex = polyIndex; matrixIdent(&newModel->rotation); vec3 up, dir; vec3Set(&up, 1.f, 0.f, 0.f); vec3Set(&dir, 0.f, 0.f, 1.f); rotMatrixFromVec3(&newModel->rotation, &dir, &up); vec3Cpy(&newModel->position, position); return newModel; }
//=========================================================================== void Scene::prepareModelRenderData(cameraData* viewCam) { matrix rotMatrix; matrixIdent(&rotMatrix); invRotMatrixFromVec3(&rotMatrix, &viewCam->dir, &viewCam->up); int newPolyIndex = 0; renderModels.clear(); renderPolygons.clear(); for (int i = 0; i < modelCount; i++) { renderModels.push_back(sceneModels[i]); model* mdl = &renderModels.back(); int newPolyCount = 0; // For every polygon in each model for (int j = 0; j < mdl->polyCount; j++) { int n = j + mdl->polyIndex; // Copy scene polygon to render polygons renderPolygons.push_back(scenePolygons[n]); polygon* poly = &renderPolygons.back(); // Transform normals to camera space matrixVec3Rot(&poly->normal, &mdl->rotation, &poly->normal); matrixVec3Rot(&poly->normal, &rotMatrix, &poly->normal); poly->normal.Normalize(); poly->point[0] = &renderVertices[poly->pointIndex[0]]; poly->point[1] = &renderVertices[poly->pointIndex[1]]; poly->point[2] = &renderVertices[poly->pointIndex[2]]; // TODO - Store points B and C as vectors from A poly->d = vec3Dot(&poly->normal, poly->point[0]); newPolyCount++; } mdl->polyIndex = newPolyIndex; mdl->polygons = renderPolygons.begin() += newPolyIndex; mdl->polyCount = newPolyCount; newPolyIndex += newPolyCount; } polyCount = newPolyIndex; }
float *vectorFromAngles(float *angles) { float *result; float **tempA; float **tempB; float **applyTrans; tempA = matrixRotation('x', angles[1]); tempB = matrixRotation('y', angles[2]); applyTrans = matrixIdent(4); applyTrans = matrixUpdate(applyTrans, matrixMult(applyTrans, tempA, 4), 4); applyTrans = matrixUpdate(applyTrans, matrixMult(applyTrans, tempB, 4), 4); result = vectorNull(4); result[2] = 1; result[3] = 1; result = vectorUpdate(result, vectorMatrixMult(result, applyTrans, 4)); matrixFree(tempA, 4); matrixFree(tempB, 4); matrixFree(applyTrans, 4); return result; }