// OnUpdate() is called once per frame. protected override void OnUpdate(TimeSpan deltaTime) { if (_inputService.IsPressed(MouseButtons.Middle, true) || _inputService.IsPressed(Buttons.RightShoulder, true, LogicalPlayerIndex.One)) { // The user has triggered an explosion. // The explosion is created at the position that is targeted with the cross-hair. // We can perform a ray hit-test to find the position. The ray starts at the camera // position and shoots forward (-z direction). var cameraGameObject = (CameraObject)_gameObjectService.Objects["Camera"]; var cameraNode = cameraGameObject.CameraNode; Vector3F cameraPosition = cameraNode.PoseWorld.Position; Vector3F cameraDirection = cameraNode.PoseWorld.ToWorldDirection(Vector3F.Forward); // Create a ray for hit-testing. var ray = new RayShape(cameraPosition, cameraDirection, 1000); // The ray should stop at the first hit. We only want the first object. ray.StopsAtFirstHit = true; // The collision detection requires a CollisionObject. var rayCollisionObject = new CollisionObject(new GeometricObject(ray, Pose.Identity)) { // In SampleGame.ResetPhysicsSimulation() a collision filter was set: // CollisionGroup = 0 ... objects that support hit-testing // CollisionGroup = 1 ... objects that are ignored during hit-testing // CollisionGroup = 2 ... objects (rays) for hit-testing CollisionGroup = 2, }; // Get the first object that has contact with the ray. ContactSet contactSet = _simulation.CollisionDomain.GetContacts(rayCollisionObject).FirstOrDefault(); if (contactSet != null && contactSet.Count > 0) { // The ray has hit something. // The contact set contains all detected contacts between the ray and another object. // Get the first contact in the contact set. (A ray hit usually contains exactly 1 contact.) Contact contact = contactSet[0]; // Create an explosion at the hit position. var explosion = new Explosion { Position = contact.Position }; _simulation.ForceEffects.Add(explosion); // Note: The Explosion force effect removes itself automatically from the simulation once // it has finished. } } }
/// <summary> /// Creates an explosion in near the center of the scene. /// </summary> private void Explode() { Explosion explosion = new Explosion { Force = 5e5f, Position = RandomHelper.Random.NextVector3F(-2, 2), Radius = 12 }; Simulation.ForceEffects.Add(explosion); }