/// <summary> /// Uaktualnia kierunek/predkosc na podstawie danych z myszki /// </summary> /// <param name="dt">Czas od ostatniej klatki</param> /// <param name="dx">obrot w kierunku x</param> /// <param name="dy">obrot w kierunku y</param> /// <param name="dz">obort w kierunku z</param> /// <param name="strafeX">Strafe x</param> /// <param name="strafeY">Strade y</param> private void Update(float dt, float dx, float dy, float strafeSpeedX, float strafeSpeedY) { //xDeltaAvg.Add(dx); //yDeltaAvg.Add(dy); //GM.GeneralLog.Write("xDelta:" + dx.ToString() + " yDelta:" + dy.ToString() + " dt:" + dt.ToString()); float rotX = dy * dt; float rotY = dx * dt; angleX += rotX; angleY += rotY; if (angleX > 0.9 * Math.PI / 2) { angleX = (float)(0.9 * Math.PI / 2); } if (angleX < -0.9 * Math.PI / 2) { angleX = (float)(-0.9 * Math.PI / 2); } MyQuaternion qY = MyQuaternion.FromAxisAngle(-angleY, initialUp); MyQuaternion qX = MyQuaternion.FromAxisAngle(-angleX, initialRight); //player.CurrentCharacter.Orientation = initialOrientation * (qY); //orientation zostanie zmienione przez fizyke, jeœli ustawie odpowiednie AngularVelocity if (dt > 0) { //float currentRotY = player.CurrentCharacter.Orientation.EulerAngles().Z/2; ////currentRotY *= (float)Math.PI / 180f; //player.CurrentCharacter.AngularVelocity = upVector * ((currentRotY-angleY) / 0.03f); //player.CurrentCharacter.AngularVelocity = upVector * ((-rotY) / 0.03f); } lookVector = initialLook; rightVector = initialRight; lookVector.Rotate(qX); lookVector.Rotate(qY); rightVector.Rotate(qY); lookVector.Normalize(); // character.LookOrientation = initialOrientation * (qY * qX); if (updateDirections) { character.LookOrientation = initialOrientation * (qY * qX); character.WalkOrientation = initialOrientation * qY; } //MyVector zVelocity = lookVector * (ship.Velocity.Dot(lookVector)); ////MyVector xVelocity = rightVector * (ship.Velocity.Dot(rightVector)); ////MyVector yVelocity = upVector * (ship.Velocity.Dot(upVector)); //MyVector xVelocity = rightVector * strafeSpeedX * dt; //MyVector yVelocity = upVector * strafeSpeedY * dt; //MyVector tVelocity = ship.Velocity - zVelocity-xVelocity-yVelocity; //this.ship.Forces += Drag(tVelocity); ////this.ship.Velocity -= tVelocity; //if (zVelocity.Length > SetVelocity) //{ // this.ship.Velocity -= lookVector * Math.Min(zVelocity.Length - SetVelocity, dt * objectParameters.Acceleration); //} //else if (zVelocity.Length < SetVelocity) //{ // this.ship.Velocity += lookVector * Math.Min(SetVelocity - zVelocity.Length, dt * objectParameters.Acceleration); ; //} //if (xVelocity.Length > strafeSpeedX) //{ //} if (updateDirections) { if (Flying) { this.character.Velocity = SetVelocity * (lookVector) + strafeSpeedX * rightVector; } else { float yVelocity = character.Velocity.Dot(upVector); this.character.Velocity = yVelocity * upVector + strafeSpeedX * rightVector - SetVelocity * (rightVector ^ upVector); } } }
public bool IntersectMesh(out MyVector intersectionPoint, out MyVector intersectionNormal, out float collisionTime, MyVector startPoint, MyVector endPoint, float dt, CollisionMesh mesh) { intersectionPoint = new MyVector(); intersectionNormal = new MyVector(); MyVector translate = (endPoint - startPoint); collisionTime = 0; int nPoints = 0; MyVector rVelocity=new MyVector(); MyVector rMove = endPoint - startPoint; MyVector iPoint; MyVector n1, n2, n3; float dist, speed, d1, d2, d3; for (int v = 0; v < mesh.m_hardPoints.Length; v++) { MyVector sPoint = startPoint + mesh.m_hardPoints[v]; MyVector axis = mesh.MeshRotation; axis.Normalize(); MyQuaternion rot = MyQuaternion.FromAxisAngle((dt * mesh.MeshRotation).Length, axis); //endPoint.Rotate(rot); //endPoint += relativeVelocity * dt; MyVector tmp=mesh.m_hardPoints[v]; tmp.Rotate(rot); MyVector ePoint = startPoint + tmp + rMove; rVelocity = (ePoint - sPoint) / dt; translate = (ePoint - sPoint); for (int i = 0; i < m_faces.Length; i++) { if ((ePoint - m_vertices[m_faces[i].v1]) * m_faces[i].n > 0) { //the line doesn't cross the triangle continue; } dist = m_faces[i].n * (sPoint - m_vertices[m_faces[i].v1]); speed = -dist / (m_faces[i].n * rVelocity); if (speed < 0) { if (translate * (m_faces[i].n)<0) { if (dist < -0.5f) continue; } else continue; } iPoint = sPoint + speed * rVelocity; //3 planes around triangle //plane1 n1 = ((m_vertices[m_faces[i].v2] - m_vertices[m_faces[i].v1]) ^ m_faces[i].n).Normalize(); d1 = n1 * (m_vertices[m_faces[i].v1]); //plane2 n2 = ((m_vertices[m_faces[i].v3] - m_vertices[m_faces[i].v2]) ^ m_faces[i].n).Normalize(); d2 = n2 * (m_vertices[m_faces[i].v2]); //plane3 n3 = ((m_vertices[m_faces[i].v1] - m_vertices[m_faces[i].v3]) ^ m_faces[i].n).Normalize(); d3 = n3 * (m_vertices[m_faces[i].v3]); float x1 = n1 * iPoint - d1; float x2 = n2 * iPoint - d2; float x3 = n3 * iPoint - d3; if (x1 <= 0 && x2 <= 0 && x3 <= 0) { if (nPoints == 0) { nPoints = 1; intersectionPoint = iPoint; intersectionNormal = m_faces[i].n; collisionTime = speed; } else if (speed < collisionTime) { nPoints = 1; intersectionPoint = iPoint; intersectionNormal = m_faces[i].n; collisionTime = speed; } else if (speed == collisionTime && nPoints > 0) { nPoints++; intersectionPoint.Add(iPoint); intersectionNormal.Add(m_faces[i].n); } } } } //second //for (int v = 0; v < m_hardPoints.Length; v++) //{ // //MyVector sPoint = -startPoint + m_hardPoints[v]; // //MyVector ePoint = -endPoint + m_hardPoints[v]; // MyVector sPoint = -startPoint + m_hardPoints[v]; // MyVector axis = -mesh.MeshRotation; // axis.Normalize(); // MyQuaternion rot = MyQuaternion.FromAxisAngle((dt * mesh.MeshRotation).Length, axis); // //endPoint.Rotate(rot); // //endPoint += relativeVelocity * dt; // MyVector tmp = m_hardPoints[v]; // tmp.Rotate(rot); // MyVector ePoint = -startPoint + tmp - rMove; // rVelocity = (ePoint - sPoint) / dt; // translate = (ePoint - sPoint); // for (int i = 0; i < mesh.m_faces.Length; i++) // { // if ((ePoint - mesh.m_vertices[mesh.m_faces[i].v1]) * mesh.m_faces[i].n > 0) // { // //the line doesn't cross the triangle // continue; // } // dist = mesh.m_faces[i].n * (sPoint - mesh.m_vertices[mesh.m_faces[i].v1]); // speed = -dist / (mesh.m_faces[i].n * (-rVelocity)); // if (speed < 0) // { // if (translate * (mesh.m_faces[i].n) < 0) // { // if (speed < -0.04) // continue; // } // else // continue; // } // iPoint = sPoint + speed * (-rVelocity); // //3 planes around triangle // //plane1 // n1 = ((mesh.m_vertices[mesh.m_faces[i].v2] - mesh.m_vertices[mesh.m_faces[i].v1]) ^ mesh.m_faces[i].n).Normalize(); // d1 = n1 * (mesh.m_vertices[mesh.m_faces[i].v1]); // //plane2 // n2 = ((mesh.m_vertices[mesh.m_faces[i].v3] - mesh.m_vertices[mesh.m_faces[i].v2]) ^ mesh.m_faces[i].n).Normalize(); // d2 = n2 * (mesh.m_vertices[mesh.m_faces[i].v2]); // //plane3 // n3 = ((mesh.m_vertices[mesh.m_faces[i].v1] - mesh.m_vertices[mesh.m_faces[i].v3]) ^ mesh.m_faces[i].n).Normalize(); // d3 = n3 * (mesh.m_vertices[mesh.m_faces[i].v3]); // float x1 = n1 * iPoint - d1; // float x2 = n2 * iPoint - d2; // float x3 = n3 * iPoint - d3; // if (x1 <= 0 && x2 <= 0 && x3 <= 0) // { // if (nPoints == 0) // { // nPoints = 1; // intersectionPoint = iPoint + startPoint; // intersectionNormal = -mesh.m_faces[i].n; // collisionTime = speed; // } // else if (speed < collisionTime) // { // nPoints = 1; // intersectionPoint = iPoint + startPoint; // intersectionNormal = -mesh.m_faces[i].n; // collisionTime = speed; // } // else if (speed == collisionTime && nPoints > 0) // { // nPoints++; // intersectionPoint.Add(iPoint + startPoint); // intersectionNormal.Add(-mesh.m_faces[i].n); // } // } // } //} if (nPoints > 1) { intersectionPoint.Divide(nPoints); intersectionNormal.Normalize(); } return (nPoints > 0); }
public bool IntersectSphere(out MyVector intersectionPoint, out MyVector intersectionNormal, out float collisionTime, MyVector startPoint, MyVector endPoint, float dt, CollisionSphere sphere) { intersectionPoint = new MyVector(); intersectionNormal = new MyVector(); collisionTime = 0; MyVector rVelocity = (endPoint - startPoint) / dt; MyVector translate = (endPoint - startPoint); int nPoints = 0; //bool status = false; MyVector iPoint; MyVector n1, n2, n3; MyVector sPoint; MyVector ePoint; float dist, speed, d1, d2, d3; // check for face collision for (int i = 0; i < m_faces.Length; i++) { sPoint = startPoint - m_faces[i].n * sphere.Radius; ePoint = endPoint - m_faces[i].n * sphere.Radius; if ((ePoint - m_vertices[m_faces[i].v1]) * m_faces[i].n > 0) { //the line doesn't cross the triangle continue; } dist = m_faces[i].n * (sPoint - m_vertices[m_faces[i].v1]); speed = -dist / (m_faces[i].n * rVelocity); if (speed < 0) { if (translate * (m_faces[i].n)<0) { if (dist < -0.5f) continue; } else continue; } if (nPoints > 0 && speed > collisionTime) continue; iPoint = sPoint + speed * rVelocity; //3 planes around triangle //plane1 n1 = ((m_vertices[m_faces[i].v2] - m_vertices[m_faces[i].v1]) ^ m_faces[i].n).Normalize(); d1 = n1 * (m_vertices[m_faces[i].v1]); //plane2 n2 = ((m_vertices[m_faces[i].v3] - m_vertices[m_faces[i].v2]) ^ m_faces[i].n).Normalize(); d2 = n2 * (m_vertices[m_faces[i].v2]); //plane3 n3 = ((m_vertices[m_faces[i].v1] - m_vertices[m_faces[i].v3]) ^ m_faces[i].n).Normalize(); d3 = n3 * (m_vertices[m_faces[i].v3]); float x1 = n1 * iPoint - d1; float x2 = n2 * iPoint - d2; float x3 = n3 * iPoint - d3; if (x1 <= 0 && x2 <= 0 && x3 <= 0) { //if (status == false) //{ // status = true; // intersectionPoint = iPoint; // intersectionNormal = m_faces[i].n; // collisionTime = speed; //} //else if (speed < collisionTime) //{ // intersectionPoint = iPoint; // intersectionNormal = m_faces[i].n; // collisionTime = speed; //} if (nPoints == 0) { nPoints = 1; intersectionPoint = iPoint; intersectionNormal = m_faces[i].n; collisionTime = speed; } else if (speed < collisionTime) { nPoints = 1; intersectionPoint = iPoint; intersectionNormal = m_faces[i].n; collisionTime = speed; } else if (speed == collisionTime && nPoints > 0) { nPoints++; intersectionPoint.Add(iPoint); intersectionNormal.Add(m_faces[i].n); } } } //if (nPoints > 1) //{ // intersectionPoint.Divide(nPoints); // intersectionNormal.Divide(nPoints); //} //if (status == true) //{ // return true; //} //status = false; // no face collision //check for edge collision foreach (CEdge e in m_edges) { MyVector DV, DVP; DV = m_vertices[e.v2] - m_vertices[e.v1]; DVP = m_vertices[e.v1] - startPoint; double a = (double)DV.LengthSq * (double)rVelocity.LengthSq - (double)(DV * rVelocity) * (double)(DV * rVelocity); double b = -2 * (double)((double)DV.LengthSq * (double)(DVP * rVelocity) - (double)(DVP * DV) * (double)(rVelocity * DV)); double c = (double)DV.LengthSq * (double)DVP.LengthSq - (double)(DV * DVP) * (double)(DV * DVP) - (double)(sphere.Radius * sphere.Radius) * (double)DV.LengthSq; double delta = b * b - 4 * a * c; if (delta >= 0) { double t; if (a > 0) { t = (-b - Math.Sqrt(delta)) / (2 * a); } else { t = (-b + Math.Sqrt(delta)) / (2 * a); } if (t < -0.04) continue; if (nPoints > 0 && t > collisionTime) continue; iPoint = startPoint + (float)t * rVelocity; iPoint.Add((DV ^ (DVP ^ DV)).Normalize() * sphere.Radius); if ((iPoint - m_vertices[e.v1]) * DV >= 0 && (iPoint - m_vertices[e.v2]) * DV <= 0) { //intersectionPoint = iPoint; //intersectionNormal = startPoint - intersectionPoint; //intersectionNormal.Normalize(); //status = true; //collisionTime = t; if (nPoints == 0) { nPoints = 1; intersectionPoint = iPoint; intersectionNormal = startPoint - intersectionPoint; intersectionNormal.Normalize(); collisionTime = (float)t; } else if (t < collisionTime) { nPoints = 1; intersectionPoint = iPoint; intersectionNormal = startPoint - intersectionPoint; intersectionNormal.Normalize(); collisionTime = (float)t; } else if (t == collisionTime && nPoints > 0) { nPoints++; intersectionPoint.Add(iPoint); intersectionNormal.Add((startPoint - intersectionPoint).Normalize()); } } } } //if (status == true) // return true; //status = false; //no edge collision //check for vertex collision for (int i = 0; i < m_vertices.Length; i++) { MyVector DVP; DVP = m_vertices[i] - startPoint; double a = rVelocity.LengthSq; double b = -2 * (DVP * rVelocity); double c = DVP.LengthSq - (sphere.Radius * sphere.Radius); double delta = b * b - 4 * a * c; if (delta >= 0) { double t; if (a > 0) { t = (-b - Math.Sqrt(delta)) / (2 * a); } else { t = (-b + Math.Sqrt(delta)) / (2 * a); } if (t < -0.04) continue; if (nPoints > 0 && t > collisionTime) continue; //if (status == false || (status == true && collisionTime > t)) //{ // intersectionPoint = m_vertices[i]; // intersectionNormal = startPoint - intersectionPoint; // intersectionNormal.Normalize(); // status = true; // collisionTime = (float)t; //} if (nPoints == 0) { nPoints = 1; intersectionPoint = m_vertices[i]; intersectionNormal = startPoint - intersectionPoint; intersectionNormal.Normalize(); collisionTime = (float)t; } else if (t < collisionTime) { nPoints = 1; intersectionPoint = m_vertices[i]; intersectionNormal = startPoint - intersectionPoint; intersectionNormal.Normalize(); collisionTime = (float)t; } else if (t == collisionTime && nPoints > 0) { nPoints++; intersectionPoint.Add(m_vertices[i]); intersectionNormal.Add((startPoint - intersectionPoint).Normalize()); } } } if (nPoints > 1) { intersectionPoint.Divide(nPoints); intersectionNormal.Normalize(); } return (nPoints > 0); }
static void Main() { using (Framework.GM gmanager = new Framework.GM()) { using (Framework.FrameworkWindow form = new Framework.FrameworkWindow(true, AppConfig.WindowWidth, AppConfig.WindowHeight)) { form.Text = "CastleButcher"; ///All resources that are allocated during OnCreate/OnResetDevice should be created ///before InitializeGraphics(), so that when the device is created those resources ///can be properly allocated. Creating resources after InitializeGraphics() can lead to ///crashes because of unallocated resources(After InitializeGraphics() OnCreateDevice may not ///be called). MyVector up = new MyVector(0, 1, 0); MyVector v = new MyVector(-21, -0.3f, 1.2f); MyVector v2 = new MyVector(v.X, 0, v.Z); v.Normalize(); v2.Normalize(); MyVector right = v ^ up; right.Normalize(); Matrix rot=Matrix.LookAtRH(new Vector3(0, 0, 0), new Vector3(v.X,v.Y,v.Z), new Vector3(0, 1, 0)); rot.Invert(); Matrix rot2=Matrix.LookAtRH(new Vector3(0, 0, 0), new Vector3(v2.X,v2.Y,v2.Z), new Vector3(0, 1, 0)); rot2.Invert(); Quaternion q = Quaternion.RotationMatrix(rot); MyQuaternion lookOrientation = (MyQuaternion)q; Quaternion q2 = Quaternion.RotationMatrix(rot2); MyQuaternion walkOrientation = (MyQuaternion)q2; MyVector vp = new MyVector(0, 0, -1); vp.Rotate(lookOrientation); MyVector v2p = new MyVector(0, 0, -1); v2p.Rotate(walkOrientation); //add guistyles/fonts GM.GUIStyleManager.AddStyle(DefaultValues.MediaPath + "mystyle.xml"); GM.GUIStyleManager.AddStyle(DefaultValues.MediaPath + "PlayerInfoStyle.xml"); GM.GUIStyleManager.SetCurrentStyle("mystyle"); if (!form.InitializeGraphics()) { MessageBox.Show("Unable to initialize DirectX"); form.Dispose(); } //push some layers to display form.PushLayer(new AppControl(form)); SoundSystem.SoundEngine.InitializeEngine(GM.AppWindow, 0, 0.05f, 1); SoundSystem.SoundEngine.NameFinder.MusicDirectoryPath = GameSettings.Default.MusicPath; SoundSystem.SoundEngine.NameFinder.SoundDirectoryPath = GameSettings.Default.SoundPath; //SoundSystem.SoundEngine.ListenerUpVector = new Vector3(0, 1, 0); //SoundSystem.SoundEngine.Update(new Vector3(10, -15, 12), new Vector3(0, 0, -1),new Vector3(0,1,0)); //SoundSystem.SoundEngine.PlaySound(SoundSystem.Enums.SoundTypes.fanfare1, new Vector3(10, -15, 12)); Application.Idle += new EventHandler(form.OnApplicationIdle); Application.Run(form); Properties.Settings.Default.Save(); } } }