Math::cVector CollisionDetection::CollideWithWorld(Math::cVector& position, Math::cVector& velocity,float length,bool isCamCollision) { if (velocity.GetLength() == 0) return position; CollisionData coll; coll.foundCollision = false; coll.nearestDistance = 0; coll.sourcepoint = position; coll.velocity = velocity.CreateNormalized(); coll.length = length; for (int j = 0; j < mVertexPosition.size(); j += 3) { Math::cVector planeOrigin = mVertexPosition[mVertexIndices[j]]; Math::cVector B = mVertexPosition[mVertexIndices[j + 1]]; Math::cVector C =mVertexPosition[mVertexIndices[j + 2]]; RayTriangleIntersection(planeOrigin,B,C, length, &coll); } if (!isCamCollision) return ResolvePlayerCollision(position,velocity, coll); else return ResolveCameraCollision(position,velocity, coll); }
void eae6320::Graphics::traceRayHorizontal(float& timeCollided) { float shortestTime = FLT_MAX; Math::cVector intersectionPoint; Math::cVector normalAtIntersection; Math::cVector parallelStart = gameObjects[0]->camObject->position; /*Math::cVector parallelEnd = gameObjects[0]->camObject->position + gameObjects[0]->camObject->velocity + gameObjects[0]->camObject->acceleration * timeLeftInFrame * timeLeftInFrame * 0.5f;*/ Math::cVector parallelEnd = gameObjects[0]->camObject->position +10.0f; bool intersected = false; float shortestTimeReal = FLT_MAX; for (int i = 0; i < context.numberOfIndices; i += 3) { int index = context.indexData[i]; int index1 = context.indexData[i + 1]; int index2 = context.indexData[i + 2]; eae6320::Math::cVector vertexA = eae6320::Math::cVector(context.vertexData[index].x, context.vertexData[index].y, context.vertexData[index].z); eae6320::Math::cVector vertexB = eae6320::Math::cVector(context.vertexData[index1].x, context.vertexData[index1].y, context.vertexData[index1].z); eae6320::Math::cVector vertexC = eae6320::Math::cVector(context.vertexData[index2].x, context.vertexData[index2].y, context.vertexData[index2].z); eae6320::Math::cVector normal = eae6320::Math::cVector(context.vertexData[i].nx, context.vertexData[i].ny, context.vertexData[i].nz); float u = 0, v = 0, w = 0, t = 0; int a = IntersectSegmentTriangle(parallelStart, parallelEnd, vertexA, vertexC, vertexB, u, v, w, t,normal); //normal = eae6320::Math::cVector(context.vertexData[i].nx, context.vertexData[i].ny, context.vertexData[i].nz); if (a == 1) { if (t < shortestTime) { shortestTime = t; //intersectionPoint = vertexA*u + vertexB*v + vertexC*w; intersectionPoint = parallelStart + (parallelEnd - parallelStart) * t; //normalAtIntersection = normal * -1.0f; shortestTimeReal = (intersectionPoint - parallelStart).GetLength() / (parallelStart - parallelEnd).GetLength(); intersected = true; } } } if (intersected) { float oneByNormal = 1 /( normalAtIntersection.GetLength()); Math::cVector verticalProjectionAcceleration = normalAtIntersection * eae6320::Math::Dot(gameObjects[0]->camObject->acceleration, normalAtIntersection)*oneByNormal*oneByNormal; gameObjects[0]->camObject->acceleration = gameObjects[0]->camObject->acceleration - verticalProjectionAcceleration; Math::cVector verticalProjectionVelocity = normalAtIntersection * eae6320::Math::Dot(gameObjects[0]->camObject->velocity, normalAtIntersection)*oneByNormal*oneByNormal; gameObjects[0]->camObject->velocity = gameObjects[0]->camObject->velocity - verticalProjectionVelocity; gameObjects[0]->camObject->position = intersectionPoint - normalAtIntersection.CreateNormalized(); timeCollided = shortestTimeReal; } }
void eae6320::Graphics::DoCOllisions(GameObject* player) { // COllision Detection player->m_velocity = player->m_position - s_playerPrevPos; player->m_position = s_collisionDet->CollideWithWorld(player->m_position, player->m_velocity, 50, false); player->m_velocity = Math::cVector(0, -9.8f, 0) * 5; player->m_position = s_collisionDet->CollideWithWorld(player->m_position, player->m_velocity, 20, false); s_playerPrevPos = player->m_position; Math::cVector playerDir = player->m_position - s_camera->m_position; Math::cMatrix_transformation mat = Math::cMatrix_transformation::cMatrix_transformation(player->m_orientation, player->m_position); Math::cVector offset = Math::cMatrix_transformation::matrixMulVector(mat, Math::cVector(0, 120, 200)); Math::cVector rayOrigin = offset; s_collisionDet->CollideWithWorld(rayOrigin, playerDir, playerDir.GetLength(), true); }