static public void InitializeGeometry() { //dlaczego musze tutaj dac ball2.obj, a przy ball.obj dostaje gigantyczna kulke? przeciez nawet nie ma ball.obj var F = new WavefrontLoader.WaveFrontObjFile("ball2.obj"); ClothGeometry = F.Geometry; Plane = CGeometry.Plane(device); Boxes = new List <CGeometry>(); BoxMatrices = new Dictionary <Actor, Matrix>(); Balls = new List <CGeometry>(); BallMatrices = new Dictionary <Actor, Matrix>(); Camera = new SDXCamera(); phi = 0; theta = 0; radius = Plane.BSphere.Radius * 3; Camera.SetPositionSpherical(phi, theta, radius); Camera.Target = Plane.BSphere.Center + new Vector3(0.0f, Plane.BSphere.Radius, 0.0f); Camera.Up = new Vector3(0, 1, 0); Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)MainWindow.ClientSize.Width / (float)MainWindow.ClientSize.Height, 0.01f, Plane.BSphere.Radius * 300); //moja kamera myCamera = new Camera(new Vector3(25.0f, 25f, 25f), new Vector3(0.0f, 0.0f, 1.0f), MainWindow.ClientSize.Width, MainWindow.ClientSize.Height, (float)Math.PI / 3); Projection = myCamera.ProjectionMat; LightPosition = new Vector3(Plane.BSphere.Radius * 2, 0.3f, 0.5f); }
static public void InitializeGeometry() { /* * CDocument doc = new CDocument(device, "box.dae"); * M = doc.Geometries["Box001"]; */ CDocument doc = new CDocument(device, "plane.dae"); M = doc.Geometries["Plane001"]; CDocument docL = new CDocument(device, "ball.dae"); L = docL.Geometries["GeoSphere001"]; Camera = new SDXCamera(); phi = 0; theta = 0; radius = M.BSphere.Radius * 3; Camera.SetPositionSpherical(phi, theta, radius); Camera.Target = M.BSphere.Center; Camera.Up = new Vector3(0, 1, 0); Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)MainWindow.ClientSize.Width / (float)MainWindow.ClientSize.Height, 0.01f, M.BSphere.Radius * 10); //LightPosition = new Vector3(M.BSphere.Radius * 2, 0, 0); LightPosition = new Vector3(M.BSphere.Radius * 2, 0, M.BSphere.Radius * 2); }
public static void InitializeBeginningActors() { Random R = new Random(); //jak zrobic zeby to bylo wieksze? Actor B; int N = howManyBeginningActors; for (int i = 0; i < howManyBeginningActors; i++) { //new Vector3(i * R.Next(0,20),10 + (i+1)*R.Next(0, 20), (R.Next(0,20)-10 )* i // new Vector3((float)i / N * 180 + R.Next(10, 10 + 180 / N) - 100, R.Next(i / N * 90, 90), (float)i / N * 180 + R.Next(10, 10 + 180 / N) - 100).ToPhx()); B = Engine.AddBox(scale.X * 0.5f, scale.Y * 0.5f, scale.Z * 0.5f, R.Next(1, MaxMass), new Vector3(R.Next(-90, 90), R.Next(0, 90), R.Next(-90, 90)).ToPhx()); //do debugowania numeru backbuffer.DebugName = "BOX " + Boxes.Count.ToString(); Matrix ActorGlobal = B.GlobalPose.As <Matrix>(); ActorGlobal.M11 *= scale.X; ActorGlobal.M22 *= scale.Y; ActorGlobal.M33 *= scale.Z; Boxes.Add(CGeometry.Box(device)); BoxMatrices.Add(B, ActorGlobal); } //dosyc nieciekawie to wyglada ale w tym miejscu powinnien byc przynajmniej jeden aktor //proba dodania lokalnej sily Engine.Actors[0].AddForceAtPosition((new Vector3(0, R.Next(10, 20), R.Next(10, 20)).ToPhx()), (new Vector3(0, 25, 0).ToPhx()), ForceMode.Acceleration); }
static public void AddBall() { Random R = new Random(); Actor B = Engine.AddBall(1, R.Next(1, 10), new Vector3(0, R.Next(10, 50), 0).ToPhx()); Balls.Add(CGeometry.Ball(device)); BallMatrices.Add(B, B.GlobalPose.As <Matrix>()); }
static public bool detectCollisionsByVertex(CGeometry First, Matrix firstMatrix, CGeometry Second, Matrix secondMatrix) { DataStream FirstStream = null; bool isCollision = false; List <CIntersection> Intersections = new List <CIntersection>(); Vector3 VertexOrig; Vector3 RayDirection = new Vector3(1, 1, 1); RayDirection.Normalize(); Matrix inverseFirstMatrix = firstMatrix; Matrix inverseSecondMatrix = secondMatrix; inverseFirstMatrix.Invert(); inverseSecondMatrix.Invert(); FirstStream = First.VertexStream; FirstStream.Position = 0; //po kolei z kazdego wierzcholka figury 1szej wysyla promien do RayDirection. Sprawdza czy //liczba przeciec z figura 2ga jest parzysta. Jezeli nie to znaczy ze wierzcholek znajduje //sie w drugiej figurze for (int i = 0; i < First.VertexCount; i++) { FirstStream.Position = i * First.VertexSizeInBytes; VertexOrig = FirstStream.Read <Vector3>(); Vector4 VertexOrigTemp = Vector3.Transform(VertexOrig, firstMatrix); //poprzednia, zla wersja //Vector4 VertexOrigTemp = Vector3.Transform(VertexOrig, Matrix.Transpose(inverseFirstMatrix)); //VertexOrigTemp = Vector4.Transform(VertexOrigTemp, secondMatrix); VertexOrig.X = VertexOrigTemp.X; VertexOrig.Y = VertexOrigTemp.Y; VertexOrig.Z = VertexOrigTemp.Z; Second.Intersect(new SlimDX.Ray(VertexOrig, RayDirection), out Intersections, secondMatrix); //do kolejnego zadania //AllRayIntersections.Add(Intersections); if (Intersections.Count % 2 != 0) { isCollision = true; return(isCollision); } } return(isCollision); }
static public bool detectCollisionsBySphere(CGeometry First, Matrix firstMatrix, CGeometry Second, Matrix secondMatrix) { bool isCollision = true; Vector3 scale = new Vector3(); Quaternion rotation = new Quaternion(); Vector3 translation = new Vector3(); float radiusSum = 0; firstMatrix.Decompose(out scale, out rotation, out translation); Vector3 Position1 = First.BSphere.Center + translation; radiusSum += First.BSphere.Radius * scale.X; //w przypadku gdy skala nie jest liniowa to powinno byc inaczej secondMatrix.Decompose(out scale, out rotation, out translation); Vector3 Position2 = Second.BSphere.Center + translation; radiusSum += Second.BSphere.Radius * scale.X; //w przypadku gdy skala nie jest liniowa to powinno byc inaczej float distance = Math.Abs(Vector3.Distance(Position1, Position2)); return(isCollision = distance > radiusSum ? false : true); }
static void CalculateTransposition(int index, float xChange, float yChange, float zChange, Actor MatrixKey, Matrix objectMatrix) { //wylacz aktora i przenies go do innej grupy Engine.Actors[index].Group = 2; //Engine.Actors[index].ActorFlags; Engine.Actors[index].Sleep(); //tutaj powaznie sie zastanowic czy to jest dobrze ; JEST DOBRZE ! Vector4 cameraTransposition = myCamera.VectorInCameraMatrix(new Vector3(xChange, -yChange, zChange)); Engine.Actors[index].GlobalPosition += new Vector3(cameraTransposition.X, cameraTransposition.Y, cameraTransposition.Z).ToPhx(); BoxMatrices[MatrixKey].set_Rows(3, objectMatrix.get_Rows(3) + cameraTransposition); int i = 0; CGeometry movingObject = Boxes[index - 7]; foreach (var M in BoxMatrices) { if (i != index - 7) { Matrix secondMat = M.Value; secondMat = (i < howManyBeginningActors) ? ScaleMatrixNoTranslation(secondMat, scale) : secondMat; if (detectCollisionsBySphere(movingObject, objectMatrix, Boxes[i], secondMat)) { if (detectCollisionsByVertex(movingObject, objectMatrix, Boxes[i], secondMat)) { //obudz na bardzo krotka chwile Engine.Actors[index].Group = 0; Engine.Actors[index].WakeUp(); } } } i++; } }
static public void MainWindow_KeyDown(object sender, KeyEventArgs e) { Random R = new Random(); Vector3 moveVector = new Vector3(0, 0, 0); switch (e.KeyCode) { case Keys.D: radius += deltar; moveVector.X += step; break; case Keys.A: radius -= deltar; moveVector.X -= step; break; case Keys.Space: phi -= deltaang; moveVector.Y += step; break; case Keys.ControlKey: phi += deltaang; moveVector.Y -= step; break; case Keys.W: theta -= deltaang; moveVector.Z += step; break; case Keys.S: theta += deltaang; moveVector.Z -= step; break; case Keys.F: cameraEnabled = false; break; case Keys.ShiftKey: step = (step == 0.6f) ? 2.2f : 0.6f; break; case Keys.C: Actor B1 = Engine.AddBox(0.5f, 0.5f, 0.5f, 5, new Vector3(0, 50, -1).ToPhx()); Actor B2 = Engine.AddBox(0.5f, 0.5f, 0.5f, 5, new Vector3(0, 50, 1).ToPhx()); Engine.AddJointActors(B1, B2, new Vector3(0, 50, -1).ToPhx(), new Vector3(0, 50, 1).ToPhx()); Boxes.Add(CGeometry.Box(device)); Boxes.Add(CGeometry.Box(device)); BoxMatrices.Add(B1, B1.GlobalPose.As <Matrix>()); BoxMatrices.Add(B2, B2.GlobalPose.As <Matrix>()); break; case Keys.X: Cloth C = Engine.AddCloth(ClothGeometry, new Vector3(0, 50, 0).ToPhx()); break; case Keys.B: { Actor B = Engine.AddBox(0.5f, 0.5f, 0.5f, R.Next(1, 10), new Vector3(0, R.Next(10, 50), 0).ToPhx()); backbuffer.DebugName = "BOX " + Boxes.Count.ToString(); Boxes.Add(CGeometry.Box(device)); BoxMatrices.Add(B, B.GlobalPose.As <Matrix>()); } break; case Keys.N: { Actor B = Engine.AddBall(1, R.Next(1, 10), new Vector3(0, R.Next(10, 50), 0).ToPhx()); Balls.Add(CGeometry.Ball(device)); BallMatrices.Add(B, B.GlobalPose.As <Matrix>()); } break; case Keys.U: { foreach (Actor A in Engine.Actors) { if (A.IsDynamic) { A.AddForce(new Vector3(0, R.Next(1, 10), 0).ToPhx(), ForceMode.Impulse); } } } break; case Keys.J: { foreach (Actor A in Engine.Actors) { if (A.IsDynamic) { A.AddForce(new Vector3(0, -R.Next(1, 10), 0).ToPhx(), ForceMode.Impulse); } } } break; case Keys.I: { foreach (Actor A in Engine.Actors) { if (A.IsDynamic) { A.AddForce(new Vector3(0, 0, R.Next(1, 10)).ToPhx(), ForceMode.Impulse); } } } break; case Keys.O: { foreach (Actor A in Engine.Actors) { if (A.IsDynamic) { A.AddForce(new Vector3(R.Next(1, 10), 0, 0).ToPhx(), ForceMode.Impulse); } } } break; case Keys.L: { foreach (Actor A in Engine.Actors) { if (A.IsDynamic) { A.AddForce(new Vector3(-R.Next(1, 10), 0, 0).ToPhx(), ForceMode.Impulse); } } } break; case Keys.K: { foreach (Actor A in Engine.Actors) { if (A.IsDynamic) { A.AddForce(new Vector3(0, 0, -R.Next(1, 10)).ToPhx(), ForceMode.Impulse); } } } break; case Keys.R: { List <Actor> Actors = new List <Actor>(); foreach (var m in BoxMatrices) { Actors.Add(m.Key); } foreach (var m in BallMatrices) { Actors.Add(m.Key); } for (int i = 0; i < Actors.Count; i++) { Actors[i].Dispose(); } Boxes.Clear(); BoxMatrices.Clear(); Balls.Clear(); BallMatrices.Clear(); List <Cloth> Clothes = new List <Cloth>(); foreach (var c in Engine.Scene.Cloths) { Clothes.Add(c); } foreach (var c in Clothes) { c.Dispose(); } List <Joint> Joints = new List <Joint>(); foreach (var c in Engine.Scene.Joints) { Joints.Add(c); } foreach (var c in Joints) { c.Dispose(); } } break; } Camera.SetPositionSpherical(phi, theta, radius); Matrix V = Camera.CameraView; Matrix VI = Matrix.Invert(V); myCamera.AddToCameraPosition(moveVector); V = myCamera.View; VI = Matrix.Invert(V); effect.GetVariableByName("mWVP").AsMatrix().SetMatrix(V * Projection); effect.GetVariableByName("mVI").AsMatrix().SetMatrix(VI); }