void ChangeCamera(DrawMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (keyboardState[Key.Tab] && !oldKeyboardState[Key.Tab]) { PhysicsObject activeCameraObject = null; int activeCameraInstanceIndex = 0; for (int i = 0; i < scene.PhysicsObjectWithCameraCount; i++) { PhysicsObject cameraObject = scene.GetPhysicsObjectWithCamera(i); if (cameraObject.Camera.Active) { activeCameraObject = cameraObject; activeCameraInstanceIndex = i; cameraObject.Camera.Active = false; } } int nextActiveCameraInstanceIndex = (activeCameraInstanceIndex + 1) % scene.PhysicsObjectWithCameraCount; PhysicsObject nextCameraObject = scene.GetPhysicsObjectWithCamera(nextActiveCameraInstanceIndex); nextCameraObject.Camera.Active = true; } oldKeyboardState = keyboardState; }
public RagdollsScene(Demo demo, string name, int instanceIndex, string info) { this.demo = demo; this.name = name; this.instanceIndexName = " " + instanceIndex.ToString(); this.info = info; // Create a new objects in the scene skyInstance1 = new Sky(demo, 1); quadInstance1 = new Quad(demo, 1); cursorInstance = new Cursor(demo); shotInstance = new Shot(demo); ragdoll1Instance1 = new Ragdoll1(demo, 1); ragdoll2Instance1 = new Ragdoll2(demo, 1); ragdoll3Instance1 = new Ragdoll3(demo, 1); ragdoll1Instance2 = new Ragdoll1(demo, 2); ragdoll2Instance2 = new Ragdoll2(demo, 2); ragdoll3Instance2 = new Ragdoll3(demo, 2); ragdoll1Instance3 = new Ragdoll1(demo, 3); ragdoll2Instance3 = new Ragdoll2(demo, 3); ragdoll3Instance3 = new Ragdoll3(demo, 3); camera1Instance1 = new Camera1(demo, 1); camera1Instance2 = new Camera1(demo, 2); lightInstance = new Lights(demo); // Create a new controllers in the scene skyDraw1Instance1 = new SkyDraw1(demo, 1); cursorDraw1Instance = new CursorDraw1(demo); camera1Animation1Instance1 = new Camera1Animation1(demo, 1); camera1Animation1Instance2 = new Camera1Animation1(demo, 2); camera1Draw1Instance1 = new Camera1Draw1(demo, 1); camera1Draw1Instance2 = new Camera1Draw1(demo, 2); oldKeyboardState = demo.GetKeyboardState(); }
public void SetControllers(bool enableDistanceCollision) { sphere = scene.Factory.ShapeManager.Find("Sphere"); cameraUp = scene.Factory.PhysicsObjectManager.Find("Camera 2 Up" + instanceIndexName); cameraBody = scene.Factory.PhysicsObjectManager.Find("Camera 2 Body" + instanceIndexName); cameraDown = scene.Factory.PhysicsObjectManager.Find("Camera 2 Down" + instanceIndexName); oldMouseState = demo.GetMouseState(); oldKeyboardState = demo.GetKeyboardState(); this.enableDistanceCollision = enableDistanceCollision; enableDistance = false; enableControl = false; distance = 0; shotCount = -1; enableShotTab = false; for (int i = 0; i < shotTab.Length; i++) { shotTab[i] = null; } PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Find("Camera 2 Up" + instanceIndexName); if (objectBase != null) { objectBase.UserControllers.TransformMethods += new SimulateMethod(MoveCursor); objectBase.UserControllers.PostTransformMethods += new SimulateMethod(Move); } }
public void SetControllers(bool enableExternalMoving) { steeringGearSwitch = scene.Factory.PhysicsObjectManager.Find("Car 1 Steering Gear Switch" + instanceIndexName); steeringGearDown = scene.Factory.PhysicsObjectManager.Find("Car 1 Steering Gear Down" + instanceIndexName); chassisUpPanel = scene.Factory.PhysicsObjectManager.Find("Car 1 Chassis Up Panel" + instanceIndexName); chassisDownPanel = scene.Factory.PhysicsObjectManager.Find("Car 1 Chassis Down Panel" + instanceIndexName); steeringWheel = scene.Factory.PhysicsObjectManager.Find("Car 1 Steering Wheel" + instanceIndexName); chassisBack = scene.Factory.PhysicsObjectManager.Find("Car 1 Chassis Back" + instanceIndexName); wheel1 = scene.Factory.PhysicsObjectManager.Find("Car 1 Wheel 1" + instanceIndexName); wheel2 = scene.Factory.PhysicsObjectManager.Find("Car 1 Wheel 2" + instanceIndexName); steeringGearConstraint = scene.Factory.ConstraintManager.Find("Car 1 Steering Gear Constraint" + instanceIndexName); constraint1 = scene.Factory.ConstraintManager.Find("Car 1 Constraint 1" + instanceIndexName); constraint4 = scene.Factory.ConstraintManager.Find("Car 1 Constraint 4" + instanceIndexName); rightDoorConstraint = scene.Factory.ConstraintManager.Find("Car 1 Right Door Constraint" + instanceIndexName); rightDoorBody = scene.Factory.PhysicsObjectManager.Find("Car 1 Right Door Body" + instanceIndexName); leftDoorConstraint = scene.Factory.ConstraintManager.Find("Car 1 Left Door Constraint" + instanceIndexName); leftDoorBody = scene.Factory.PhysicsObjectManager.Find("Car 1 Left Door Body" + instanceIndexName); chassisMiddleDown = scene.Factory.PhysicsObjectManager.Find("Car 1 Chassis Middle Down" + instanceIndexName); chassisMiddleFront = scene.Factory.PhysicsObjectManager.Find("Car 1 Chassis Middle Front" + instanceIndexName); chassisMiddleBack = scene.Factory.PhysicsObjectManager.Find("Car 1 Chassis Middle Back" + instanceIndexName); upBodyFront = scene.Factory.PhysicsObjectManager.Find("Car 1 Up Body Front" + instanceIndexName); upBodyDownBack = scene.Factory.PhysicsObjectManager.Find("Car 1 Up Body Down Back" + instanceIndexName); oldKeyboardState = demo.GetKeyboardState(); this.enableExternalMoving = enableExternalMoving; totalSwitchLeftTime = 2.0f * maxSwitchLeftTime; totalSwitchRightTime = 2.0f * maxSwitchRightTime; PhysicsObject objectBase = null; objectBase = scene.Factory.PhysicsObjectManager.Find("Car 1 Body" + instanceIndexName); if (objectBase != null) { objectBase.UserControllers.PostTransformMethods += new SimulateMethod(Move); } objectBase = scene.Factory.PhysicsObjectManager.Find("Car 1 Left Door Switch" + instanceIndexName); if (objectBase != null) { objectBase.UserControllers.PostTransformMethods += new SimulateMethod(SwitchLeft); } objectBase = scene.Factory.PhysicsObjectManager.Find("Car 1 Right Door Switch" + instanceIndexName); if (objectBase != null) { objectBase.UserControllers.PostTransformMethods += new SimulateMethod(SwitchRight); } }
public void SetControllers() { sphere = scene.Factory.ShapeManager.Find("Sphere"); oldMouseState = demo.GetMouseState(); oldKeyboardState = demo.GetKeyboardState(); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Find("Simple Camera" + instanceIndexName); if (objectBase != null) { objectBase.UserControllers.PostTransformMethods += new SimulateMethod(Move); } }
public void SetControllers(bool enableDrawBoundingBoxes, bool enableDrawContactPoints, bool enableDrawSlipingObjects, bool enableDrawImpactFactors, bool enableDrawLights, bool enableWireframe) { oldKeyboardState = demo.GetKeyboardState(); this.enableDrawBoundingBoxes = enableDrawBoundingBoxes; this.enableDrawContactPoints = enableDrawContactPoints; this.enableDrawSlipingObjects = enableDrawSlipingObjects; this.enableDrawImpactFactors = enableDrawImpactFactors; this.enableDrawLights = enableDrawLights; this.enableWireframe = enableWireframe; PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Find("Camera 2 Up" + instanceIndexName); if (objectBase != null) { objectBase.UserControllers.DrawMethods += new DrawMethod(Draw); objectBase.UserControllers.DrawMethods += new DrawMethod(DrawBoundingBoxes); objectBase.UserControllers.DrawMethods += new DrawMethod(DrawContactPoints); objectBase.UserControllers.DrawMethods += new DrawMethod(DrawImpactFactors); } }
public void SetControllers(bool enableExternalMoving) { bodyBox0 = scene.Factory.PhysicsObjectManager.Find("Boat 1 Body Box 0" + instanceIndexName); engineSwitch = scene.Factory.PhysicsObjectManager.Find("Boat 1 Engine Switch" + instanceIndexName); constraint1 = scene.Factory.ConstraintManager.Find("Boat 1 Constraint 1" + instanceIndexName); constraint2 = scene.Factory.ConstraintManager.Find("Boat 1 Constraint 2" + instanceIndexName); engine = scene.Factory.PhysicsObjectManager.Find("Boat 1 Engine" + instanceIndexName); engineRotor = scene.Factory.PhysicsObjectManager.Find("Boat 1 Engine Rotor" + instanceIndexName); oldKeyboardState = demo.GetKeyboardState(); this.enableExternalMoving = enableExternalMoving; PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Find("Boat 1 Body" + instanceIndexName); if (objectBase != null) { objectBase.UserControllers.PostTransformMethods += new SimulateMethod(Move); } }
public void SetControllers() { sphere = scene.Factory.ShapeManager.Find("Sphere"); turretGun = scene.Factory.PhysicsObjectManager.Find("Amphibian 1 Turret Gun" + instanceIndexName); turretConstraint = scene.Factory.ConstraintManager.Find("Amphibian 1 Turret Constraint" + instanceIndexName); turretGunConstraint = scene.Factory.ConstraintManager.Find("Amphibian 1 Turret Gun Constraint" + instanceIndexName); turretGun1 = scene.Factory.PhysicsObjectManager.Find("Amphibian 1 Turret Gun 1" + instanceIndexName); turretGun2 = scene.Factory.PhysicsObjectManager.Find("Amphibian 1 Turret Gun 2" + instanceIndexName); turretBodyDown = scene.Factory.PhysicsObjectManager.Find("Amphibian 1 Turret Body Down" + instanceIndexName); shotCount = -1; oldKeyboardState = demo.GetKeyboardState(); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Find("Amphibian 1 Body" + instanceIndexName); if (objectBase != null) { objectBase.UserControllers.PostTransformMethods += new SimulateMethod(Move); } }
public void SetControllers() { sphere = scene.Factory.ShapeManager.Find("Sphere"); oldMouseState = demo.GetMouseState(); oldKeyboardState = demo.GetKeyboardState(); shotCount = -1; enableShotTab = false; for (int i = 0; i < shotTab.Length; i++) { shotTab[i] = null; } PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Find("Camera 1" + instanceIndexName); if (objectBase != null) { objectBase.UserControllers.TransformMethods += new SimulateMethod(MoveCursor); objectBase.UserControllers.PostTransformMethods += new SimulateMethod(Move); } }
public void Draw(DrawMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); if (!objectBase.Camera.Enabled) { return; } if (!objectBase.Camera.Active) { return; } DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (keyboardState[Key.B] && !oldKeyboardState[Key.B]) { enableDrawBoundingBoxes = !enableDrawBoundingBoxes; } if (keyboardState[Key.C] && !oldKeyboardState[Key.C]) { enableDrawContactPoints = !enableDrawContactPoints; } if (keyboardState[Key.V] && !oldKeyboardState[Key.V]) { enableDrawSlipingObjects = !enableDrawSlipingObjects; } if (keyboardState[Key.I] && !oldKeyboardState[Key.I]) { enableDrawImpactFactors = !enableDrawImpactFactors; } if (keyboardState[Key.G] && !oldKeyboardState[Key.G]) { enableDrawLights = !enableDrawLights; } if (keyboardState[Key.N] && !oldKeyboardState[Key.N]) { enableWireframe = !enableWireframe; } oldKeyboardState = keyboardState; demo.EnableWireframe = enableWireframe; PhysicsObject menuPhysicsObjectWithCamera = demo.MenuScene.PhysicsScene.GetPhysicsObjectWithCamera(0); if (menuPhysicsObjectWithCamera != null) { if (menuPhysicsObjectWithCamera.Camera.UserDataObj != null) { Camera3Draw1 menuCamera = menuPhysicsObjectWithCamera.Camera.UserDataObj as Camera3Draw1; if (menuCamera != null) { menuCamera.EnableDrawBoundingBoxes = enableDrawBoundingBoxes; menuCamera.EnableDrawContactPoints = enableDrawContactPoints; menuCamera.EnableDrawSlipingObjects = enableDrawSlipingObjects; menuCamera.EnableDrawLights = enableDrawLights; menuCamera.EnableWireframe = enableWireframe; } } } float time = args.Time; PhysicsObject drawPhysicsObject, transparentPhysicsObject, lightPhysicsObject; PhysicsLight sceneLight, drawLight; DemoMesh mesh, quad; objectBase.Camera.View.GetViewMatrix(ref view); objectBase.Camera.Projection.GetProjectionMatrix(ref projection); sceneLight = scene.Light; quad = demo.Meshes[quadName]; GL.BindFramebuffer(FramebufferTarget.Framebuffer, demo.SceneFrameBuffer); targets[0] = DrawBuffersEnum.ColorAttachment0; targets[1] = DrawBuffersEnum.ColorAttachment1; targets[2] = DrawBuffersEnum.ColorAttachment2; targets[3] = DrawBuffersEnum.ColorAttachment3; GL.DrawBuffers(4, targets); GL.Clear(ClearBufferMask.DepthBufferBit); GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.Zero); GL.BlendEquation(BlendEquationMode.FuncAdd); GL.Disable(EnableCap.Blend); GL.Disable(EnableCap.DepthTest); GL.DepthMask(false); GL.Disable(EnableCap.CullFace); renderClear.SetClearScreenColor(ref demo.ClearScreenColor); quad.Draw(renderClear); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.DepthTest); GL.DepthMask(true); for (int i = 0; i < objectBase.Camera.DrawPhysicsObjectCount; i++) { drawPhysicsObject = objectBase.Camera.GetDrawPhysicsObject(i); if ((drawPhysicsObject.UserControllers.DrawMethods == null) || (drawPhysicsObject == objectBase)) { if (drawPhysicsObject.UserDataStr == null) { continue; } if ((drawPhysicsObject.Shape == null) && drawPhysicsObject.IsBrokenRigidGroup) { continue; } if ((drawPhysicsObject.RigidGroupOwner != drawPhysicsObject) && (drawPhysicsObject.RigidGroupOwner.UserDataStr != null)) { continue; } drawPhysicsObject.MainWorldTransform.GetTransformMatrix(ref world); mesh = demo.Meshes[drawPhysicsObject.UserDataStr]; mesh.Draw(ref world, ref view, ref projection, sceneLight, drawPhysicsObject.Material, objectBase.Camera, drawPhysicsObject.RigidGroupOwner.IsSleeping && enableDrawSlipingObjects, enableWireframe); } else { if (drawPhysicsObject.UserControllers.EnableDraw) { continue; } if ((drawPhysicsObject.Shape == null) && drawPhysicsObject.IsBrokenRigidGroup) { continue; } if ((drawPhysicsObject.RigidGroupOwner != drawPhysicsObject) && (drawPhysicsObject.RigidGroupOwner.UserDataStr != null)) { continue; } drawPhysicsObject.UserControllers.DrawMethodArgs.Time = time; drawPhysicsObject.UserControllers.DrawMethodArgs.OwnerIndex = drawPhysicsObject.Index; drawPhysicsObject.UserControllers.DrawMethodArgs.OwnerSceneIndex = scene.Index; drawPhysicsObject.UserControllers.DrawMethods(drawPhysicsObject.UserControllers.DrawMethodArgs); } } if (objectBase.Camera.TransparentPhysicsObjectCount != 0) { targets[0] = DrawBuffersEnum.ColorAttachment0; targets[1] = DrawBuffersEnum.ColorAttachment1; targets[2] = DrawBuffersEnum.None; targets[3] = DrawBuffersEnum.None; GL.DrawBuffers(4, targets); GL.DepthMask(false); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha); GL.BlendEquation(BlendEquationMode.FuncAdd); for (int i = 0; i < objectBase.Camera.TransparentPhysicsObjectCount; i++) { transparentPhysicsObject = objectBase.Camera.GetTransparentPhysicsObject(i); if ((transparentPhysicsObject.UserControllers.DrawMethods == null) || (transparentPhysicsObject == objectBase)) { if (transparentPhysicsObject.UserDataStr == null) { continue; } if ((transparentPhysicsObject.Shape == null) && transparentPhysicsObject.IsBrokenRigidGroup) { continue; } if ((transparentPhysicsObject.RigidGroupOwner != transparentPhysicsObject) && (transparentPhysicsObject.RigidGroupOwner.UserDataStr != null)) { continue; } transparentPhysicsObject.MainWorldTransform.GetTransformMatrix(ref world); mesh = demo.Meshes[transparentPhysicsObject.UserDataStr]; mesh.Draw(ref world, ref view, ref projection, sceneLight, transparentPhysicsObject.Material, objectBase.Camera, transparentPhysicsObject.RigidGroupOwner.IsSleeping && enableDrawSlipingObjects, enableWireframe); } else { if (transparentPhysicsObject.UserControllers.EnableDraw) { continue; } if ((transparentPhysicsObject.Shape == null) && transparentPhysicsObject.IsBrokenRigidGroup) { continue; } if ((transparentPhysicsObject.RigidGroupOwner != transparentPhysicsObject) && (transparentPhysicsObject.RigidGroupOwner.UserDataStr != null)) { continue; } transparentPhysicsObject.UserControllers.DrawMethodArgs.Time = time; transparentPhysicsObject.UserControllers.DrawMethodArgs.OwnerIndex = transparentPhysicsObject.Index; transparentPhysicsObject.UserControllers.DrawMethodArgs.OwnerSceneIndex = scene.Index; transparentPhysicsObject.UserControllers.DrawMethods(transparentPhysicsObject.UserControllers.DrawMethodArgs); } } targets[0] = DrawBuffersEnum.None; targets[1] = DrawBuffersEnum.None; targets[2] = DrawBuffersEnum.ColorAttachment2; targets[3] = DrawBuffersEnum.ColorAttachment3; GL.DrawBuffers(4, targets); GL.DepthMask(true); GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.Zero); GL.BlendEquation(BlendEquationMode.FuncAdd); GL.Disable(EnableCap.Blend); for (int i = 0; i < objectBase.Camera.TransparentPhysicsObjectCount; i++) { transparentPhysicsObject = objectBase.Camera.GetTransparentPhysicsObject(i); if (!transparentPhysicsObject.Material.TransparencySecondPass) { continue; } if ((transparentPhysicsObject.UserControllers.DrawMethods == null) || (transparentPhysicsObject == objectBase)) { if (transparentPhysicsObject.UserDataStr == null) { continue; } if ((transparentPhysicsObject.Shape == null) && transparentPhysicsObject.IsBrokenRigidGroup) { continue; } if ((transparentPhysicsObject.RigidGroupOwner != transparentPhysicsObject) && (transparentPhysicsObject.RigidGroupOwner.UserDataStr != null)) { continue; } transparentPhysicsObject.MainWorldTransform.GetTransformMatrix(ref world); mesh = demo.Meshes[transparentPhysicsObject.UserDataStr]; mesh.Draw(ref world, ref view, ref projection, sceneLight, transparentPhysicsObject.Material, objectBase.Camera, transparentPhysicsObject.RigidGroupOwner.IsSleeping && enableDrawSlipingObjects, enableWireframe); } else { if (transparentPhysicsObject.UserControllers.EnableDraw) { continue; } if ((transparentPhysicsObject.Shape == null) && transparentPhysicsObject.IsBrokenRigidGroup) { continue; } if ((transparentPhysicsObject.RigidGroupOwner != transparentPhysicsObject) && (transparentPhysicsObject.RigidGroupOwner.UserDataStr != null)) { continue; } transparentPhysicsObject.UserControllers.DrawMethodArgs.Time = time; transparentPhysicsObject.UserControllers.DrawMethodArgs.OwnerIndex = transparentPhysicsObject.Index; transparentPhysicsObject.UserControllers.DrawMethodArgs.OwnerSceneIndex = scene.Index; transparentPhysicsObject.UserControllers.DrawMethods(transparentPhysicsObject.UserControllers.DrawMethodArgs); } } } GL.BindFramebuffer(FramebufferTarget.Framebuffer, demo.LightFrameBuffer); GL.DrawBuffer(DrawBufferMode.ColorAttachment0); GL.ClearColor(demo.ClearLightColor); GL.Clear(ClearBufferMask.ColorBufferBit); GL.Disable(EnableCap.DepthTest); GL.DepthMask(false); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.SrcAlpha); GL.BlendEquation(BlendEquationMode.FuncAdd); if ((sceneLight != null) && sceneLight.Enabled) { GL.Disable(EnableCap.CullFace); sceneLight.GetDirection(ref direction); sceneLight.GetDiffuse(ref lightDiffuse); sceneLight.GetSpecular(ref lightSpecular); renderLightDirectional.Enable = true; renderLightDirectional.Width = objectBase.Camera.Projection.Width; renderLightDirectional.Height = objectBase.Camera.Projection.Height; renderLightDirectional.SetView(ref view); renderLightDirectional.SetProjection(ref projection); renderLightDirectional.SetLightDirection(ref direction); renderLightDirectional.SetLightDiffuse(ref lightDiffuse); renderLightDirectional.SetLightSpecular(ref lightSpecular); renderLightDirectional.Intensity = sceneLight.Intensity; renderLightDirectional.SpecularTexture = demo.SpecularTexture; renderLightDirectional.NormalTexture = demo.NormalTexture; renderLightDirectional.DepthTexture = demo.DepthTexture; quad.Draw(renderLightDirectional); } for (int i = 0; i < objectBase.Camera.LightPhysicsObjectCount; i++) { lightPhysicsObject = objectBase.Camera.GetLightPhysicsObject(i); drawLight = lightPhysicsObject.Light; if ((drawLight == null) || !drawLight.Enabled) { continue; } if (drawLight.Type == PhysicsLightType.Directional) { GL.Disable(EnableCap.CullFace); drawLight.GetDirection(ref direction); drawLight.GetDiffuse(ref lightDiffuse); drawLight.GetSpecular(ref lightSpecular); renderLightDirectional.Enable = true; renderLightDirectional.Width = objectBase.Camera.Projection.Width; renderLightDirectional.Height = objectBase.Camera.Projection.Height; renderLightDirectional.SetView(ref view); renderLightDirectional.SetProjection(ref projection); renderLightDirectional.SetLightDirection(ref direction); renderLightDirectional.SetLightDiffuse(ref lightDiffuse); renderLightDirectional.SetLightSpecular(ref lightSpecular); renderLightDirectional.Intensity = drawLight.Intensity; renderLightDirectional.SpecularTexture = demo.SpecularTexture; renderLightDirectional.NormalTexture = demo.NormalTexture; renderLightDirectional.DepthTexture = demo.DepthTexture; quad.Draw(renderLightDirectional); } else if (drawLight.Type == PhysicsLightType.Point) { lightPhysicsObject.EnableAddToCameraDrawTransparentPhysicsObjects = false; if (enableDrawLights) { lightPhysicsObject.EnableAddToCameraDrawTransparentPhysicsObjects = true; lightPhysicsObject.Material.TransparencyFactor = 0.5f; lightPhysicsObject.Material.TransparencySecondPass = false; } GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Front); lightPhysicsObject.MainWorldTransform.GetPosition(ref position); lightPhysicsObject.MainWorldTransform.GetTransformMatrix(ref world); drawLight.GetDiffuse(ref lightDiffuse); drawLight.GetSpecular(ref lightSpecular); renderLightPoint.Enable = true; renderLightPoint.Width = objectBase.Camera.Projection.Width; renderLightPoint.Height = objectBase.Camera.Projection.Height; renderLightPoint.SetWorld(ref world); renderLightPoint.SetView(ref view); renderLightPoint.SetProjection(ref projection); renderLightPoint.SetLightPosition(ref position); renderLightPoint.SetLightDiffuse(ref lightDiffuse); renderLightPoint.SetLightSpecular(ref lightSpecular); renderLightPoint.Range = drawLight.Range; renderLightPoint.Intensity = drawLight.Intensity; renderLightPoint.SpecularTexture = demo.SpecularTexture; renderLightPoint.NormalTexture = demo.NormalTexture; renderLightPoint.DepthTexture = demo.DepthTexture; mesh = demo.Meshes[lightPhysicsObject.UserDataStr]; mesh.Draw(renderLightPoint); } else if (drawLight.Type == PhysicsLightType.Spot) { lightPhysicsObject.EnableAddToCameraDrawTransparentPhysicsObjects = false; if (enableDrawLights) { lightPhysicsObject.EnableAddToCameraDrawTransparentPhysicsObjects = true; lightPhysicsObject.Material.TransparencyFactor = 0.5f; lightPhysicsObject.Material.TransparencySecondPass = false; } GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Front); lightPhysicsObject.MainWorldTransform.GetPosition(ref position); lightPhysicsObject.MainWorldTransform.GetTransformMatrix(ref world); drawLight.GetDiffuse(ref lightDiffuse); drawLight.GetSpecular(ref lightSpecular); direction.X = -world.Row2.X; direction.Y = -world.Row2.Y; direction.Z = -world.Row2.Z; Vector3.Subtract(ref position, ref direction, out position); renderLightSpot.Enable = true; renderLightSpot.Width = objectBase.Camera.Projection.Width; renderLightSpot.Height = objectBase.Camera.Projection.Height; renderLightSpot.SetWorld(ref world); renderLightSpot.SetView(ref view); renderLightSpot.SetProjection(ref projection); renderLightSpot.SetLightPosition(ref position); renderLightSpot.SetLightDirection(ref direction); renderLightSpot.SetLightDiffuse(ref lightDiffuse); renderLightSpot.SetLightSpecular(ref lightSpecular); renderLightSpot.Range = drawLight.Range; renderLightSpot.Intensity = drawLight.Intensity; renderLightSpot.InnerRadAngle = drawLight.SpotInnerRadAngle; renderLightSpot.OuterRadAngle = drawLight.SpotOuterRadAngle; renderLightSpot.SpecularTexture = demo.SpecularTexture; renderLightSpot.NormalTexture = demo.NormalTexture; renderLightSpot.DepthTexture = demo.DepthTexture; mesh = demo.Meshes[lightPhysicsObject.UserDataStr]; mesh.Draw(renderLightSpot); } } if (!demo.EnableMenu) { GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); } else { GL.BindFramebuffer(FramebufferTarget.Framebuffer, demo.ScreenFrameBuffer); GL.DrawBuffer(DrawBufferMode.ColorAttachment0); } GL.Disable(EnableCap.CullFace); GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.Zero); GL.BlendEquation(BlendEquationMode.FuncAdd); GL.Disable(EnableCap.Blend); renderScreen.Width = objectBase.Camera.Projection.Width; renderScreen.Height = objectBase.Camera.Projection.Height; renderScreen.ColorTexture = demo.ColorTexture; renderScreen.LightTexture = demo.LightTexture; quad.Draw(renderScreen); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.DepthTest); GL.DepthMask(true); }
public void RefreshControllers() { oldKeyboardState = demo.GetKeyboardState(); }
public void DrawSky(DrawMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (keyboardState[Key.J]) { scene.Light.RotationDegAngleX += 0.5f; render.SunRotationDegAngleX = scene.Light.RotationDegAngleX; render.UpdateSunParameters(); } if (keyboardState[Key.K]) { scene.Light.RotationDegAngleX -= 0.5f; render.SunRotationDegAngleX = scene.Light.RotationDegAngleX; render.UpdateSunParameters(); } curTime = DateTime.Now.Hour * 60 + DateTime.Now.Minute; if (curTime != oldTime) { oldTime = curTime; // Sun position update based on the current time //scene.Light.RotationDegAngleX = curTime * 0.25f - 90.0f; //render.SunRotationDegAngleX = scene.Light.RotationDegAngleX; //render.UpdateSunParameters(); } render.GetLightDirection(ref direction); render.GetSunColor(ref diffuse); Vector3.Multiply(ref diffuse, 0.5f, out diffuse); Vector3.Multiply(ref diffuse, 0.4f, out specular); scene.Light.SetDirection(ref direction); scene.Light.SetDiffuse(ref diffuse); scene.Light.SetSpecular(ref specular); PhysicsObject physicsObjectWithActiveCamera = scene.GetPhysicsObjectWithActiveCamera(0); if (physicsObjectWithActiveCamera == null) { return; } PhysicsCamera activeCamera = physicsObjectWithActiveCamera.Camera; if (activeCamera == null) { return; } if (demo.EnableWireframe) { return; } float time = args.Time; activeCamera.View.GetViewMatrix(ref view); activeCamera.Projection.GetProjectionMatrix(ref projection); if (objectBase.UserDataStr != null) { DemoMesh mesh = demo.Meshes[objectBase.UserDataStr]; targets[0] = DrawBuffersEnum.ColorAttachment0; targets[1] = DrawBuffersEnum.None; targets[2] = DrawBuffersEnum.None; targets[3] = DrawBuffersEnum.None; GL.DrawBuffers(4, targets); GL.CullFace(mesh.CullMode); GL.Disable(EnableCap.Texture2D); GL.DepthMask(false); physicsObjectWithActiveCamera.MainWorldTransform.GetPosition(ref position); Matrix4.CreateTranslation(ref position, out translation); objectBase.MainWorldTransform.GetTransformMatrix(ref transform); Matrix4.Mult(ref transform, ref translation, out world); //Matrix4.Mult(ref world, ref view, out transform); //Matrix4.Mult(ref transform, ref projection, out world); //render.SetWorldViewProjection(ref world); //render.Texture = mesh.DemoTexture.Handle; render.SetWorld(ref world); render.SetView(ref view); render.SetProjection(ref projection); mesh.Draw(render); GL.DepthMask(true); GL.Enable(EnableCap.Texture2D); targets[0] = DrawBuffersEnum.ColorAttachment0; targets[1] = DrawBuffersEnum.ColorAttachment1; targets[2] = DrawBuffersEnum.ColorAttachment2; targets[3] = DrawBuffersEnum.ColorAttachment3; GL.DrawBuffers(4, targets); } }
void Move(SimulateMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); float time = args.Time; PhysicsObject physicsObjectWithActiveCamera = scene.GetPhysicsObjectWithActiveCamera(0); if (physicsObjectWithActiveCamera == null) { return; } PhysicsObject cameraBody = physicsObjectWithActiveCamera.RigidGroupOwner.FindChildPhysicsObject(cameraBodyName, true, true); PhysicsObject cameraDown = physicsObjectWithActiveCamera.RigidGroupOwner.FindChildPhysicsObject(cameraDownName, true, true); if ((bodyBox0 == null) || (engineSwitch == null) || (cameraBody == null) || (cameraDown == null)) { return; } Constraint CameraConstraint = objectBase.Scene.Factory.ConstraintManager.Find(cameraConstraintName); bool rotorWorking = true; if (constraint2 != null) { if (constraint2.PhysicsObject1 != null) { if (constraint2.PhysicsObject1.IsBrokenRigidGroup || constraint2.IsBroken) { rotorWorking = false; } } if (constraint2.PhysicsObject2 != null) { if (constraint2.PhysicsObject2.IsBrokenRigidGroup || constraint2.IsBroken) { rotorWorking = false; } } } else { rotorWorking = false; } if (objectBase.IsBrokenRigidGroup || !rotorWorking) { if (constraint1 != null) { constraint1.EnableControlAngleY = false; } if ((CameraConstraint != null) && (CameraConstraint.PhysicsObject1 == cameraDown)) { objectBase.Scene.Factory.ConstraintManager.Remove(CameraConstraint); physicsObjectWithActiveCamera.Camera.EnableControl = false; cameraDown.RigidGroupOwner.MaxPreUpdateAngularVelocity = 0.0f; cameraDown.RigidGroupOwner.MaxPostUpdateAngularVelocity = 0.0f; Vector3 euler = vectorZero; Vector3 cameraEuler = vectorZero; Vector3 objectEuler = vectorZero; physicsObjectWithActiveCamera.Camera.GetEuler(ref cameraEuler); physicsObjectWithActiveCamera.MainWorldTransform.GetTransposeRotation(ref objectRotation); physicsObjectWithActiveCamera.InitLocalTransform.GetRotation(ref objectInitRotation); Matrix4.Mult(ref objectRotation, ref objectInitRotation, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref rotation); physicsObjectWithActiveCamera.Camera.GetEuler(ref objectEuler); Vector3.Add(ref objectEuler, ref cameraEuler, out euler); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref euler); physicsObjectWithActiveCamera.Camera.SetRotation(ref rotation); Matrix4.CreateRotationY(euler.Y, out rotation); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); physicsObjectWithActiveCamera.RigidGroupOwner.RecalculateMainTransform(); } return; } DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (physicsObjectWithActiveCamera.Camera.EnableControl && (CameraConstraint != null) && (CameraConstraint.PhysicsObject1 == cameraDown)) { if (keyboardState[Key.Space] && !oldKeyboardState[Key.Space]) { if (constraint1 != null) { constraint1.EnableControlAngleY = false; } objectBase.Scene.Factory.ConstraintManager.Remove(CameraConstraint); physicsObjectWithActiveCamera.Camera.EnableControl = false; cameraDown.RigidGroupOwner.MaxPreUpdateAngularVelocity = 0.0f; cameraDown.RigidGroupOwner.MaxPostUpdateAngularVelocity = 0.0f; Vector3 euler = vectorZero; Vector3 cameraEuler = vectorZero; Vector3 objectEuler = vectorZero; physicsObjectWithActiveCamera.Camera.GetEuler(ref cameraEuler); physicsObjectWithActiveCamera.MainWorldTransform.GetTransposeRotation(ref objectRotation); physicsObjectWithActiveCamera.InitLocalTransform.GetRotation(ref objectInitRotation); Matrix4.Mult(ref objectRotation, ref objectInitRotation, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref rotation); physicsObjectWithActiveCamera.Camera.GetEuler(ref objectEuler); Vector3.Add(ref objectEuler, ref cameraEuler, out euler); euler.Z = 0.0f; Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref euler); physicsObjectWithActiveCamera.Camera.SetRotation(ref rotation); Matrix4.CreateRotationY(euler.Y, out rotation); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); physicsObjectWithActiveCamera.RigidGroupOwner.RecalculateMainTransform(); oldKeyboardState = keyboardState; return; } if (keyboardState[Key.W]) { if ((engine != null) && (engineRotor != null)) { Vector3.Multiply(ref unitZ, -10.0f, out velocity); engineRotor.MainWorldTransform.SetLocalAngularVelocity(ref velocity); if (engineRotor.IsUnderFluidSurface) { Vector3.Multiply(ref unitZ, 5.0f, out velocity); engine.MainWorldTransform.AddLocalLinearVelocity(ref velocity); } } } if (keyboardState[Key.S]) { if ((engine != null) && (engineRotor != null)) { Vector3.Multiply(ref unitZ, 10.0f, out velocity); engineRotor.MainWorldTransform.SetLocalAngularVelocity(ref velocity); if (engineRotor.IsUnderFluidSurface) { Vector3.Multiply(ref unitZ, -5.0f, out velocity); engine.MainWorldTransform.AddLocalLinearVelocity(ref velocity); } } } if (keyboardState[Key.D]) { if (constraint1 != null) { constraint1.ControlDegAngleY += 0.5f; } } if (keyboardState[Key.A]) { if (constraint1 != null) { constraint1.ControlDegAngleY -= 0.5f; } } } else { if (constraint1 != null) { constraint1.EnableControlAngleY = false; } if (engineSwitch.IsColliding(cameraBody)) { if (keyboardState[Key.Space] && !oldKeyboardState[Key.Space]) { physicsObjectWithActiveCamera.Camera.EnableControl = true; if (constraint1 != null) { constraint1.EnableControlAngleY = true; } constraint1.ControlDegAngleY = 0.0f; cameraDown.RigidGroupOwner.MaxPreUpdateAngularVelocity = 1000.0f; cameraDown.RigidGroupOwner.MaxPostUpdateAngularVelocity = 1000.0f; Quaternion cameraOrientationX = quaternionIdentity; Quaternion cameraOrientationY = quaternionIdentity; Quaternion cameraOrientationZ = quaternionIdentity; Quaternion cameraOrientationXY = quaternionIdentity; Quaternion cameraOrientation = quaternionIdentity; Quaternion.Multiply(ref cameraOrientationX, ref cameraOrientationY, out cameraOrientationXY); Quaternion.Multiply(ref cameraOrientationXY, ref cameraOrientationZ, out cameraOrientation); rotation = Matrix4.CreateFromQuaternion(cameraOrientation); physicsObjectWithActiveCamera.Camera.SetOrientation(ref cameraOrientation); physicsObjectWithActiveCamera.Camera.SetRotation(ref rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref rotation); objectOrientation = quaternionIdentity; objectRotation = Matrix4.CreateFromQuaternion(objectOrientation); Vector3 boatDeckPosition = vectorZero; Matrix4 boatDeckRotation = matrixIdentity; bodyBox0.MainWorldTransform.GetPosition(ref boatDeckPosition); bodyBox0.MainWorldTransform.GetRotation(ref boatDeckRotation); Matrix4.Mult(ref objectRotation, ref boatDeckRotation, out rotation); Vector3.TransformVector(ref driverLocalPosition, ref boatDeckRotation, out position); Vector3.Add(ref boatDeckPosition, ref position, out position); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetPosition(ref position); physicsObjectWithActiveCamera.RigidGroupOwner.RecalculateMainTransform(); if (CameraConstraint == null) { CameraConstraint = scene.Factory.ConstraintManager.Create(cameraConstraintName); CameraConstraint.PhysicsObject1 = cameraDown; CameraConstraint.PhysicsObject2 = bodyBox0; CameraConstraint.PhysicsObject1.MainWorldTransform.GetPosition(ref position1); CameraConstraint.PhysicsObject1.MainWorldTransform.GetOrientation(ref orientation1); CameraConstraint.PhysicsObject2.MainWorldTransform.GetOrientation(ref orientation2); boatDeckPosition.X = boatDeckPosition.Z = 0.0f; boatDeckPosition.Y = 2.0f; Vector3.Subtract(ref position1, ref boatDeckPosition, out position1); CameraConstraint.SetAnchor1(ref position1); CameraConstraint.SetAnchor2(ref position1); CameraConstraint.SetInitWorldOrientation1(ref orientation1); CameraConstraint.SetInitWorldOrientation2(ref orientation2); CameraConstraint.EnableLimitAngleX = true; CameraConstraint.EnableLimitAngleY = true; CameraConstraint.EnableLimitAngleZ = true; CameraConstraint.LimitAngleForce = 0.6f; CameraConstraint.Update(); } } } if (enableExternalMoving) { if (keyboardState[Key.Up]) { if (constraint1 != null) { constraint1.EnableControlAngleY = true; } if ((engine != null) && (engineRotor != null)) { Vector3.Multiply(ref unitZ, -10.0f, out velocity); engineRotor.MainWorldTransform.SetLocalAngularVelocity(ref velocity); if (engineRotor.IsUnderFluidSurface) { Vector3.Multiply(ref unitZ, 5.0f, out velocity); engine.MainWorldTransform.AddLocalLinearVelocity(ref velocity); } } } if (keyboardState[Key.Down]) { if (constraint1 != null) { constraint1.EnableControlAngleY = true; } if ((engine != null) && (engineRotor != null)) { Vector3.Multiply(ref unitZ, 10.0f, out velocity); engineRotor.MainWorldTransform.SetLocalAngularVelocity(ref velocity); if (engineRotor.IsUnderFluidSurface) { Vector3.Multiply(ref unitZ, -5.0f, out velocity); engine.MainWorldTransform.AddLocalLinearVelocity(ref velocity); } } } if (keyboardState[Key.Right]) { if (constraint1 != null) { constraint1.EnableControlAngleY = true; } if (constraint1 != null) { constraint1.ControlDegAngleY += 0.5f; } } if (keyboardState[Key.Left]) { if (constraint1 != null) { constraint1.EnableControlAngleY = true; } if (constraint1 != null) { constraint1.ControlDegAngleY -= 0.5f; } } } } oldKeyboardState = keyboardState; }
public void Move(SimulateMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); if (!objectBase.Camera.Enabled) { return; } if (!objectBase.Camera.Active) { return; } float time = (float)args.Time; Vector3 deltaRotation = vectorZero; Vector3 deltaTranslation = vectorZero; float rotationSpeed = 8.0f; float translationSpeed = 8.0f; bool enableShot = false; DemoMouseState mouseState = demo.GetMouseState(); DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (mouseState[MouseButton.Right]) { deltaRotation.Y += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.X - oldMouseState.X) * time); deltaRotation.X += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.Y - oldMouseState.Y) * time); } if (mouseState[MouseButton.Middle] && !oldMouseState[MouseButton.Middle]) { enableShot = true; } if ((keyboardState[Key.ControlRight] && !oldKeyboardState[Key.ControlRight]) || (keyboardState[Key.ControlLeft] && !oldKeyboardState[Key.ControlLeft])) { enableShot = true; } if (keyboardState[Key.W]) { deltaTranslation.Z += translationSpeed * time; } if (keyboardState[Key.S]) { deltaTranslation.Z -= translationSpeed * time; } if (keyboardState[Key.D]) { deltaTranslation.X += translationSpeed * time; } if (keyboardState[Key.A]) { deltaTranslation.X -= translationSpeed * time; } oldMouseState = mouseState; oldKeyboardState = keyboardState; if (deltaRotation.LengthSquared != 0.0f) { Vector3 euler = vectorZero; objectBase.Camera.GetEuler(ref euler); Vector3.Add(ref euler, ref deltaRotation, out euler); objectBase.Camera.SetEuler(ref euler); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out cameraRotation); objectBase.Camera.SetRotation(ref cameraRotation); objectBase.MainWorldTransform.SetTransposeRotation(ref cameraRotation); objectBase.RecalculateMainTransform(); } if (deltaTranslation.LengthSquared != 0.0f) { objectBase.MainWorldTransform.GetRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out direction); objectBase.MainWorldTransform.GetPosition(ref position); Vector3.Add(ref position, ref direction, out position); objectBase.MainWorldTransform.SetPosition(ref position); objectBase.RecalculateMainTransform(); } objectBase.Camera.Projection.CreatePerspectiveLH(1.0f, 11000.0f, 70.0f, demo.WindowWidth, demo.WindowHeight); objectBase.MainWorldTransform.GetPosition(ref position); objectBase.Camera.GetTransposeRotation(ref cameraRotation); objectBase.Camera.View.CreateLookAtLH(ref position, ref cameraRotation, 0.0f); objectBase.Camera.UpdateFrustum(); if (enableShot) { Vector3 shotScale, shotColor; string frameCountName = scene.SimulationFrameCount.ToString(); PhysicsObject shot = scene.Factory.PhysicsObjectManager.Create(shotName + frameCountName); PhysicsObject shotBase = scene.Factory.PhysicsObjectManager.Create(shotBaseName + frameCountName); PhysicsObject shotLight = scene.Factory.PhysicsObjectManager.Create(shotLightName + frameCountName); shot.AddChildPhysicsObject(shotBase); shot.AddChildPhysicsObject(shotLight); shotScale = shotColor = vectorZero; shotScale.X = shotScale.Y = shotScale.Z = 0.5f; objectBase.MainWorldTransform.GetPosition(ref position); objectBase.Camera.GetTransposeRotation(ref cameraRotation); direction.X = cameraRotation.Row2.X; direction.Y = cameraRotation.Row2.Y; direction.Z = cameraRotation.Row2.Z; Vector3.Multiply(ref direction, 300.0f, out direction); shot.InitLocalTransform.SetRotation(ref matrixIdentity); shot.InitLocalTransform.SetPosition(ref position); shot.InitLocalTransform.SetLinearVelocity(ref direction); shot.InitLocalTransform.SetAngularVelocity(ref vectorZero); shot.MaxSimulationFrameCount = 200; //shot.EnableLocalGravity = true; shotBase.Shape = sphere; shotBase.UserDataStr = sphereName; shotBase.InitLocalTransform.SetScale(ref shotScale); shotBase.Integral.SetDensity(10.0f); shotBase.Material.RigidGroup = true; shotBase.EnableBreakRigidGroup = false; shotBase.EnableCollisions = true; shotBase.DisableCollision(objectBase, true); shotBase.MaxDisableCollisionFrameCount = 50; shotLight.Shape = sphere; shotLight.UserDataStr = sphereName; shotLight.CreateLight(true); shotLight.Light.Type = PhysicsLightType.Point; shotLight.Light.Range = 20.0f; shotColor.X = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Y = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Z = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotLight.Light.SetDiffuse(ref shotColor); shotLight.InitLocalTransform.SetScale(20.0f); shotLight.Material.RigidGroup = true; shotLight.EnableBreakRigidGroup = false; shotLight.EnableCollisions = false; shotLight.EnableCursorInteraction = false; shotLight.EnableAddToCameraDrawTransparentPhysicsObjects = false; scene.UpdateFromInitLocalTransform(shot); } objectBase.Camera.UpdatePhysicsObjects(true, true, true); objectBase.Camera.SortDrawPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); objectBase.Camera.SortTransparentPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); }
public void Move(SimulateMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); if (!objectBase.Camera.Enabled) { return; } if (!objectBase.Camera.Active) { return; } float time = (float)args.Time; Vector3 deltaRotation = vectorZero; Vector3 deltaTranslation = vectorZero; float rotationSpeed = 8.0f; float translationSpeed = 8.0f; float jumpSpeed = 8.0f; float swimUpSpeed = 0.2f; float swimUpOnSurfaceSpeed = 0.06f; float translationInFluidFactor = 0.15f; float soundPositionFactor = 0.1f; bool enableJump = false; bool enableSwimUp = false; bool enableShot = false; bool cameraBodyCollision = cameraBody.IsColliding(); bool cameraDownCollision = cameraDown.IsColliding(); DemoMouseState mouseState = demo.GetMouseState(); DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (mouseState[MouseButton.Right]) { deltaRotation.Y += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.X - oldMouseState.X) * time); deltaRotation.X += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.Y - oldMouseState.Y) * time); } mousePosition.X = mouseState.X; mousePosition.Y = mouseState.Y; if (!objectBase.Camera.EnableControl) { if (mouseState[MouseButton.Middle] && !oldMouseState[MouseButton.Middle]) { enableShot = true; } if ((keyboardState[Key.ControlRight] && !oldKeyboardState[Key.ControlRight]) || (keyboardState[Key.ControlLeft] && !oldKeyboardState[Key.ControlLeft])) { enableShot = true; } } PhysicsObject cursorA = scene.Factory.PhysicsObjectManager.Find(cursorAName); PhysicsObject cursorB = scene.Factory.PhysicsObjectManager.Find(cursorBName); if (!demo.EnableMenu) { if (cursorA != null) { cursorA.EnableDrawing = true; } if (cursorB != null) { cursorB.EnableDrawing = true; } } else { if (cursorA != null) { cursorA.EnableDrawing = false; } if (cursorB != null) { cursorB.EnableDrawing = false; } } if (!objectBase.Camera.EnableControl) { if (keyboardState[Key.W]) { deltaTranslation.Z += translationSpeed * time; } if (keyboardState[Key.S]) { deltaTranslation.Z -= translationSpeed * time; } if (keyboardState[Key.D]) { deltaTranslation.X += translationSpeed * time; } if (keyboardState[Key.A]) { deltaTranslation.X -= translationSpeed * time; } if (keyboardState[Key.Space] && !oldKeyboardState[Key.Space]) { enableJump = true; } if (keyboardState[Key.Space]) { enableSwimUp = true; } } if (keyboardState[Key.Tab] && !oldKeyboardState[Key.Tab]) { enableDistance = !enableDistance; } oldMouseState = mouseState; oldKeyboardState = keyboardState; Vector3 gravityDirection = vectorZero; scene.GetGravityDirection(ref gravityDirection); if (!objectBase.Camera.EnableControl) { if (deltaRotation.LengthSquared != 0.0f) { Vector3 euler = vectorZero; objectBase.Camera.GetEuler(ref euler); Vector3.Add(ref euler, ref deltaRotation, out euler); objectBase.Camera.SetEuler(ref euler); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out cameraRotation); objectBase.Camera.SetRotation(ref cameraRotation); Matrix4.CreateRotationY(euler.Y, out rotation); objectBase.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); objectBase.RigidGroupOwner.RecalculateMainTransform(); } } else { Vector3 euler = vectorZero; Matrix4 objectRotation = matrixIdentity; objectBase.Camera.GetEuler(ref euler); Vector3.Add(ref euler, ref deltaRotation, out euler); objectBase.RigidGroupOwner.MainWorldTransform.GetRotation(ref objectRotation); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(euler.X, out rotationX); Matrix4.CreateRotationY(euler.Y, out rotationY); Matrix4.Mult(ref rotationX, ref rotationY, out cameraRotation); Matrix4.Mult(ref cameraRotation, ref objectRotation, out rotation); objectBase.Camera.SetEuler(ref euler); objectBase.Camera.SetTransposeRotation(ref rotation); } if (deltaTranslation.LengthSquared != 0.0f) { if (objectBase.RigidGroupOwner.IsUnderFluidSurface) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 10.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 10.0f; if (enableSwimUp) { objectBase.InitLocalTransform.GetTransposeRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out direction); objectBase.MainWorldTransform.GetRotation(ref rotation); Vector3.TransformVector(ref direction, ref rotation, out moveForce); Vector3.Multiply(ref moveForce, translationInFluidFactor * objectBase.RigidGroupOwner.Integral.Mass / (time * time), out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); } else { objectBase.Camera.GetTransposeRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out moveForce); Vector3.Multiply(ref moveForce, translationInFluidFactor * objectBase.RigidGroupOwner.Integral.Mass / (time * time), out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); } } else { if (cameraDownCollision) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 100000.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 100000.0f; objectBase.InitLocalTransform.GetTransposeRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out direction); objectBase.MainWorldTransform.GetRotation(ref rotation); Vector3.TransformVector(ref direction, ref rotation, out moveForce); Vector3.Multiply(ref moveForce, objectBase.RigidGroupOwner.Integral.Mass / (time * time), out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); cameraDown.UpdateFeedbackForce(ref moveForce); } else { if (objectBase.RigidGroupOwner.IsInFluid) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 10.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 10.0f; objectBase.InitLocalTransform.GetTransposeRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out direction); objectBase.MainWorldTransform.GetRotation(ref rotation); Vector3.TransformVector(ref direction, ref rotation, out moveForce); Vector3.Multiply(ref moveForce, translationInFluidFactor * objectBase.RigidGroupOwner.Integral.Mass / (time * time), out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); } } } } if (enableSwimUp) { if (cameraDown.IsUnderFluidSurface && cameraBody.IsUnderFluidSurface) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 10.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 10.0f; PhysicsObject fluidPhysicsObject = objectBase.RigidGroupOwner.FluidPhysicsObject; fluidPhysicsObject.InternalControllers.FluidController.GetNormal(ref fluidNormal); Vector3.Multiply(ref fluidNormal, swimUpSpeed * objectBase.RigidGroupOwner.Integral.Mass / time, out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); } else if (!cameraDownCollision && cameraBody.IsInFluid && (deltaTranslation.LengthSquared == 0.0f)) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 10.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 10.0f; PhysicsObject fluidPhysicsObject = objectBase.RigidGroupOwner.FluidPhysicsObject; fluidPhysicsObject.InternalControllers.FluidController.GetNormal(ref fluidNormal); Vector3.Multiply(ref fluidNormal, swimUpOnSurfaceSpeed * objectBase.RigidGroupOwner.Integral.Mass / time, out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); } } if (enableJump) { if (!enableControl && !objectBase.Camera.EnableControl && cameraDownCollision && !cameraDown.IsUnderFluidSurface && !cameraBody.IsUnderFluidSurface) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 100000.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 100000.0f; Vector3.Multiply(ref gravityDirection, -jumpSpeed * objectBase.RigidGroupOwner.Integral.Mass / time, out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); cameraDown.UpdateFeedbackForce(ref moveForce); } } if (enableDistance) { if (distance > maxDistance) { distance -= 2.0f; } if (enableDistanceCollision) { float margin = 1.0f; objectBase.MainWorldTransform.GetPosition(ref startPoint); objectBase.Camera.GetTransposeRotation(ref cameraRotation); direction.X = cameraRotation.Row2.X; direction.Y = cameraRotation.Row2.Y; direction.Z = cameraRotation.Row2.Z; Vector3.Multiply(ref direction, distance, out direction); Vector3.Add(ref startPoint, ref direction, out endPoint); scene.UpdatePhysicsObjectsIntersectedBySegment(ref startPoint, ref endPoint, margin, true); float minDistance = float.MaxValue; float curDistance = 0.0f; for (int i = 0; i < scene.IntersectedPhysicsObjectsCount; i++) { PhysicsObject hitObject = scene.GetIntersectedPhysicsObject(i, ref hitPoint); if (hitObject.RigidGroupOwner == objectBase.RigidGroupOwner) { continue; } //if ((hitObject.InternalControllers.FluidController != null) && hitObject.InternalControllers.FluidController.Enabled) // continue; if (!hitObject.EnableCollisions) { continue; } Vector3.Subtract(ref startPoint, ref hitPoint, out hitDistance); curDistance = hitDistance.Length; if (curDistance < minDistance) { minDistance = curDistance; } } if (minDistance < Math.Abs(distance)) { distance = -minDistance; } } } else { if (distance < 0.0f) { distance += 2.0f; } if (distance > 0.0f) { distance = 0.0f; } } if (enableDistance) { if (distance > maxDistance) { if ((cameraUp != null) && (cameraBody != null) && (cameraDown != null)) { objectBase.RigidGroupOwner.EnableDrawing = true; cameraUp.EnableDrawing = true; cameraBody.EnableDrawing = true; cameraDown.EnableDrawing = true; } } } else { if (distance >= 0.0f) { if ((cameraUp != null) && (cameraBody != null) && (cameraDown != null)) { objectBase.RigidGroupOwner.EnableDrawing = false; cameraUp.EnableDrawing = false; cameraBody.EnableDrawing = false; cameraDown.EnableDrawing = false; } } } enableControl = objectBase.Camera.EnableControl; float gravityDistance = 0.0f; Vector3 gravityLinearVelocity = vectorZero; Vector3 tangentLinearVelocity = vectorZero; Vector3 velocity = vectorZero; objectBase.MainWorldTransform.GetLinearVelocity(ref velocity); Vector3.Dot(ref gravityDirection, ref velocity, out gravityDistance); Vector3.Multiply(ref gravityDirection, gravityDistance, out gravityLinearVelocity); Vector3.Subtract(ref velocity, ref gravityLinearVelocity, out tangentLinearVelocity); float tangentLength = tangentLinearVelocity.Length; if (tangentLength > maxTangentLength) { tangentLinearVelocity *= maxTangentLength / tangentLength; } Vector3.Add(ref gravityLinearVelocity, ref tangentLinearVelocity, out velocity); objectBase.RigidGroupOwner.MainWorldTransform.SetLinearVelocity(ref velocity); objectBase.Camera.Projection.CreatePerspectiveLH(1.0f, 11000.0f, 70.0f, demo.WindowWidth, demo.WindowHeight); objectBase.MainWorldTransform.GetPosition(ref position); objectBase.Camera.GetTransposeRotation(ref cameraRotation); objectBase.Camera.View.CreateLookAtLH(ref position, ref cameraRotation, distance); objectBase.Camera.UpdateFrustum(); objectBase.Camera.View.GetViewMatrix(ref view); objectBase.Camera.Projection.GetProjectionMatrix(ref projection); Vector3 rayPosition, rayDirection; rayPosition = rayDirection = vectorZero; objectBase.UnProjectToRay(ref mousePosition, 0, 0, demo.WindowWidth, demo.WindowHeight, 0.0f, 1.0f, ref view, ref matrixIdentity, ref projection, ref rayPosition, ref rayDirection); PhysicsObject cursor = scene.Factory.PhysicsObjectManager.Find(cursorName); if (cursor != null) { Vector3 cursorPosition = vectorZero; Matrix4 cursorLocalRotation = matrixIdentity; Matrix4 cursorWorldRotation = matrixIdentity; cursor.InitLocalTransform.GetPosition(ref cursorPosition); cursor.InitLocalTransform.GetRotation(ref cursorLocalRotation); cursor.MainWorldTransform.GetRotation(ref cursorWorldRotation); objectBase.Camera.GetTransposeRotation(ref cameraRotation); Matrix4.Mult(ref cursorLocalRotation, ref cameraRotation, out rotation); Vector3.TransformVector(ref cursorPosition, ref cursorWorldRotation, out position); cursor.MainWorldTransform.SetRotation(ref rotation); Vector3.Add(ref position, ref rayPosition, out position); Vector3.Add(ref position, ref rayDirection, out position); cursor.MainWorldTransform.SetPosition(ref position); cursor.RecalculateMainTransform(); } CursorController cursorController = objectBase.InternalControllers.CursorController; if (cursorController.IsDragging) { if (cursorController.HitPhysicsObject.Integral.IsStatic && (cursorController.HitPhysicsObject.InternalControllers.HeightmapController != null) && cursorController.HitPhysicsObject.InternalControllers.HeightmapController.Enabled) { Vector3 cursorStartPosition = vectorZero; Vector3 cursorEndPosition = vectorZero; cursorController.GetAnchor1(ref cursorStartPosition); cursorController.GetAnchor2(ref cursorEndPosition); Vector3.Subtract(ref cursorEndPosition, ref cursorStartPosition, out direction); float dir = direction.Y; if (dir != 0.0f) { Vector3 scale = vectorZero; cursorController.HitPhysicsObject.MainWorldTransform.GetScale(ref scale); float positionX = cursorStartPosition.X + 0.5f * scale.X; float positionY = cursorStartPosition.Y + 0.5f * scale.Y; float positionZ = cursorStartPosition.Z + 0.5f * scale.Z; cursorController.HitPhysicsObject.InternalControllers.HeightmapController.AddHeight(positionX, positionY, positionZ, dir / scale.Y); cursorController.HitPhysicsObject.InternalControllers.HeightmapController.UpdateBounding(); // To change the friction and restitution of the heightmap surface by the cursor, add the following lines of code //cursorController.HitPhysicsObject.InternalControllers.HeightmapController.SetFriction(positionX, positionY, positionZ, 0.0f); //cursorController.HitPhysicsObject.InternalControllers.HeightmapController.SetRestitution(positionX, positionY, positionZ, 2.0f); cursorStartPosition.Y += dir; cursorController.SetAnchor1(ref cursorStartPosition); } } if (cursorController.HitPhysicsObject.Integral.IsStatic && (cursorController.HitPhysicsObject.InternalControllers.HeightmapController != null) && cursorController.HitPhysicsObject.InternalControllers.HeightmapController.Enabled) { Vector3 cursorStartPosition = vectorZero; Vector3 cursorEndPosition = vectorZero; cursorController.GetAnchor1(ref cursorStartPosition); cursorController.GetAnchor2(ref cursorEndPosition); Vector3.Subtract(ref cursorEndPosition, ref cursorStartPosition, out direction); if (direction.LengthSquared != 0.0f) { // To move the heightmap surface by the cursor, add the following lines of code //cursorController.HitPhysicsObject.MainWorldTransform.GetPosition(ref cursorStartPosition); //Vector3.Add(ref cursorStartPosition, ref direction, out cursorStartPosition); //cursorController.HitPhysicsObject.MainWorldTransform.SetPosition(ref cursorStartPosition); //cursorController.HitPhysicsObject.RecalculateMainTransform(); } } if (cursorController.HitPhysicsObject.Integral.IsStatic && (cursorController.HitPhysicsObject.InternalControllers.FluidController != null) && cursorController.HitPhysicsObject.InternalControllers.FluidController.Enabled) { Vector3 cursorStartPosition = vectorZero; Vector3 cursorEndPosition = vectorZero; cursorController.GetAnchor1(ref cursorStartPosition); cursorController.GetAnchor2(ref cursorEndPosition); Vector3.Subtract(ref cursorEndPosition, ref cursorStartPosition, out direction); if (direction.LengthSquared != 0.0f) { // To move the fluid surface by the cursor, add the following lines of code // and set EnableCursorInteraction flag to true in the Lake class //cursorController.HitPhysicsObject.MainWorldTransform.GetPosition(ref cursorStartPosition); //Vector3.Add(ref cursorStartPosition, ref direction, out cursorStartPosition); //cursorController.HitPhysicsObject.MainWorldTransform.SetPosition(ref cursorStartPosition); //cursorController.HitPhysicsObject.RecalculateMainTransform(); } } } objectBase.MainWorldTransform.GetPosition(ref listenerPosition); objectBase.Camera.GetTransposeRotation(ref rotation); Vector3.Multiply(ref listenerPosition, soundPositionFactor, out position); listenerTopDirection.X = rotation.Row1.X; listenerTopDirection.Y = rotation.Row1.Y; listenerTopDirection.Z = rotation.Row1.Z; listenerFrontDirection.X = rotation.Row2.X; listenerFrontDirection.Y = rotation.Row2.Y; listenerFrontDirection.Z = rotation.Row2.Z; listener.Position = position; listener.TopDirection = listenerTopDirection; listener.FrontDirection = listenerFrontDirection; listenerRange = objectBase.Sound.Range; if (enableShot) { Vector3 shotScale, shotColor; shotCount = (shotCount + 1) % shotTab.Length; string shotCountName = shotCount.ToString(); PhysicsObject shot = scene.Factory.PhysicsObjectManager.FindOrCreate(shotName + shotCountName); PhysicsObject shotBase = scene.Factory.PhysicsObjectManager.FindOrCreate(shotBaseName + shotCountName); PhysicsObject shotLight = scene.Factory.PhysicsObjectManager.FindOrCreate(shotLightName + shotCountName); shot.AddChildPhysicsObject(shotBase); shot.AddChildPhysicsObject(shotLight); shotTab[shotCount] = shotBase; enableShotTab = true; shotScale = shotColor = vectorZero; shotScale.X = shotScale.Y = shotScale.Z = 0.5f; Vector3.Multiply(ref rayDirection, 300.0f, out rayDirection); shot.InitLocalTransform.SetRotation(ref matrixIdentity); shot.InitLocalTransform.SetPosition(ref rayPosition); shot.InitLocalTransform.SetLinearVelocity(ref rayDirection); shot.InitLocalTransform.SetAngularVelocity(ref vectorZero); shot.MaxSimulationFrameCount = 200; shot.EnableRemovePhysicsObjectsFromManagerAfterMaxSimulationFrameCount = false; //shot.EnableLocalGravity = true; shotBase.Shape = sphere; shotBase.UserDataStr = sphereName; shotBase.InitLocalTransform.SetScale(ref shotScale); shotBase.Integral.SetDensity(10.0f); shotBase.Material.RigidGroup = true; shotBase.EnableBreakRigidGroup = false; shotBase.EnableCollisions = true; shotBase.CreateSound(true); if ((cameraUp != null) && (cameraBody != null) && (cameraDown != null)) { shotBase.DisableCollision(cameraUp, true); shotBase.DisableCollision(cameraBody, true); shotBase.DisableCollision(cameraDown, true); shotBase.MaxDisableCollisionFrameCount = 50; } shotLight.Shape = sphere; shotLight.UserDataStr = sphereName; shotLight.CreateLight(true); shotLight.Light.Type = PhysicsLightType.Point; shotLight.Light.Range = 20.0f; shotColor.X = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Y = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Z = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotLight.Light.SetDiffuse(ref shotColor); shotLight.InitLocalTransform.SetScale(20.0f); shotLight.Material.RigidGroup = true; shotLight.Material.UserDataStr = yellowName; shotLight.EnableBreakRigidGroup = false; shotLight.EnableCollisions = false; shotLight.EnableCursorInteraction = false; shotLight.EnableAddToCameraDrawTransparentPhysicsObjects = false; scene.UpdateFromInitLocalTransform(shot); shotSoundGroup = demo.SoundGroups[hitName]; shotSoundGroup.MaxHitRepeatTime = 0.0f; if (shotSound == null) { shotSound = shotSoundGroup.GetSound(objectBase.Sound, listener, emitter); } shotSound.Update(time); Vector3.Multiply(ref rayPosition, soundPositionFactor, out shotSound.HitPosition); shotSound.HitVolume = 1.0f; shotSound.FrontDirection.X = rotation.Row2.X; shotSound.FrontDirection.Y = rotation.Row2.Y; shotSound.FrontDirection.Z = rotation.Row2.Z; demo.SoundQueue.EnqueueSound(shotSound); } else { PhysicsObject shotBase; DemoSound shotBaseSound; if (shotSound != null) { shotSound.Update(time); if (shotSound.Stop()) { shotSound.SoundGroup.SetSound(shotSound); shotSound = null; } } if (enableShotTab) { enableShotTab = false; for (int i = 0; i < shotTab.Length; i++) { shotBase = shotTab[i]; if (shotBase != null) { if (shotBase.Sound.UserDataObj != null) { enableShotTab = true; shotBaseSound = (DemoSound)shotBase.Sound.UserDataObj; shotBaseSound.Update(time); if (shotBaseSound.Stop()) { shotBaseSound.SoundGroup.SetSound(shotBaseSound); shotBase.Sound.UserDataObj = null; shotTab[i] = null; } } } } } } objectBase.Camera.UpdatePhysicsObjects(true, true, true); objectBase.Camera.SortDrawPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); objectBase.Camera.SortTransparentPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); objectBase.Camera.SortLightPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); PhysicsObject currentPhysicsObject; PhysicsSound currentSound; DemoSoundGroup soundGroup; DemoSound sound; if (demo.SoundQueue.SoundCount > 0) { return; } for (int i = 0; i < scene.TotalPhysicsObjectCount; i++) { if (demo.SoundQueue.SoundCount >= demo.SoundQueue.MaxSoundCount) { break; } currentPhysicsObject = scene.GetPhysicsObject(i); currentSound = !currentPhysicsObject.EnableSoundFromRigidGroupOwner ? currentPhysicsObject.Sound : currentPhysicsObject.RigidGroupOwner.Sound; if (currentSound == null) { continue; } if (!currentSound.Enabled) { continue; } if (currentSound.UserDataStr == null) { soundGroup = demo.SoundGroups[defaultName]; } else { soundGroup = demo.SoundGroups[currentSound.UserDataStr]; } if (currentPhysicsObject.IsSleeping && (currentSound.UserDataObj == null) && !soundGroup.EnableBackground) { continue; } if (currentPhysicsObject.GetSoundData(ref listenerPosition, listenerRange, soundGroup.EnableHit, soundGroup.EnableRoll, soundGroup.EnableSlide, soundGroup.EnableBackground, currentSound.BackgroundVolumeVelocityModulation, ref hitPosition, ref rollPosition, ref slidePosition, ref backgroundPosition, ref hitVolume, ref rollVolume, ref slideVolume, ref backgroundVolume)) { if (currentSound.UserDataObj == null) { sound = soundGroup.GetSound(currentSound, listener, emitter); currentSound.UserDataObj = sound; } else { sound = (DemoSound)currentSound.UserDataObj; } sound.Update(time); currentPhysicsObject.MainWorldTransform.GetTransposeRotation(ref rotation); Vector3.Multiply(ref hitPosition, soundPositionFactor, out sound.HitPosition); Vector3.Multiply(ref rollPosition, soundPositionFactor, out sound.RollPosition); Vector3.Multiply(ref slidePosition, soundPositionFactor, out sound.SlidePosition); Vector3.Multiply(ref backgroundPosition, soundPositionFactor, out sound.BackgroundPosition); sound.HitVolume = hitVolume; sound.RollVolume = rollVolume; sound.SlideVolume = slideVolume; sound.BackgroundVolume = backgroundVolume; sound.FrontDirection.X = rotation.Row2.X; sound.FrontDirection.Y = rotation.Row2.Y; sound.FrontDirection.Z = rotation.Row2.Z; demo.SoundQueue.EnqueueSound(sound); } else { if (currentSound.UserDataObj != null) { sound = (DemoSound)currentSound.UserDataObj; sound.Update(time); if (sound.Stop()) { soundGroup.SetSound(sound); currentSound.UserDataObj = null; } } } } }
public void Move(SimulateMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); float time = args.Time; if ((turretGun == null) || (turretConstraint == null) || (turretGunConstraint == null)) { return; } Vector3 deltaTranslation = vectorZero; bool turretWorking = true; if (objectBase.IsBrokenRigidGroup || turretConstraint.IsBroken) { turretWorking = false; } bool turretGunWorking = true; if (!turretWorking || turretGun.IsBrokenRigidGroup || turretGunConstraint.IsBroken) { turretGunWorking = false; } DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (keyboardState[Key.Up]) { deltaTranslation.Z += 10000.0f; } if (keyboardState[Key.Down]) { deltaTranslation.Z -= 10000.0f; } if (deltaTranslation.LengthSquared != 0.0f) { objectBase.WorldAccumulator.AddLocalForce(ref deltaTranslation); } if (turretWorking) { if (keyboardState[Key.Right]) { turretConstraint.ControlDegAngleY -= 2.0f; } if (keyboardState[Key.Left]) { turretConstraint.ControlDegAngleY += 2.0f; } } if (turretGunWorking) { if (keyboardState[Key.PageUp]) { turretGunConstraint.ControlDegAngleX += 1.0f; } if (keyboardState[Key.PageDown]) { turretGunConstraint.ControlDegAngleX -= 1.0f; } if (keyboardState[Key.F] && !oldKeyboardState[Key.F]) { shotCount = (shotCount + 1) % maxShotCount; string shotCountName = shotCount.ToString(); if (turretGun1 != null) { shot = scene.Factory.PhysicsObjectManager.FindOrCreate(shot1Name + shotCountName); shotBase = scene.Factory.PhysicsObjectManager.FindOrCreate(shot1BaseName + shotCountName); shotLight = scene.Factory.PhysicsObjectManager.FindOrCreate(shot1LightName + shotCountName); shot.AddChildPhysicsObject(shotBase); shot.AddChildPhysicsObject(shotLight); Vector3 turretGunPosition = vectorZero; Matrix4 turretGunRotation = matrixIdentity; turretGun1.MainWorldTransform.GetPosition(ref turretGunPosition); turretGun1.MainWorldTransform.GetRotation(ref turretGunRotation); Vector3 shotPosition = vectorZero; Vector3 shotLocalPosition = vectorZero; shotLocalPosition.X = 0.0f; shotLocalPosition.Y = 2.0f; shotLocalPosition.Z = 0.0f; Vector3.TransformVector(ref shotLocalPosition, ref turretGunRotation, out shotPosition); Vector3.Add(ref shotPosition, ref turretGunPosition, out shotPosition); Vector3 shotDirection = vectorZero; Vector3 shotScale = vectorZero; shotDirection.X = turretGunRotation.Row1.X; shotDirection.Y = turretGunRotation.Row1.Y; shotDirection.Z = turretGunRotation.Row1.Z; shotScale.X = shotScale.Y = shotScale.Z = 0.5f; shot.InitLocalTransform.SetRotation(ref matrixIdentity); shot.InitLocalTransform.SetPosition(ref shotPosition); Vector3.Multiply(ref shotDirection, 200.0f, out shotDirection); shot.InitLocalTransform.SetLinearVelocity(ref shotDirection); shot.InitLocalTransform.SetAngularVelocity(ref vectorZero); shot.MaxSimulationFrameCount = 100; shot.EnableRemovePhysicsObjectsFromManagerAfterMaxSimulationFrameCount = false; shotBase.Shape = sphere; shotBase.UserDataStr = sphereName; shotBase.Material.RigidGroup = true; shotBase.InitLocalTransform.SetScale(ref shotScale); shotBase.Integral.SetDensity(1.0f); shotBase.EnableCollisions = true; shotBase.DisableCollision(turretGun1, true); shotBase.MaxDisableCollisionFrameCount = 10; shotBase.EnableBreakRigidGroup = false; shotBase.CreateSound(true); shotLight.Shape = sphere; shotLight.UserDataStr = sphereName; shotLight.CreateLight(true); shotLight.Light.Type = PhysicsLightType.Point; shotLight.Light.Range = 20.0f; shotLight.Light.SetDiffuse(1.0f, 0.7f, 0.0f); shotLight.InitLocalTransform.SetScale(20.0f); shotLight.Material.RigidGroup = true; shotLight.Material.UserDataStr = yellowName; shotLight.EnableBreakRigidGroup = false; shotLight.EnableCollisions = false; shotLight.EnableCursorInteraction = false; shotLight.EnableAddToCameraDrawTransparentPhysicsObjects = false; scene.UpdateFromInitLocalTransform(shot); } if (turretGun2 != null) { shot = scene.Factory.PhysicsObjectManager.FindOrCreate(shot2Name + shotCountName); shotBase = scene.Factory.PhysicsObjectManager.FindOrCreate(shot2BaseName + shotCountName); shotLight = scene.Factory.PhysicsObjectManager.FindOrCreate(shot2LightName + shotCountName); shot.AddChildPhysicsObject(shotBase); shot.AddChildPhysicsObject(shotLight); Vector3 turretGunPosition = vectorZero; Matrix4 turretGunRotation = matrixIdentity; turretGun2.MainWorldTransform.GetPosition(ref turretGunPosition); turretGun2.MainWorldTransform.GetRotation(ref turretGunRotation); Vector3 shotPosition = vectorZero; Vector3 shotLocalPosition = vectorZero; shotLocalPosition.X = 0.0f; shotLocalPosition.Y = 2.0f; shotLocalPosition.Z = 0.0f; Vector3.TransformVector(ref shotLocalPosition, ref turretGunRotation, out shotPosition); Vector3.Add(ref shotPosition, ref turretGunPosition, out shotPosition); Vector3 shotDirection = vectorZero; Vector3 shotScale = vectorZero; shotDirection.X = turretGunRotation.Row1.X; shotDirection.Y = turretGunRotation.Row1.Y; shotDirection.Z = turretGunRotation.Row1.Z; shotScale.X = shotScale.Y = shotScale.Z = 0.5f; shot.InitLocalTransform.SetRotation(ref matrixIdentity); shot.InitLocalTransform.SetPosition(ref shotPosition); Vector3.Multiply(ref shotDirection, 200.0f, out shotDirection); shot.InitLocalTransform.SetLinearVelocity(ref shotDirection); shot.InitLocalTransform.SetAngularVelocity(ref vectorZero); shot.MaxSimulationFrameCount = 100; shot.EnableRemovePhysicsObjectsFromManagerAfterMaxSimulationFrameCount = false; shotBase.Shape = sphere; shotBase.UserDataStr = sphereName; shotBase.Material.RigidGroup = true; shotBase.InitLocalTransform.SetScale(ref shotScale); shotBase.Integral.SetDensity(1.0f); shotBase.EnableCollisions = true; shotBase.DisableCollision(turretGun1, true); shotBase.MaxDisableCollisionFrameCount = 10; shotBase.EnableBreakRigidGroup = false; shotBase.CreateSound(true); shotLight.Shape = sphere; shotLight.UserDataStr = sphereName; shotLight.CreateLight(true); shotLight.Light.Type = PhysicsLightType.Point; shotLight.Light.Range = 20.0f; shotLight.Light.SetDiffuse(1.0f, 0.7f, 0.0f); shotLight.InitLocalTransform.SetScale(20.0f); shotLight.Material.RigidGroup = true; shotLight.Material.UserDataStr = yellowName; shotLight.EnableBreakRigidGroup = false; shotLight.EnableCollisions = false; shotLight.EnableCursorInteraction = false; shotLight.EnableAddToCameraDrawTransparentPhysicsObjects = false; scene.UpdateFromInitLocalTransform(shot); } } } else { if (turretGunConstraint.IsBroken) { turretBodyDown.DisableCollision(turretGun1, false); turretBodyDown.DisableCollision(turretGun2, false); } } oldKeyboardState = keyboardState; }
public void Move(SimulateMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); float time = args.Time; PhysicsObject physicsObjectWithActiveCamera = scene.GetPhysicsObjectWithActiveCamera(0); if (physicsObjectWithActiveCamera == null) { return; } PhysicsObject cameraBody = physicsObjectWithActiveCamera.RigidGroupOwner.FindChildPhysicsObject(cameraBodyName, true, true); PhysicsObject cameraDown = physicsObjectWithActiveCamera.RigidGroupOwner.FindChildPhysicsObject(cameraDownName, true, true); if ((chassisMiddleDown == null) || (steeringGearSwitch == null)) { return; } Constraint CameraConstraint = objectBase.Scene.Factory.ConstraintManager.Find(cameraConstraintName); bool steeringGearWorking = true; if (steeringGearConstraint != null) { if (steeringGearConstraint.PhysicsObject1 != null) { if (steeringGearConstraint.PhysicsObject1.IsBrokenRigidGroup || steeringGearConstraint.IsBroken) { steeringGearWorking = false; } } if (steeringGearConstraint.PhysicsObject2 != null) { if (steeringGearConstraint.PhysicsObject2.IsBrokenRigidGroup || steeringGearConstraint.IsBroken) { steeringGearWorking = false; } } } else { steeringGearWorking = false; } if (!steeringGearWorking) { steeringGearDown.DisableCollision(chassisUpPanel, false); steeringGearDown.DisableCollision(chassisDownPanel, false); steeringGearDown.DisableCollision(upBodyFront, false); steeringGearDown.DisableCollision(upBodyDownBack, false); } if (objectBase.IsBrokenRigidGroup || !steeringGearWorking) { if ((CameraConstraint != null) && (CameraConstraint.PhysicsObject1 == cameraDown)) { objectBase.Scene.Factory.ConstraintManager.Remove(CameraConstraint); physicsObjectWithActiveCamera.Camera.EnableControl = false; cameraDown.RigidGroupOwner.MaxPreUpdateAngularVelocity = 0.0f; cameraDown.RigidGroupOwner.MaxPostUpdateAngularVelocity = 0.0f; cameraDown.DisableCollision(steeringWheel, false); cameraDown.DisableCollision(chassisBack, false); cameraDown.DisableCollision(chassisMiddleBack, false); cameraDown.DisableCollision(upBodyFront, false); cameraDown.DisableCollision(upBodyDownBack, false); cameraBody.DisableCollision(steeringGearDown, false); cameraBody.DisableCollision(steeringWheel, false); cameraBody.DisableCollision(chassisBack, false); cameraBody.DisableCollision(chassisMiddleBack, false); cameraBody.DisableCollision(upBodyFront, false); cameraBody.DisableCollision(upBodyDownBack, false); Vector3 euler = vectorZero; Vector3 cameraEuler = vectorZero; Vector3 objectEuler = vectorZero; physicsObjectWithActiveCamera.Camera.GetEuler(ref cameraEuler); physicsObjectWithActiveCamera.MainWorldTransform.GetTransposeRotation(ref objectRotation); physicsObjectWithActiveCamera.InitLocalTransform.GetRotation(ref objectInitRotation); Matrix4.Mult(ref objectRotation, ref objectInitRotation, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref rotation); physicsObjectWithActiveCamera.Camera.GetEuler(ref objectEuler); Vector3.Add(ref objectEuler, ref cameraEuler, out euler); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref euler); physicsObjectWithActiveCamera.Camera.SetRotation(ref rotation); Matrix4.CreateRotationY(euler.Y, out rotation); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); physicsObjectWithActiveCamera.RigidGroupOwner.RecalculateMainTransform(); } return; } DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (physicsObjectWithActiveCamera.Camera.EnableControl && (CameraConstraint != null) && (CameraConstraint.PhysicsObject1 == cameraDown)) { if (keyboardState[Key.Space] && !oldKeyboardState[Key.Space]) { objectBase.Scene.Factory.ConstraintManager.Remove(CameraConstraint); physicsObjectWithActiveCamera.Camera.EnableControl = false; cameraDown.RigidGroupOwner.MaxPreUpdateAngularVelocity = 0.0f; cameraDown.RigidGroupOwner.MaxPostUpdateAngularVelocity = 0.0f; cameraDown.DisableCollision(steeringWheel, false); cameraDown.DisableCollision(chassisBack, false); cameraDown.DisableCollision(chassisMiddleBack, false); cameraDown.DisableCollision(upBodyFront, false); cameraDown.DisableCollision(upBodyDownBack, false); cameraBody.DisableCollision(steeringGearDown, false); cameraBody.DisableCollision(steeringWheel, false); cameraBody.DisableCollision(chassisBack, false); cameraBody.DisableCollision(chassisMiddleBack, false); cameraBody.DisableCollision(upBodyFront, false); cameraBody.DisableCollision(upBodyDownBack, false); Vector3 euler = vectorZero; Vector3 cameraEuler = vectorZero; Vector3 objectEuler = vectorZero; physicsObjectWithActiveCamera.Camera.GetEuler(ref cameraEuler); physicsObjectWithActiveCamera.MainWorldTransform.GetTransposeRotation(ref objectRotation); physicsObjectWithActiveCamera.InitLocalTransform.GetRotation(ref objectInitRotation); Matrix4.Mult(ref objectRotation, ref objectInitRotation, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref rotation); physicsObjectWithActiveCamera.Camera.GetEuler(ref objectEuler); Vector3.Add(ref objectEuler, ref cameraEuler, out euler); euler.Z = 0.0f; Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref euler); physicsObjectWithActiveCamera.Camera.SetRotation(ref rotation); Matrix4.CreateRotationY(euler.Y, out rotation); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); physicsObjectWithActiveCamera.RigidGroupOwner.RecalculateMainTransform(); oldKeyboardState = keyboardState; return; } if (keyboardState[Key.W]) { if (wheel1 != null) { Vector3.Multiply(ref unitY, 5.0f, out velocity); wheel1.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } if (wheel2 != null) { Vector3.Multiply(ref unitY, 5.0f, out velocity); wheel2.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } } if (keyboardState[Key.S]) { if (wheel1 != null) { Vector3.Multiply(ref unitY, -5.0f, out velocity); wheel1.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } if (wheel2 != null) { Vector3.Multiply(ref unitY, -5.0f, out velocity); wheel2.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } } if (keyboardState[Key.D]) { if (steeringGearConstraint != null) { steeringGearConstraint.ControlDegAngleY -= 2.0f; } if (constraint1 != null) { constraint1.ControlDegAngleY += 1.0f; } if (constraint4 != null) { constraint4.ControlDegAngleY += 1.0f; } } if (keyboardState[Key.A]) { if (steeringGearConstraint != null) { steeringGearConstraint.ControlDegAngleY += 2.0f; } if (constraint1 != null) { constraint1.ControlDegAngleY -= 1.0f; } if (constraint4 != null) { constraint4.ControlDegAngleY -= 1.0f; } } } else { if ((cameraBody != null) && steeringGearSwitch.IsColliding(cameraBody)) { if (keyboardState[Key.Space] && !oldKeyboardState[Key.Space]) { physicsObjectWithActiveCamera.Camera.EnableControl = true; cameraDown.RigidGroupOwner.MaxPreUpdateAngularVelocity = 1000.0f; cameraDown.RigidGroupOwner.MaxPostUpdateAngularVelocity = 1000.0f; cameraDown.DisableCollision(steeringWheel, true); cameraDown.DisableCollision(chassisBack, true); cameraDown.DisableCollision(chassisMiddleBack, true); cameraDown.DisableCollision(upBodyFront, true); cameraDown.DisableCollision(upBodyDownBack, true); cameraBody.DisableCollision(steeringGearDown, true); cameraBody.DisableCollision(steeringWheel, true); cameraBody.DisableCollision(chassisBack, true); cameraBody.DisableCollision(chassisMiddleBack, true); cameraBody.DisableCollision(upBodyFront, true); cameraBody.DisableCollision(upBodyDownBack, true); Quaternion cameraOrientationX = quaternionIdentity; Quaternion cameraOrientationY = quaternionIdentity; Quaternion cameraOrientationZ = quaternionIdentity; Quaternion cameraOrientationXY = quaternionIdentity; Quaternion cameraOrientation = quaternionIdentity; Quaternion.Multiply(ref cameraOrientationX, ref cameraOrientationY, out cameraOrientationXY); Quaternion.Multiply(ref cameraOrientationXY, ref cameraOrientationZ, out cameraOrientation); rotation = Matrix4.CreateFromQuaternion(cameraOrientation); physicsObjectWithActiveCamera.Camera.SetOrientation(ref cameraOrientation); physicsObjectWithActiveCamera.Camera.SetRotation(ref rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref rotation); objectOrientation = Quaternion.FromAxisAngle(unitY, MathHelper.DegreesToRadians(180.0f)); objectRotation = Matrix4.CreateFromQuaternion(objectOrientation); Vector3 carChassisPosition = vectorZero; Matrix4 carChassisRotation = matrixIdentity; chassisMiddleDown.MainWorldTransform.GetPosition(ref carChassisPosition); chassisMiddleDown.MainWorldTransform.GetRotation(ref carChassisRotation); Matrix4.Mult(ref objectRotation, ref carChassisRotation, out rotation); Vector3.TransformVector(ref driverLocalPosition, ref carChassisRotation, out position); Vector3.Add(ref carChassisPosition, ref position, out position); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetPosition(ref position); physicsObjectWithActiveCamera.RigidGroupOwner.RecalculateMainTransform(); if (CameraConstraint == null) { CameraConstraint = scene.Factory.ConstraintManager.Create(cameraConstraintName); CameraConstraint.PhysicsObject1 = cameraDown; CameraConstraint.PhysicsObject2 = chassisMiddleDown; CameraConstraint.PhysicsObject1.MainWorldTransform.GetPosition(ref position1); CameraConstraint.PhysicsObject1.MainWorldTransform.GetOrientation(ref orientation1); CameraConstraint.PhysicsObject2.MainWorldTransform.GetOrientation(ref orientation2); carChassisPosition.X = carChassisPosition.Z = 0.0f; carChassisPosition.Y = 2.0f; Vector3.Subtract(ref position1, ref carChassisPosition, out position1); CameraConstraint.SetAnchor1(ref position1); CameraConstraint.SetAnchor2(ref position1); CameraConstraint.SetInitWorldOrientation1(ref orientation1); CameraConstraint.SetInitWorldOrientation2(ref orientation2); CameraConstraint.EnableLimitAngleX = true; CameraConstraint.EnableLimitAngleY = true; CameraConstraint.EnableLimitAngleZ = true; CameraConstraint.LimitAngleForce = 0.4f; CameraConstraint.MinResponseLinearVelocity = 0.005f; CameraConstraint.MinResponseAngularVelocity = 0.005f; CameraConstraint.Update(); } } } if (enableExternalMoving) { if (keyboardState[Key.Up]) { if (wheel1 != null) { Vector3.Multiply(ref unitY, 5.0f, out velocity); wheel1.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } if (wheel2 != null) { Vector3.Multiply(ref unitY, 5.0f, out velocity); wheel2.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } } if (keyboardState[Key.Down]) { if (wheel1 != null) { Vector3.Multiply(ref unitY, -5.0f, out velocity); wheel1.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } if (wheel2 != null) { Vector3.Multiply(ref unitY, -5.0f, out velocity); wheel2.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } } if (keyboardState[Key.Right]) { if (steeringGearConstraint != null) { steeringGearConstraint.ControlDegAngleY -= 2.0f; } if (constraint1 != null) { constraint1.ControlDegAngleY += 1.0f; } if (constraint4 != null) { constraint4.ControlDegAngleY += 1.0f; } } if (keyboardState[Key.Left]) { if (steeringGearConstraint != null) { steeringGearConstraint.ControlDegAngleY += 2.0f; } if (constraint1 != null) { constraint1.ControlDegAngleY -= 1.0f; } if (constraint4 != null) { constraint4.ControlDegAngleY -= 1.0f; } } } } oldKeyboardState = keyboardState; }
public void Move(SimulateMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); if (!objectBase.Camera.Enabled) { return; } if (!objectBase.Camera.Active) { return; } float time = (float)args.Time; Vector3 deltaRotation = vectorZero; Vector3 deltaTranslation = vectorZero; float rotationSpeed = 8.0f; float translationSpeed = 8.0f; float soundPositionFactor = 0.1f; bool enableShot = false; DemoMouseState mouseState = demo.GetMouseState(); DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (mouseState[MouseButton.Right]) { deltaRotation.Y += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.X - oldMouseState.X) * time); deltaRotation.X += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.Y - oldMouseState.Y) * time); } mousePosition.X = mouseState.X; mousePosition.Y = mouseState.Y; if (mouseState[MouseButton.Middle] && !oldMouseState[MouseButton.Middle]) { enableShot = true; } if ((keyboardState[Key.ControlRight] && !oldKeyboardState[Key.ControlRight]) || (keyboardState[Key.ControlLeft] && !oldKeyboardState[Key.ControlLeft])) { enableShot = true; } PhysicsObject cursorA = scene.Factory.PhysicsObjectManager.Find(cursorAName); PhysicsObject cursorB = scene.Factory.PhysicsObjectManager.Find(cursorBName); if (!demo.EnableMenu) { if (cursorA != null) { cursorA.EnableDrawing = true; } if (cursorB != null) { cursorB.EnableDrawing = true; } } else { if (cursorA != null) { cursorA.EnableDrawing = false; } if (cursorB != null) { cursorB.EnableDrawing = false; } } if (keyboardState[Key.W]) { deltaTranslation.Z += translationSpeed * time; } if (keyboardState[Key.S]) { deltaTranslation.Z -= translationSpeed * time; } if (keyboardState[Key.D]) { deltaTranslation.X += translationSpeed * time; } if (keyboardState[Key.A]) { deltaTranslation.X -= translationSpeed * time; } oldMouseState = mouseState; oldKeyboardState = keyboardState; if (deltaRotation.LengthSquared != 0.0f) { Vector3 euler = vectorZero; objectBase.Camera.GetEuler(ref euler); Vector3.Add(ref euler, ref deltaRotation, out euler); objectBase.Camera.SetEuler(ref euler); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out cameraRotation); objectBase.Camera.SetRotation(ref cameraRotation); objectBase.MainWorldTransform.SetTransposeRotation(ref cameraRotation); objectBase.RecalculateMainTransform(); } if (deltaTranslation.LengthSquared != 0.0f) { objectBase.MainWorldTransform.GetRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out direction); objectBase.MainWorldTransform.GetPosition(ref position); Vector3.Add(ref position, ref direction, out position); objectBase.MainWorldTransform.SetPosition(ref position); objectBase.RecalculateMainTransform(); } objectBase.Camera.Projection.CreatePerspectiveLH(1.0f, 11000.0f, 70.0f, demo.WindowWidth, demo.WindowHeight); objectBase.MainWorldTransform.GetPosition(ref position); objectBase.Camera.GetTransposeRotation(ref cameraRotation); objectBase.Camera.View.CreateLookAtLH(ref position, ref cameraRotation, 0.0f); objectBase.Camera.UpdateFrustum(); objectBase.Camera.View.GetViewMatrix(ref view); objectBase.Camera.Projection.GetProjectionMatrix(ref projection); Vector3 rayPosition, rayDirection; rayPosition = rayDirection = vectorZero; objectBase.UnProjectToRay(ref mousePosition, 0, 0, demo.WindowWidth, demo.WindowHeight, 0.0f, 1.0f, ref view, ref matrixIdentity, ref projection, ref rayPosition, ref rayDirection); PhysicsObject cursor = scene.Factory.PhysicsObjectManager.Find(cursorName); if (cursor != null) { Vector3 cursorPosition = vectorZero; Matrix4 cursorLocalRotation = matrixIdentity; Matrix4 cursorWorldRotation = matrixIdentity; cursor.InitLocalTransform.GetPosition(ref cursorPosition); cursor.InitLocalTransform.GetRotation(ref cursorLocalRotation); cursor.MainWorldTransform.GetRotation(ref cursorWorldRotation); objectBase.Camera.GetTransposeRotation(ref cameraRotation); Matrix4.Mult(ref cursorLocalRotation, ref cameraRotation, out rotation); Vector3.TransformVector(ref cursorPosition, ref cursorWorldRotation, out position); cursor.MainWorldTransform.SetRotation(ref rotation); Vector3.Add(ref position, ref rayPosition, out position); Vector3.Add(ref position, ref rayDirection, out position); cursor.MainWorldTransform.SetPosition(ref position); cursor.RecalculateMainTransform(); } CursorController cursorController = objectBase.InternalControllers.CursorController; if (cursorController.IsDragging) { if (cursorController.HitPhysicsObject.Integral.IsStatic && (cursorController.HitPhysicsObject.Shape != null) && (cursorController.HitPhysicsObject.Shape.ShapePrimitive != null) && (cursorController.HitPhysicsObject.Shape.ShapePrimitive.ShapePrimitiveType == ShapePrimitiveType.TriangleMesh)) { Vector3 cursorStartPosition = vectorZero; Vector3 cursorEndPosition = vectorZero; cursorController.GetAnchor1(ref cursorStartPosition); cursorController.GetAnchor2(ref cursorEndPosition); Vector3.Subtract(ref cursorEndPosition, ref cursorStartPosition, out direction); if (direction.LengthSquared != 0.0f) { cursorController.HitPhysicsObject.MainWorldTransform.GetPosition(ref cursorStartPosition); Vector3.Add(ref cursorStartPosition, ref direction, out cursorStartPosition); cursorController.HitPhysicsObject.MainWorldTransform.SetPosition(ref cursorStartPosition); cursorController.HitPhysicsObject.RecalculateMainTransform(); } } } objectBase.MainWorldTransform.GetPosition(ref listenerPosition); objectBase.Camera.GetTransposeRotation(ref rotation); Vector3.Multiply(ref listenerPosition, soundPositionFactor, out position); listenerTopDirection.X = rotation.Row1.X; listenerTopDirection.Y = rotation.Row1.Y; listenerTopDirection.Z = rotation.Row1.Z; listenerFrontDirection.X = rotation.Row2.X; listenerFrontDirection.Y = rotation.Row2.Y; listenerFrontDirection.Z = rotation.Row2.Z; listener.Position = position; listener.TopDirection = listenerTopDirection; listener.FrontDirection = listenerFrontDirection; listenerRange = objectBase.Sound.Range; if (enableShot) { Vector3 shotScale, shotColor; shotCount = (shotCount + 1) % shotTab.Length; string shotCountName = shotCount.ToString(); PhysicsObject shot = scene.Factory.PhysicsObjectManager.FindOrCreate(shotName + shotCountName); PhysicsObject shotBase = scene.Factory.PhysicsObjectManager.FindOrCreate(shotBaseName + shotCountName); PhysicsObject shotLight = scene.Factory.PhysicsObjectManager.FindOrCreate(shotLightName + shotCountName); shot.AddChildPhysicsObject(shotBase); shot.AddChildPhysicsObject(shotLight); shotTab[shotCount] = shotBase; enableShotTab = true; shotScale = shotColor = vectorZero; shotScale.X = shotScale.Y = shotScale.Z = 0.5f; Vector3.Multiply(ref rayDirection, 300.0f, out rayDirection); shot.InitLocalTransform.SetRotation(ref matrixIdentity); shot.InitLocalTransform.SetPosition(ref rayPosition); shot.InitLocalTransform.SetLinearVelocity(ref rayDirection); shot.InitLocalTransform.SetAngularVelocity(ref vectorZero); shot.MaxSimulationFrameCount = 200; shot.EnableRemovePhysicsObjectsFromManagerAfterMaxSimulationFrameCount = false; //shot.EnableLocalGravity = true; shotBase.Shape = sphere; shotBase.UserDataStr = sphereName; shotBase.InitLocalTransform.SetScale(ref shotScale); shotBase.Integral.SetDensity(10.0f); shotBase.Material.RigidGroup = true; shotBase.EnableBreakRigidGroup = false; shotBase.EnableCollisions = true; shotBase.DisableCollision(objectBase, true); shotBase.MaxDisableCollisionFrameCount = 50; shotBase.CreateSound(true); shotLight.Shape = sphere; shotLight.UserDataStr = sphereName; shotLight.CreateLight(true); shotLight.Light.Type = PhysicsLightType.Point; shotLight.Light.Range = 20.0f; shotColor.X = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Y = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Z = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotLight.Light.SetDiffuse(ref shotColor); shotLight.InitLocalTransform.SetScale(20.0f); shotLight.Material.UserDataStr = yellowName; shotLight.Material.RigidGroup = true; shotLight.EnableBreakRigidGroup = false; shotLight.EnableCollisions = false; shotLight.EnableCursorInteraction = false; shotLight.EnableAddToCameraDrawTransparentPhysicsObjects = false; scene.UpdateFromInitLocalTransform(shot); shotSoundGroup = demo.SoundGroups[hitName]; shotSoundGroup.MaxHitRepeatTime = 0.0f; if (shotSound == null) { shotSound = shotSoundGroup.GetSound(objectBase.Sound, listener, emitter); } shotSound.Update(time); Vector3.Multiply(ref rayPosition, soundPositionFactor, out shotSound.HitPosition); shotSound.HitVolume = 1.0f; shotSound.FrontDirection.X = rotation.Row2.X; shotSound.FrontDirection.Y = rotation.Row2.Y; shotSound.FrontDirection.Z = rotation.Row2.Z; demo.SoundQueue.EnqueueSound(shotSound); } else { PhysicsObject shotBase; DemoSound shotBaseSound; if (shotSound != null) { shotSound.Update(time); if (shotSound.Stop()) { shotSound.SoundGroup.SetSound(shotSound); shotSound = null; } } if (enableShotTab) { enableShotTab = false; for (int i = 0; i < shotTab.Length; i++) { shotBase = shotTab[i]; if (shotBase != null) { if (shotBase.Sound.UserDataObj != null) { enableShotTab = true; shotBaseSound = (DemoSound)shotBase.Sound.UserDataObj; shotBaseSound.Update(time); if (shotBaseSound.Stop()) { shotBaseSound.SoundGroup.SetSound(shotBaseSound); shotBase.Sound.UserDataObj = null; shotTab[i] = null; } } } } } } objectBase.Camera.UpdatePhysicsObjects(true, true, true); objectBase.Camera.SortDrawPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); objectBase.Camera.SortTransparentPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); objectBase.Camera.SortLightPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); PhysicsObject currentPhysicsObject; PhysicsSound currentSound; DemoSoundGroup soundGroup; DemoSound sound; if (demo.SoundQueue.SoundCount > 0) { return; } for (int i = 0; i < scene.TotalPhysicsObjectCount; i++) { if (demo.SoundQueue.SoundCount >= demo.SoundQueue.MaxSoundCount) { break; } currentPhysicsObject = scene.GetPhysicsObject(i); currentSound = !currentPhysicsObject.EnableSoundFromRigidGroupOwner ? currentPhysicsObject.Sound : currentPhysicsObject.RigidGroupOwner.Sound; if (currentSound == null) { continue; } if (!currentSound.Enabled) { continue; } if (currentSound.UserDataStr == null) { soundGroup = demo.SoundGroups[defaultName]; } else { soundGroup = demo.SoundGroups[currentSound.UserDataStr]; } if (currentPhysicsObject.IsSleeping && (currentSound.UserDataObj == null) && !soundGroup.EnableBackground) { continue; } if (currentPhysicsObject.GetSoundData(ref listenerPosition, listenerRange, soundGroup.EnableHit, soundGroup.EnableRoll, soundGroup.EnableSlide, soundGroup.EnableBackground, currentSound.BackgroundVolumeVelocityModulation, ref hitPosition, ref rollPosition, ref slidePosition, ref backgroundPosition, ref hitVolume, ref rollVolume, ref slideVolume, ref backgroundVolume)) { if (currentSound.UserDataObj == null) { sound = soundGroup.GetSound(currentSound, listener, emitter); currentSound.UserDataObj = sound; } else { sound = (DemoSound)currentSound.UserDataObj; } sound.Update(time); currentPhysicsObject.MainWorldTransform.GetTransposeRotation(ref rotation); Vector3.Multiply(ref hitPosition, soundPositionFactor, out sound.HitPosition); Vector3.Multiply(ref rollPosition, soundPositionFactor, out sound.RollPosition); Vector3.Multiply(ref slidePosition, soundPositionFactor, out sound.SlidePosition); Vector3.Multiply(ref backgroundPosition, soundPositionFactor, out sound.BackgroundPosition); sound.HitVolume = hitVolume; sound.RollVolume = rollVolume; sound.SlideVolume = slideVolume; sound.BackgroundVolume = backgroundVolume; sound.FrontDirection.X = rotation.Row2.X; sound.FrontDirection.Y = rotation.Row2.Y; sound.FrontDirection.Z = rotation.Row2.Z; demo.SoundQueue.EnqueueSound(sound); } else { if (currentSound.UserDataObj != null) { sound = (DemoSound)currentSound.UserDataObj; sound.Update(time); if (sound.Stop()) { soundGroup.SetSound(sound); currentSound.UserDataObj = null; } } } } }