Dvoid Camera::ViewMatrixCalculate() { Matrix33 rotate; rotate.SetColumns(m_vDir[CD_RIGHT], m_vDir[CD_UP], -m_vDir[CD_FRONT]); rotate.Transpose(); m_matCam[CM_VIEW].Rotation(rotate); // set translation (rotate into view space) Vector3 invEye = -(rotate* m_vPos); m_matCam[CM_VIEW](0, 3) = invEye.x; m_matCam[CM_VIEW](1, 3) = invEye.y; m_matCam[CM_VIEW](2, 3) = invEye.z; }
//world -> view Matrix44 TransformHelper::CreateView(const Vector3& eye, const Vector3& lookAt, const Vector3& up) { // compute view vectors Vector3 view = lookAt - eye; Vector3 right; Vector3 viewUp; view.Normalize(); right = view.Cross(up); right.Normalize(); viewUp = right.Cross(view); viewUp.Normalize(); // now set up matrices // base rotation matrix Matrix33 rotate; rotate.SetColumns(right, viewUp, -view); // view->world transform // set rotation //Matrix44 mViewToWorldMatrix; //mViewToWorldMatrix.Rotation(rotate); //// set translation (eye position) //mViewToWorldMatrix(0, 3) = eye.x; //mViewToWorldMatrix(1, 3) = eye.y; //mViewToWorldMatrix(2, 3) = eye.z; // world->view transform // set rotation Matrix44 mWorldToViewMatrix; rotate.Transpose(); mWorldToViewMatrix.Rotation(rotate); // set translation (rotate into view space) Vector3 invEye = -(rotate*eye); mWorldToViewMatrix(0, 3) = invEye.x; mWorldToViewMatrix(1, 3) = invEye.y; mWorldToViewMatrix(2, 3) = invEye.z; return mWorldToViewMatrix; }
void HitSpinnerWall( Spinner* sp, Plane &plane, Ball* b ) { Vector3 wall = plane.points[2] - plane.points[0]; Vector3 ballFromWall = b->Position() - plane.points[0]; float relativePosition = plane.normal * ballFromWall; float relativeDirection = plane.normal * b->Velocity(); if ((relativePosition > 0 && relativeDirection < 0) || (relativePosition < 0 && relativeDirection > 0)) { float theta, c, s; theta = acos((wall * Vector3(1,0,0))/wall.getLength()); if (b->position.x<0) { theta *= -1; } c = cos(theta), s = sin(theta); Vector3 position2 = Vector3(); Vector3 velocityRot = Vector3(); position2 = Vector3(c * ballFromWall.x + s * ballFromWall.y, c * ballFromWall.y - s * ballFromWall.x, 0); velocityRot = Vector3( c * b->Velocity().x + s * b->Velocity().y, c * b->Velocity().y - s * b->Velocity().x, 0); // Set the ball outside the wall, reverse velocity toward wall Vector3 scaledNormal = b->radius * Vector3(0,-1,0); position2.y = scaledNormal.y; velocityRot.y *= -1; // Rotate back ballFromWall = Vector3(c * position2.x - s * position2.y,c * position2.y + s * position2.x,0); b->Velocity(Vector3(c * velocityRot.x - s * velocityRot.y,c * velocityRot.y + s * velocityRot.x,0)); Point3 newPos = plane.points[0] + ballFromWall; b->position = newPos; //float bEnergy = .5 * b->Mass() * b->Velocity() * b->Velocity(); Vector3 momenteum = b->Mass() * b->Velocity(); Point3 closest = plane.ClosestPointToPoint(b->Position()); //Vector3 tempV = diff; //tempV.normalize(); //Vector3 proj = (closest*tempV)*tempV; //float temp = proj.getLength(); //// check if in the bounding box //if(diff.getLength() > temp + b->radius) //{ // continue; //} //// then it needs to go, "Hey!". ////find the force applied to the bar // ft = dp/dt = m*dv/dt ////find perpendicular momentum //Vector3 totalForce = bEnergy * b->Velocity(); //// t = r X F //Vector3 torque = diff.cross(totalForce); Vector3 radius = Point3(closest.x,closest.y,closest.z) - sp->Position(); Vector3 torque = radius.cross(momenteum); Vector3 dL = .05 * torque; //// find the rotation matrix and the inverse of the rotation matrix Matrix33 myRotation = Matrix33(sp->Rotation()); Matrix33 myRotationT = myRotation.Transpose(); sp->MyL(sp->MyL() + dL); ////w(i+1) = q(i+1) * I(-1) * qT(i+1) * L(i+1) sp->AngularVel(myRotation * *sp->Tensor()->inverse() * myRotationT * sp->MyL()); ////q(n+1) = q(n) + h(.5 * w(n)*q(n)) sp->Rotation(sp->Rotation() + .05f*(.5 * Quaternion(sp->AngularVel()) * sp->Rotation())); Quaternion q = sp->Rotation(); q.normalize(); sp->Rotation(q); } }