protected override void LoadModel() { base.LoadModel(); pUEntity = mSceneMgr.CreateEntity("Sphere.mesh"); pUNode = mSceneMgr.CreateSceneNode(); pUNode.Scale(0.5f, 0.5f, 0.5f); pUNode.AttachObject(pUEntity); controlNode = mSceneMgr.CreateSceneNode(); controlNode.AddChild(pUNode); mSceneMgr.RootSceneNode.AddChild(controlNode); float radius = 0.5f; controlNode.Position += radius * Vector3.UNIT_Y; pUNode.Position -= radius * Vector3.UNIT_Y; physObj = new PhysObj(radius, "Ball", 0.1f, 0.7f, 0.3f); physObj.SceneNode = controlNode; physObj.Position = controlNode.Position; physObj.AddForceToList(new WeightForce(physObj.InvMass)); Physics.AddPhysObj(physObj); physObj.SceneNode = pUNode; pUEntity.GetMesh().BuildEdgeList(); }
/// <summary> /// This method loads the nodes and entities needed by the compound model /// </summary> protected override void LoadModelElements() { hullGroup = mSceneMgr.CreateSceneNode(); wheelGroup = mSceneMgr.CreateSceneNode(); gunGroup = mSceneMgr.CreateSceneNode(); mainHull = mSceneMgr.CreateSceneNode(); hullEntity = mSceneMgr.CreateEntity("Main.mesh"); hullEntity.GetMesh().BuildEdgeList(); sphere = mSceneMgr.CreateSceneNode(); sphereEntity = mSceneMgr.CreateEntity("Sphere.mesh"); sphereEntity.GetMesh().BuildEdgeList(); powerCells = mSceneMgr.CreateSceneNode(); powerCellsEntity = mSceneMgr.CreateEntity("PowerCells.mesh"); powerCellsEntity.GetMesh().BuildEdgeList(); model = mSceneMgr.CreateSceneNode(); float radius = 50; model.Position += radius * Vector3.UNIT_Y; hullGroup.Position -= radius * Vector3.UNIT_Y; physObj = new PhysObj(radius, "PlayerModel", 0.1f, 0.21f, 0.1f); physObj.SceneNode = model; physObj.Position = model.Position; physObj.AddForceToList(new WeightForce(physObj.InvMass)); physObj.AddForceToList(new FrictionForce(physObj)); Physics.AddPhysObj(physObj); }
/// <summary> // The material is attached to the powerup mesh. The model is positioned randomly in the scene and PhysObj attached here /// </summary> protected override void LoadModel() //The class should override the LoadModel method from PowerUp { //load the geometry for the power up and the scene graph nodes base.LoadModel(); lifePickUpEntity = mSceneMgr.CreateEntity("Heart.mesh"); lifePickUpEntity.SetMaterialName("HeartHMD"); lifePickUpNode = mSceneMgr.CreateSceneNode(); lifePickUpNode.Scale(new Vector3(5f, 5f, 5f)); lifePickUpNode.SetPosition(Mogre.Math.RangeRandom(-450, 450), 150, Mogre.Math.RangeRandom(-450, 450)); lifePickUpNode.Position += 3 * Vector3.UNIT_Y; lifePickUpNode.Position -= new Vector3(0f, -8f, 0f); //Model.GameNode.Position -= (3 * Vector3.UNIT_Y); //Model.GameNode.Position -= new Vector3(0f, -80f, 0f); lifePickUpNode.AttachObject(lifePickUpEntity); mSceneMgr.RootSceneNode.AddChild(lifePickUpNode); lifePickUpEntity.GetMesh().BuildEdgeList(); physObj = new PhysObj(9f, "LifePickUp", 0.1f, 0.3f, 20f); physObj.SceneNode = lifePickUpNode; physObj.Position = lifePickUpNode.Position; physObj.AddForceToList(new WeightForce(physObj.InvMass)); physObj.AddForceToList(new FrictionForce(physObj)); Physics.AddPhysObj(physObj); }
/// <summary> // The material is attached to the powerup mesh. The model is positioned randomly in the scene and PhysObj attached here /// </summary> protected override void LoadModel() //The class should override the LoadModel method from PowerUp { //load the geometry for the power up and the scene graph nodes base.LoadModel(); timePickUpEntity = mSceneMgr.CreateEntity("Watch.mesh"); timePickUpEntity.SetMaterialName("clockPickup"); timePickUpNode = mSceneMgr.CreateSceneNode(); timePickUpNode.Scale(new Vector3(0.3f, 0.3f, 0.3f)); timePickUpNode.SetPosition(Mogre.Math.RangeRandom(-450, 450), 150, Mogre.Math.RangeRandom(-450, 450)); timePickUpNode.AttachObject(timePickUpEntity); mSceneMgr.RootSceneNode.AddChild(timePickUpNode); timePickUpEntity.GetMesh().BuildEdgeList(); physObj = new PhysObj(6f, "TimePickUp", 0.1f, 0.3f, 20f); physObj.SceneNode = timePickUpNode; physObj.Position = timePickUpNode.Position; physObj.AddForceToList(new WeightForce(physObj.InvMass)); physObj.AddForceToList(new FrictionForce(physObj)); Physics.AddPhysObj(physObj); }
protected override void LoadModel() { base.LoadModel(); gemEntity = mSceneMgr.CreateEntity("Gem.mesh"); gemNode = mSceneMgr.CreateSceneNode(); gemNode.AttachObject(gemEntity); mSceneMgr.RootSceneNode.AddChild(gemNode); gemEntity.GetMesh().BuildEdgeList(); }
protected override void LoadModel() { base.LoadModel(); pUEntity = mSceneMgr.CreateEntity("PowerCells.mesh"); pUNode = mSceneMgr.CreateSceneNode(); pUNode.AttachObject(pUEntity); pUNode.Scale(2f, 2f, 2f); pUNode.Translate(0, -15, 0); mSceneMgr.RootSceneNode.AddChild(pUNode); pUEntity.GetMesh().BuildEdgeList(); }
public void Load() { plane = new Plane(Vector3.UNIT_Y, 0); MeshPtr groundMeshPtr = MeshManager.Singleton.CreatePlane("ground", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane, groundWidth, groundHeight, grounXSegs, grounZSegs, true, 1, uTiles, vTiles, Vector3.UNIT_Z); groundEntity = mSceneMgr.CreateEntity("ground"); groundNode = mSceneMgr.CreateSceneNode(); groundNode.AttachObject(groundEntity); mSceneMgr.RootSceneNode.AddChild(groundNode); groundEntity.SetMaterialName("Floor"); groundEntity.GetMesh().BuildEdgeList(); groundNode.Position = new Vector3(0, 0, 0); }
protected override void LoadModel() { base.LoadModel(); pUEntity = mSceneMgr.CreateEntity("Heart.mesh"); pUNode = mSceneMgr.CreateSceneNode(); pUNode.Scale(4f, 4f, 4f); pUNode.AttachObject(pUEntity); mSceneMgr.RootSceneNode.AddChild(pUNode); pUEntity.GetMesh().BuildEdgeList(); pUEntity.SetMaterialName("Heart"); }
private void createPhysics() { rigidBody = new RigidBody(); RigidBodyDescription description = new RigidBodyDescription(); description.Density = 4; rigidBody.CreateStatic( node.Position, description, physicsScene, physicsScene.Physics.CreateTriangleMesh(new StaticMeshData(ent.GetMesh()))); ((SinbadState)state).AddActorNode(new ActorNode(node, Actor)); }
protected override void create() { itemEnt = camera.SceneManager.CreateEntity(itemName, itemMeshName); itemNode = camera.SceneManager.RootSceneNode.CreateChildSceneNode(); itemNode.AttachObject(itemEnt); ActorDesc actorDesc = new ActorDesc(); actorDesc.Density = 4; actorDesc.Body = null; actorDesc.Shapes.Add(physics.CreateTriangleMesh(new StaticMeshData(itemEnt.GetMesh()))); itemActor = physicsScene.CreateActor(actorDesc); }
public void Load() { plane = new Plane(Vector3.UNIT_X, 0); MeshPtr groundMeshPtr = MeshManager.Singleton.CreatePlane("wall", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane, groundWidth, groundHeight, grounXSegs, grounZSegs, true, 1, uTiles, vTiles, Vector3.UNIT_Y); groundEntity = mSceneMgr.CreateEntity("wall"); groundNode = mSceneMgr.CreateSceneNode(); groundNode.AttachObject(groundEntity); mSceneMgr.RootSceneNode.AddChild(groundNode); groundEntity.SetMaterialName("Floor"); groundEntity.GetMesh().BuildEdgeList(); groundNode.Position = new Vector3(-2000, 350, 0); plane2 = new Plane(Vector3.UNIT_X, 0); MeshPtr groundMeshPtr2 = MeshManager.Singleton.CreatePlane("wall1", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane, groundWidth, groundHeight, grounXSegs, grounZSegs, true, 1, uTiles, vTiles, Vector3.UNIT_Y); groundEntity = mSceneMgr.CreateEntity("wall1"); groundNode = mSceneMgr.CreateSceneNode(); groundNode.AttachObject(groundEntity); mSceneMgr.RootSceneNode.AddChild(groundNode); groundEntity.SetMaterialName("Floor"); groundEntity.GetMesh().BuildEdgeList(); groundNode.Position = new Vector3(2000, 350, 0); groundNode.Yaw(3.14f); plane3 = new Plane(Vector3.UNIT_X, 0); MeshPtr groundMeshPtr3 = MeshManager.Singleton.CreatePlane("wall2", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane, groundWidth, groundHeight, grounXSegs, grounZSegs, true, 1, uTiles, vTiles, Vector3.UNIT_Y); groundEntity = mSceneMgr.CreateEntity("wall2"); groundNode = mSceneMgr.CreateSceneNode(); groundNode.AttachObject(groundEntity); mSceneMgr.RootSceneNode.AddChild(groundNode); groundEntity.SetMaterialName("Floor"); groundEntity.GetMesh().BuildEdgeList(); groundNode.Position = new Vector3(0, 350, 1996); groundNode.Yaw(1.57f); plane4 = new Plane(Vector3.UNIT_X, 0); MeshPtr groundMeshPtr4 = MeshManager.Singleton.CreatePlane("wall3", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane, groundWidth, groundHeight, grounXSegs, grounZSegs, true, 1, uTiles, vTiles, Vector3.UNIT_Y); groundEntity = mSceneMgr.CreateEntity("wall3"); groundNode = mSceneMgr.CreateSceneNode(); groundNode.AttachObject(groundEntity); mSceneMgr.RootSceneNode.AddChild(groundNode); groundEntity.SetMaterialName("Floor"); groundEntity.GetMesh().BuildEdgeList(); groundNode.Position = new Vector3(0, 350, -1996); groundNode.Yaw(-1.57f); }
private void setupPhysics() { //if (mControlled) //{ // SimpleShape shape = new SimpleShape(new Mogre.Vector3(50, CHAR_HEIGHT, 0)); // CharacterControllerDescription description = new CharacterControllerDescription(); // physicsController = new CharacterController(); // physicsController.createCharacterController(mBodyNode.Position, shape, description, mPhysicsScene); //} //else //{ rigidBody = new RigidBody(); RigidBodyDescription description = new RigidBodyDescription(); description.Density = 4; description.LinearVelocity = new Mogre.Vector3(0, 2, 5); ShapeDesc shape = mPhysics.CreateConvexHull(new StaticMeshData(mBodyEnt.GetMesh())); rigidBody.CreateDynamic( mBodyNode.Position, mBodyNode.Orientation.ToRotationMatrix(), description, mPhysicsScene, shape); //BodyDesc bodyDesc = new BodyDesc(); //bodyDesc.LinearVelocity = new Mogre.Vector3(0, 2, 5); // //// the actor properties control the mass, position and orientation //// if you leave the body set to null it will become a static actor and wont move //ActorDesc actorDesc = new ActorDesc(); //actorDesc.Density = 4; //actorDesc.Body = bodyDesc; //actorDesc.GlobalPosition = mBodyNode.Position; //actorDesc.GlobalOrientation = mBodyNode.Orientation.ToRotationMatrix(); // a quick trick the get the size of the physics shape right is to use the bounding box of the entity //actorDesc.Shapes.Add( // mPhysics.CreateConvexHull(new // StaticMeshData(mBodyEnt.GetMesh()))); // //// finally, create the actor in the physics scene //mActor = mPhysicsScene.CreateActor(actorDesc); if (!mControlled) { ((SinbadState)mWorld).AddActorNode(new ActorNode(mBodyNode, rigidBody.Actor)); } //} }
private void createPanel(SceneManager scm) { MeshManager.Singleton.CreatePlane("floor", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, new Plane(Mogre.Vector3.UNIT_Y, 0), 100, 100, 10, 10, true, 1, 10, 10, Mogre.Vector3.UNIT_Z); Entity floor = scm.CreateEntity("Floor", "floor"); floor.SetMaterialName("Examples/Rockwall"); floor.CastShadows = (false); scm.RootSceneNode.AttachObject(floor); ActorDesc actorDesc = new ActorDesc(); actorDesc.Density = 4; actorDesc.Body = null; actorDesc.Shapes.Add(physx.CreateTriangleMesh(new StaticMeshData(floor.GetMesh()))); Actor floorActor = scene.CreateActor(actorDesc); }
public ActorDesc CreateActorDesc(EntityWorldEntity entityNode, Entity entity, Vector3 position, Quaternion orientation, Vector3 scale) { ActorDesc actorDesc = new ActorDesc(); actorDesc.GlobalPosition = position; actorDesc.GlobalOrientation = orientation.ToRotationMatrix(); if (entityNode.CollisionMode == CollisionMode.ConvexHull || entityNode.CollisionMode == CollisionMode.TriangleMesh) { StaticMeshData meshData = new StaticMeshData(entity.GetMesh(), scale); if (entityNode.CollisionMode == CollisionMode.TriangleMesh) { actorDesc.Shapes.Add(Engine.Physics.CreateTriangleMesh(meshData)); } else { actorDesc.Shapes.Add(Engine.Physics.CreateConvexHull(meshData)); } } else { switch (entityNode.CollisionMode) { case CollisionMode.BoundingBox: actorDesc.Shapes.Add(new BoxShapeDesc(entity.BoundingBox.HalfSize * scale, entity.BoundingBox.Center * scale)); break; case CollisionMode.BoundingSphere: actorDesc.Shapes.Add(new SphereShapeDesc(Engine.MaxAxis(entity.BoundingBox.HalfSize), entity.BoundingBox.Center * scale)); break; case CollisionMode.Shapes: foreach (ShapeDesc shapeDesc in entityNode.Shapes) { actorDesc.Shapes.Add(shapeDesc); } break; default: throw new Exception(entityNode.CollisionMode.ToString() + " not implemented"); } } return(actorDesc); }
/// <summary> // The colour material is attached to the gem mesh. The model is positioned randomly in the scene and PhysObj attached here /// </summary> protected override void LoadModel() //The class should override the LoadModel method from Gem { base.LoadModel(); //you first call the LoadModel from the base class, base.LoadModel() gemEntity = mSceneMgr.CreateEntity("Gem.mesh"); gemEntity.SetMaterialName("blue"); gemNode = mSceneMgr.CreateSceneNode(); gemNode.Scale(new Vector3(4f, 4f, 4f)); gemNode.SetPosition(Mogre.Math.RangeRandom(-450, 450), 150, Mogre.Math.RangeRandom(-450, 450)); gemNode.AttachObject(gemEntity); mSceneMgr.RootSceneNode.AddChild(gemNode); gemEntity.GetMesh().BuildEdgeList(); physObj = new PhysObj(11f, "Gem", 1f, 0.3f, 1f); physObj.SceneNode = gemNode; physObj.Position = gemNode.Position; physObj.AddForceToList(new WeightForce(physObj.InvMass)); physObj.AddForceToList(new FrictionForce(physObj)); Physics.AddPhysObj(physObj); }
//Keeping this code in case I need to use it for the Level Editor //Instead of changing the Scale on each model to look roughly the same, I know change where the camera is located //Whereas I might want it the other way in the level editor because I'll care about relative sizes of the models //public void AutoScale(string currentEntity, double renderWindowHeight, double renderWindowWidth) //{ // if (!mMgr.HasEntity(currentEntity)) // return; // Entity ent = mMgr.GetEntity(currentEntity); // Mesh currentMesh = ent.GetMesh(); //helpful: foo.numlod, numSubMeshes, numAnimations // float posYScale = (float)((renderWindowHeight / 2) / (currentMesh.Bounds.Center.y + currentMesh.Bounds.HalfSize.y)); // float posXScale = (float)((renderWindowWidth / 2) / (currentMesh.Bounds.Center.x + currentMesh.Bounds.HalfSize.x)); // float negYScale = (float)((renderWindowHeight / 2) / (currentMesh.Bounds.Center.y - currentMesh.Bounds.HalfSize.y)) * -1; // float negXScale = (float)((renderWindowWidth / 2) / (currentMesh.Bounds.Center.x - currentMesh.Bounds.HalfSize.x)) * -1; // //Figure out the limiting scale, use that one // if (posYScale <= posXScale && posYScale <= negYScale && posYScale <= negXScale) // Scale(posYScale, posYScale, posYScale, currentMesh.Name); // else if (posXScale <= posYScale && posXScale <= negYScale && posXScale <= negXScale) // Scale(posXScale, posXScale, posXScale, currentMesh.Name); // else if (negYScale <= posXScale && negYScale <= posYScale && negYScale <= negXScale) // Scale(negYScale, negYScale, negYScale, currentMesh.Name); // else // Scale(negXScale, negXScale, negXScale, currentMesh.Name); //} //todo - do I need to add camera, viewport, etc... stuff to this? private void RedrawScene(string attachedEntityName, string meshName, List <KeyValuePair <int, string> > materialNames, string sceneNodeName, float currentScale, bool?autoScale, double renderWindowHeight, double renderWindowWidth) { mMgr.ClearScene(); SceneNode node = null; mAnimationState = null; //I think by default it'll have the materials set as long as they are found - this is needed if we want to change the materials used... Entity ent = mMgr.CreateEntity(attachedEntityName, meshName); Mesh currentMesh = ent.GetMesh(); //helpful: foo.numlod, numSubMeshes, numAnimations try { uint cnt = ent.NumSubEntities; uint meshCounter = 0; Mesh.SubMeshIterator testIter = ent.GetMesh().GetSubMeshIterator(); while (testIter.MoveNext()) { string assignedMaterialName = materialNames.FirstOrDefault(n => n.Key == (int)meshCounter).Value; if (meshCounter < cnt && assignedMaterialName != null) { ent.GetSubEntity(meshCounter).SetMaterialName(assignedMaterialName); } meshCounter++; // SubMesh foobar = testIter.Current; // foobar.SetMaterialName(foobar.MaterialName); } } catch (Exception ex) { ent.SetMaterialName(materialNames[0].Value); //todo, fix logic } //ent.SetMaterialName("Examples/DarkMaterial"); node = mMgr.RootSceneNode.CreateChildSceneNode(sceneNodeName); node.AttachObject(ent); //todo - make this a debug toggle (or perhaps a checkbox on the UI) node.ShowBoundingBox = true; //todo - is this a good way of doing this? float z = 0; if (ent.BoundingBox.Size.z > ent.BoundingBox.Size.x && ent.BoundingBox.Size.z > ent.BoundingBox.Size.y) { z = ent.BoundingBox.Size.z; } else if (ent.BoundingBox.Size.y > ent.BoundingBox.Size.x && ent.BoundingBox.Size.y > ent.BoundingBox.Size.z) { z = ent.BoundingBox.Size.y; } else { z = ent.BoundingBox.Size.x; } //Since mCameraAutoAspectRatio = true, it means: mCamera.AspectRatio = (float)(renderWindowWidth / renderWindowHeight); //By doing this I remove the need for scaling the object - unless you're zooming in/out mCamera.Position = new Vector3(ent.BoundingBox.Center.x, ent.BoundingBox.Center.y, z * 2); //*2 for buffer mCamera.LookAt(ent.BoundingBox.Center); mCamera.NearClipDistance = ent.BoundingBox.Size.z / 3; //Create a single point light source Light light2 = mMgr.CreateLight("MainLight"); light2.Position = new Vector3(0, 10, -25); light2.Type = Light.LightTypes.LT_POINT; light2.SetDiffuseColour(1.0f, 1.0f, 1.0f); light2.SetSpecularColour(0.1f, 0.1f, 0.1f); }
/**************** New Functions Begin ***************/ /// <summary> /// Generate navmesh by entity /// </summary> /// <param name="ent">Ogre Entity</param> /// <returns>Navmesh</returns> public static Navmesh LoadNavmesh(Entity ent) { bool addedSharedVertex = false; vertices.Clear(); faces.Clear(); MeshPtr mesh = ent.GetMesh(); Mesh.SubMeshIterator subIterator = mesh.GetSubMeshIterator(); uint vertexNum = 0; uint vertexOffset = mesh.sharedVertexData.vertexStart; MyVector3 <float>[] verticeArray = new MyVector3 <float> [vertexNum]; VertexElement posElem = mesh.sharedVertexData.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.VES_POSITION); HardwareVertexBufferSharedPtr vertexBuffer = mesh.sharedVertexData.vertexBufferBinding.GetBuffer(posElem.Source); while (subIterator.MoveNext()) { SubMesh subMesh = subIterator.Current; VertexData vertexData = subMesh.useSharedVertices ? mesh.sharedVertexData : subMesh.vertexData; HardwareIndexBufferSharedPtr indexBuffer = subMesh.indexData.indexBuffer; HardwareIndexBuffer.IndexType indexType = indexBuffer.Type; uint indexCount = subMesh.indexData.indexCount; uint trisNum = indexCount / 3; uint[] indcies = new uint[indexCount]; uint indexOffset = subMesh.indexData.indexStart; if (subMesh.useSharedVertices) { if (!addedSharedVertex) { vertexNum += mesh.sharedVertexData.vertexCount; addedSharedVertex = true; } } else { vertexNum += subMesh.vertexData.vertexCount; } unsafe { uint * pLong = (uint *)(indexBuffer.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY)); ushort *pShort = (ushort *)pLong; for (int i = 0; i < indexCount; i++) { if (indexType == HardwareIndexBuffer.IndexType.IT_32BIT) { indcies[indexOffset] = pLong[i] + vertexNum; } else { indcies[indexOffset] = pShort[i] + vertexNum; } indexOffset++; } } int indexLength = indcies.Length / 3; for (int i = 0; i < indexLength; i++) { faces.Add(new MyVector3 <ushort>( (ushort)indcies[i * 3 + 0], (ushort)indcies[i * 3 + 1], (ushort)indcies[i * 3 + 2] )); } indexBuffer.Unlock(); if (subMesh.vertexData != null) { vertexNum = subMesh.vertexData.vertexCount; vertexOffset = subMesh.vertexData.vertexStart; verticeArray = new MyVector3 <float> [vertexNum]; posElem = subMesh.vertexData.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.VES_POSITION); vertexBuffer = subMesh.vertexData.vertexBufferBinding.GetBuffer(posElem.Source); unsafe { byte * vertexMemory = (byte *)vertexBuffer.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY); float *pVertexBuffer; for (int i = 0; i < vertexNum; i++) { posElem.BaseVertexPointerToElement(vertexMemory, &pVertexBuffer); verticeArray[vertexOffset] = (new MyVector3 <float>( pVertexBuffer[0], pVertexBuffer[1], pVertexBuffer[2] )); vertexMemory += vertexBuffer.VertexSize; vertexOffset++; } } for (int i = 0; i < verticeArray.Length; i++) { vertices.Add(verticeArray[i]); } vertexBuffer.Unlock(); } } vertexNum = mesh.sharedVertexData.vertexCount; vertexOffset = mesh.sharedVertexData.vertexStart; verticeArray = new MyVector3 <float> [vertexNum]; posElem = mesh.sharedVertexData.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.VES_POSITION); vertexBuffer = mesh.sharedVertexData.vertexBufferBinding.GetBuffer(posElem.Source); unsafe { byte * vertexMemory = (byte *)vertexBuffer.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY); float *pVertexBuffer; for (int i = 0; i < vertexNum; i++) { posElem.BaseVertexPointerToElement(vertexMemory, &pVertexBuffer); verticeArray[vertexOffset] = (new MyVector3 <float>( pVertexBuffer[0], pVertexBuffer[1], pVertexBuffer[2] )); vertexMemory += vertexBuffer.VertexSize; vertexOffset++; } } for (int i = 0; i < verticeArray.Length; i++) { vertices.Add(verticeArray[i]); } vertexBuffer.Unlock(); return(GenerateNavmesh()); }
/// <summary> /// Give it an entity and it'll create a BulletSharp.TriangleMesh out of it /// </summary> /// <param name="ent">The entity to convert. It'll grab its mesh and use all of its submeshes</param> /// <param name="node">The node the entity is attached to. We aren't modifying it, but we'll use its transforms</param> /// <returns>A bullet trimesh</returns> public static TriangleMesh Convert(Entity ent, SceneNode node) { return Convert(ent.GetMesh(), node._getDerivedPosition(), node._getDerivedOrientation(), node._getDerivedScale()); }
public RaycastResult Raycast(Ray ray, uint queryMask) { RaycastResult rr = new RaycastResult(); RaySceneQuery raySceneQuery = this.sceneMgr.CreateRayQuery(new Ray()); raySceneQuery.SetSortByDistance(true); // check we are initialised if (raySceneQuery != null) { // create a query object raySceneQuery.Ray = ray; raySceneQuery.SetSortByDistance(true); raySceneQuery.QueryMask = queryMask; // execute the query, returns a vector of hits if (raySceneQuery.Execute().Count <= 0) { // raycast did not hit an objects bounding box return(null); } } else { // LogManager.Singleton.LogMessage("Cannot raycast without RaySceneQuery instance"); return(null); } // at this point we have raycast to a series of different objects bounding boxes. // we need to test these different objects to see which is the first polygon hit. // there are some minor optimizations (distance based) that mean we wont have to // check all of the objects most of the time, but the worst case scenario is that // we need to test every triangle of every object. // Ogre::Real closest_distance = -1.0f; rr.Distance = -1.0f; Vector3 closest_result = Vector3.ZERO; RaySceneQueryResult query_result = raySceneQuery.GetLastResults(); for (int qridx = 0; qridx < query_result.Count; qridx++) { // stop checking if we have found a raycast hit that is closer // than all remaining entities if ((rr.Distance >= 0.0f) && (rr.Distance < query_result[qridx].distance)) { break; } // only check this result if its a hit against an entity if ((query_result[qridx].movable != null) && (query_result[qridx].movable.MovableType.CompareTo("Entity") == 0)) { // get the entity to check Entity pentity = (Entity)query_result[qridx].movable; // mesh data to retrieve uint vertex_count; uint index_count; Vector3[] vertices; ulong[] indices; // get the mesh information GetMeshInformation( pentity.GetMesh(), out vertex_count, out vertices, out index_count, out indices, pentity.ParentNode._getDerivedPosition(), pentity.ParentNode._getDerivedOrientation(), pentity.ParentNode.GetScale()); // test for hitting individual triangles on the mesh bool new_closest_found = false; for (int i = 0; i < (int)index_count; i += 3) { // check for a hit against this triangle Pair <bool, float> hit = Mogre.Math.Intersects(ray, vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]], true, false); // if it was a hit check if its the closest if (hit.first) { if ((rr.Distance < 0.0f) || (hit.second < rr.Distance)) { // this is the closest so far, save it off rr.Distance = hit.second; new_closest_found = true; } } } // if we found a new closest raycast for this object, update the // closest_result before moving on to the next object. if (new_closest_found) { rr.Target = pentity; closest_result = ray.GetPoint(rr.Distance); } } } this.sceneMgr.DestroyQuery(raySceneQuery); raySceneQuery.Dispose(); // return the result if (rr.Distance >= 0.0f) { // raycast success rr.Position = closest_result; return(rr); } else { return(null); } }
public RaycastResult Raycast(Ray ray, uint queryMask) { RaycastResult rr = new RaycastResult(); RaySceneQuery raySceneQuery = this.sceneMgr.CreateRayQuery(new Ray()); raySceneQuery.SetSortByDistance(true); // check we are initialised if (raySceneQuery != null) { // create a query object raySceneQuery.Ray = ray; raySceneQuery.SetSortByDistance(true); raySceneQuery.QueryMask = queryMask; using (RaySceneQueryResult queryResult = raySceneQuery.Execute()) { // execute the query, returns a vector of hits if (queryResult.Count <= 0) { // raycast did not hit an objects bounding box this.sceneMgr.DestroyQuery(raySceneQuery); raySceneQuery.Dispose(); return(null); } // at this point we have raycast to a series of different objects bounding boxes. // we need to test these different objects to see which is the first polygon hit. // there are some minor optimizations (distance based) that mean we wont have to // check all of the objects most of the time, but the worst case scenario is that // we need to test every triangle of every object. // Ogre::Real closest_distance = -1.0f; rr.Distance = -1.0f; Vector3 closestResult = Vector3.ZERO; for (int qridx = 0; qridx < queryResult.Count; qridx++) { // stop checking if we have found a raycast hit that is closer // than all remaining entities if (rr.Distance >= 0.0f && rr.Distance < queryResult[qridx].distance) { break; } // only check this result if its a hit against an entity if (queryResult[qridx].movable != null && queryResult[qridx].movable.MovableType == "Entity") { // get the entity to check Entity entity = (Entity)queryResult[qridx].movable; // mesh data to retrieve Vector3[] vertices; int[] indices; RenderOperation.OperationTypes opType; // get the mesh information using (MeshPtr mesh = entity.GetMesh()) { opType = mesh.GetSubMesh(0).operationType; Debug.Assert(CheckSubMeshOpType(mesh, opType)); GetMeshInformation( mesh, out vertices, out indices, entity.ParentNode._getDerivedPosition(), entity.ParentNode._getDerivedOrientation(), entity.ParentNode._getDerivedScale()); } int vertexCount = vertices.Length; int indexCount = indices.Length; // test for hitting individual triangles on the mesh bool newClosestFound = false; Pair <bool, float> hit; switch (opType) { case RenderOperation.OperationTypes.OT_TRIANGLE_LIST: for (int i = 0; i < indexCount; i += 3) { hit = Mogre.Math.Intersects(ray, vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]], true, false); if (CheckDistance(rr, hit)) { newClosestFound = true; } } break; case RenderOperation.OperationTypes.OT_TRIANGLE_STRIP: for (int i = 0; i < indexCount - 2; i++) { hit = Mogre.Math.Intersects(ray, vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]], true, true); if (CheckDistance(rr, hit)) { newClosestFound = true; } } break; case RenderOperation.OperationTypes.OT_TRIANGLE_FAN: for (int i = 0; i < indexCount - 2; i++) { hit = Mogre.Math.Intersects(ray, vertices[indices[0]], vertices[indices[i + 1]], vertices[indices[i + 2]], true, true); if (CheckDistance(rr, hit)) { newClosestFound = true; } } break; default: throw new Exception("invalid operation type"); } // if we found a new closest raycast for this object, update the // closest_result before moving on to the next object. if (newClosestFound) { rr.Target = entity; closestResult = ray.GetPoint(rr.Distance); } } } this.sceneMgr.DestroyQuery(raySceneQuery); raySceneQuery.Dispose(); // return the result if (rr.Distance >= 0.0f) { // raycast success rr.Position = closestResult; return(rr); } else { return(null); } } } else { return(null); } }
/// <summary> /// Give it an entity and it'll create a BulletSharp.ConvexHullShape out of it /// </summary> /// <param name="ent">The entity to convert. It'll grab its mesh and use all of its submeshes</param> /// <param name="node">The node the entity is attached to. We aren't modifying it, but we'll use its transforms</param> /// <returns>A bullet trimesh</returns> public static ConvexHullShape ConvertToHull(Entity ent, SceneNode node) { return ConvertToHull(ent.GetMesh(), node._getDerivedPosition(), node._getDerivedOrientation(), node._getDerivedScale()); }
/// <summary> /// 将当前结点状态与之同步 /// operateflag说明 /// 1增加结点 /// 2删除结点 /// 3更新位置和缩放,旋转 /// </summary> /// <param name="node"></param> /// <param name="operateflat"></param> public ModelEntryStruct AddRemoveState(SceneNode node, int operateflag) { if (node == null) { return(null); } switch (operateflag) { case 1: //增加操作 { ModelEntryStruct modelentry = new ModelEntryStruct(); Entity en = node.GetAttachedObject(0) as Entity; if (en != null) { //得到模型的相关信息, modelentry.模型名 = en.GetMesh().Name; modelentry.材质 = ""; modelentry.实体名 = en.Name; modelentry.缩放比例 = node.GetScale(); modelentry.位置 = node.Position; modelentry.旋转角度 = new Vector3(); modelEntry.模型链表.Add(modelentry); //modelentry.名称 = MainOgreForm.GetDateName(); //更新树节点 if (this.AddNode != null) { this.AddNode(modelentry); } //MainOgreForm.Singleton.AddNodeTree(modelentry); return(modelentry); } } break; case 2: //删除 { Entity en = node.GetAttachedObject(0) as Entity; if (en == null) { return(null); } for (int i = 0; i < modelEntry.模型链表.Count; i++) { if (modelEntry.模型链表[i].实体名 == en.Name) { if (this.RemoveNode != null) { this.RemoveNode(modelEntry.模型链表[i]); } //MainOgreForm.Singleton.RemoveNodeTree(modelEntry.模型链表[i]); modelEntry.模型链表.RemoveAt(i); break; } } } break; default: break; } return(null); }