Jump to content
  • Advertisement

mitko29

Member
  • Content Count

    11
  • Joined

  • Last visited

Community Reputation

100 Neutral

About mitko29

  • Rank
    Member
  1. Ok I convert it to c# , but in xna framework I don't see a Matrix4x4 only Matrix and the problem comes , because I can't get Matrix.M00 , because it's start from M11: void TransformMouseCoordinatesInto3D(GraphicsDevice m,Vector2 cursorPosition,Matrix projectionMatrix) { Matrix mProj = projectionMatrix; float w = (float)m.PresentationParameters.BackBufferWidth; float h = (float)m.PresentationParameters.BackBufferHeight; float X = (2 * cursorPosition.X / w - 1) * mProj.M00; float Y = (-2 * cursorPosition.Y / h + 1) * mProj.M11; projection.Position = new Microsoft.Xna.Framework.Vector3(0, 0, 0); projection.Direction = new Microsoft.Xna.Framework.Vector3(X, Y, 1); }
  2. Well thanks for explanation , but I was able to do it after I wrote my own Intersect method Now I am trying to shoot a projectile were mouse click in different angle,I apply a picture for better understanding: the red box is the enemy,the black box is the player,green dot is where mouse clicked and the brown is the projectile. So my question is how to transform the mouse click into 3D world coordinate ? How to calculate the angle and draw the projectile in that direction ?
  3. Well I build your snipped code and i find out that the above I post do the same,but I am not able to understand the c++ collision code and what is AABB ? When however the result now is this, but I still can detect collision , simply using the Intersect method :
  4. Well I upgrade the code above a little bit: protected BoundingBox CalculateBox() { Matrix worldTransform = Matrix.CreateTranslation(Vector3.Zero); Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue); ModelMesh mesh = myModel.Meshes[0]; foreach (ModelMeshPart meshPart in mesh.MeshParts) { // Vertex buffer parameters int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride; int vertexBufferSize = meshPart.NumVertices * vertexStride; // Get vertex data as float float[] vertexData = new float[vertexBufferSize / sizeof(float)]; meshPart.VertexBuffer.GetData<float>(vertexData); // Iterate through vertices (possibly) growing bounding box, all calculations are done in world space for (int i = 0; i < vertexBufferSize / sizeof(float); i += vertexStride / sizeof(float)) { Vector3 transformedPosition = Vector3.Transform(new Vector3(vertexData, vertexData[i + 1], vertexData[i + 2]), worldTransform); min = Vector3.Min(min, transformedPosition); max = Vector3.Max(max, transformedPosition); } } // Create and return bounding box return new BoundingBox(min, max); Now I get better bounding box around my model,but I am not sure how WorldTransform is changing the transformation . So I am drawing my player this way: foreach (ModelMesh mesh in myModel.Meshes) { foreach (BasicEffect effect in mesh.Effects) { if (effect is BasicEffect) { (effect).EnableDefaultLighting(); } effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(1 / mesh.BoundingSphere.Radius) * scaling * rotation * translation; effect.View = Matrix.CreateLookAt( new Microsoft.Xna.Framework.Vector3(0, 0, 3), Microsoft.Xna.Framework.Vector3.Zero, Microsoft.Xna.Framework.Vector3.Up); effect.Projection = Matrix.CreatePerspective( 1, 1 / GraphicsDevice.Viewport.AspectRatio, 1f, 10000); } mesh.Draw(); } } and I am drawing enemy this way: Matrix[] transforms = new Matrix[texture.Bones.Count]; texture.CopyAbsoluteBoneTransformsTo(transforms); foreach (ModelMesh meshh in texture.Meshes) { foreach (BasicEffect effect in meshh.Effects) { if (effect is BasicEffect) { (effect).EnableDefaultLighting(); } effect.World = transforms[meshh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation) * Matrix.CreateTranslation(position) ; effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, 5000.0f), Vector3.Zero, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); } meshh.Draw(); } and check for collision this way: for (int i = enemies.Count - 1; i >= 0; i--) { enemies.Update(gameTime); if (enemies.Sbox.Intersects(bb)) { enemies.Alive = false; } } Where enemies is List<Enemy>. The result is : The white bounding box is the player and the red is the enemy. but no matter where the player is the enemy always stop at this red line in the middle. When I debug the collision code I receive this : I hope this is enough , if you need something else just say
  5. oh, sorry for my stupidity,now I put the code inside the Update method and debuget again now the result are great: but the problem is that when enemy initialize on screen and I check for collision it's always true and the values of position.X don't change (I understand that the problem come from the collision , because when I remove the code for collision the problem disappear). and the second problem I am now seeing is that the box become to big while the model is moving maybe I have to calculate the old and the new position and then redraw the center of the box ?
  6. Hi all , I am trying to make boundingbox collision for my 3D models instead using Sphere because is more accurate, but collision is always true here,how am I drawing the box: BoundingBox sbox; private Vector3 min = new Vector3(float.MaxValue,float.MaxValue,float.MaxValue); private Vector3 max = new Vector3(float.MinValue,float.MinValue,float.MinValue); foreach (ModelMesh meshh in texture.Meshes) { foreach (ModelMeshPart meshPart in meshh.MeshParts) { int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride; int vertexBufferSize = meshPart.NumVertices * vertexStride; float[] vertexData = new float[vertexBufferSize / sizeof(float)]; meshPart.VertexBuffer.GetData<float>(vertexData); for (int i = 0; i < vertexBufferSize / sizeof(float); i += vertexStride / sizeof(float)) { Vector3 transformedPosition = Vector3.Transform(new Vector3(vertexData, vertexData[i + 1], vertexData[i + 2]), enemymat); min = Vector3.Min(min, transformedPosition); max = Vector3.Max(max, transformedPosition); } } sbox = new BoundingBox(min,max); Result: and when I debug the bounding box , here what I see:
  7. Hi guys I am trying to apply the bounding sphere on my models , but when I try to detect collision nothing happened..... Here how am I drawing and applying the bounding sphere: In enemy class : if ((texture != null) && (texture.Meshes.Count > 0)) { Matrix[] transforms = new Matrix[texture.Bones.Count]; texture.CopyAbsoluteBoneTransformsTo(transforms); foreach (ModelMesh meshh in texture.Meshes) { int a = texture.Meshes.Count; if (sphere.Radius == 0) sphere = meshh.BoundingSphere; else sphere = BoundingSphere. CreateMerged(sphere, meshh.BoundingSphere); DebugShapeRenderer.AddBoundingSphere(meshh.BoundingSphere, Microsoft.Xna.Framework.Color.Red); foreach (BasicEffect effect in meshh.Effects) { effect.EnableDefaultLighting(); enemymat = effect.World = transforms[meshh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation) * Matrix.CreateTranslation(position); effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, 5000.0f), Vector3.Zero, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); view1 = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, 5000.0f), Vector3.Zero, Vector3.Up); projection1 = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); } meshh.Draw(); DebugShapeRenderer.AddBoundingSphere(sphere, Microsoft.Xna.Framework.Color.Pink); DebugShapeRenderer.Draw(gameTime, view1, projection1); } sphere.Transform(ref enemymat,out sphere); } } and in Update method: sphere.Transform(ref enemymat, out sphere); and here how I load the enemy texture in load method: eninit = new Enemy(secondModel, position, 100, 100, 0.5f,secondModel.Meshes[0].BoundingSphere); and here how I draw the player: if ((myModel != null) && (myModel.Meshes.Count > 0) && transformation == true) { Matrix[] transforms = new Matrix[myModel.Bones.Count]; myModel.CopyAbsoluteBoneTransformsTo(transforms); Matrix rotation = Matrix.CreateFromYawPitchRoll(-yaw, -pitch, roll); Matrix translation = Matrix.CreateTranslation( modelTransformaton.V03, modelTransformaton.V13, -modelTransformaton.V23); Matrix scaling = Matrix.CreateScale(100); foreach (ModelMesh mesh in myModel.Meshes) { if (modelsp.Radius == 0) modelsp = mesh.BoundingSphere; else modelsp = BoundingSphere. CreateMerged(modelsp, mesh.BoundingSphere); DebugShapeRenderer.AddBoundingSphere(mesh.BoundingSphere, Microsoft.Xna.Framework.Color.Green); foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); world = effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(1 / mesh.BoundingSphere.Radius) * scaling * rotation * translation; effect.View = Matrix.CreateLookAt( new Microsoft.Xna.Framework.Vector3(0, 0, 3), Microsoft.Xna.Framework.Vector3.Zero, Microsoft.Xna.Framework.Vector3.Up); effect.Projection = Matrix.CreatePerspective( 1, 1 / GraphicsDevice.Viewport.AspectRatio, 1f, 10000); view1 = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(1 / mesh.BoundingSphere.Radius) * scaling * rotation * translation; projection1 = Matrix.CreatePerspective( 1, 1 / GraphicsDevice.Viewport.AspectRatio, 1f, 10000); //pos = Matrix.Invert(translation).Translation; } mesh.Draw(); DebugShapeRenderer.AddBoundingSphere(modelsp, Microsoft.Xna.Framework.Color.Beige); DebugShapeRenderer.Draw(gameTime, view1, projection1); } modelsp.Transform(ref world,out modelsp); } and in the update method: modelsp.Transform(ref world,out modelsp); and in LoadContent(): modelsp = myModel.Meshes[0].BoundingSphere; The result: And the meshes on both model are equal to 1; And this way I am trying to detect collision: //enemies is a List<Enemy> if (enemies.Sphere.Intersects(modelsp)) { // only for test enemies.Speed = 0.0f; }
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!