/// <summary> /// Update method /// </summary> /// <param name="gameTime"></param> protected override void Update(TimeSpan gameTime) { touchPanelState = WaveServices.Input.TouchPanelState; bestValue = float.MaxValue; if (touchPanelState.IsConnected && touchPanelState.Count > 0) { // Calculate the ray CalculateRay(); // Look for all entities in the game... for (int i = 0; i < EntityManager.Count; i++) { currentEntity = EntityManager.EntityGraph.ElementAt(i);; entityCollider = currentEntity.FindComponent <BoxCollider3D>(); // ... but only a collidable entities ( entities which have a boxCollider component) if (entityCollider != null) { // Intersect our calculated ray with the entity's boxCollider collisionResult = entityCollider.Intersects(ref ray); // If any collision if (collisionResult.HasValue) { // Check the distance. We want to have the closer to the screen entity, so we want to get the low collisionResult value if (collisionResult.Value < bestValue) { // Send to the scene the new entity picked name if (this.myScene != null) { this.myScene.ShowPickedEntity(currentEntity.Name); } bestValue = collisionResult.Value; } } } } } else { if (this.myScene != null) { this.myScene.ShowPickedEntity("None"); } } }
private void Ray(Camera3D activeCamera, TouchLocation touchLocation) { // Creates Ray from Touch 2D position _nearPosition.X = touchLocation.Position.X; _nearPosition.Y = touchLocation.Position.Y; _nearPosition.Z = 0.0f; _farPosition.X = touchLocation.Position.X; _farPosition.Y = touchLocation.Position.Y; _farPosition.Z = 1.0f; // Unproject Mouse Position _nearPosition = activeCamera.Unproject(ref _nearPosition); _farPosition = activeCamera.Unproject(ref _farPosition); // Update ray. Ray launched from nearPosition in rayDirection direction. _rayDirection = _farPosition - _nearPosition; _rayDirection.Normalize(); _ray.Direction = _rayDirection; _ray.Position = _nearPosition; foreach (Entity entity in this.Owner.Scene.EntityManager.FindAllByTag("touchable")) { _entityBoxCollider = entity.FindComponent <BoxCollider3D>(); if (_entityBoxCollider != null) { _materialComponent = entity.FindComponent <MaterialComponent>(); _collisionResult = _entityBoxCollider.Intersects(ref _ray); if (_collisionResult.HasValue) { PushpinTapped?.Invoke(this, new PushpinTappedEventArgs(entity.Name)); } } } }
protected override void Update(TimeSpan gameTime) { touchPanelState = WaveServices.Input.TouchPanelState; if (touchPanelState.IsConnected && touchPanelState.Count > 0) { // Calculate the ray CalculateRay(); // Look for all entities in the game... Entity auxEntity = currentEntity = null; bestValue = float.MaxValue; for (int i = 0; i < EntityManager.Count; i++) { auxEntity = EntityManager.EntityGraph.ElementAt(i); entityCollider = auxEntity.FindComponent<BoxCollider3D>(); // ... but only a collidable entities ( entities which have a boxCollider component) if (entityCollider != null && ( auxEntity.Name.Contains("box") || auxEntity.Name.Contains("anchor") || auxEntity.Name.Contains("BigBall")) ) { // Intersect our calculated ray with the entity's boxCollider collisionResult = entityCollider.Intersects(ref ray); // If any collision if (collisionResult.HasValue && collisionResult.Value > 0.001f) { //Labels.Add("CollisionResult", collisionResult.ToString()); //Labels.Add("CollisionValue", collisionResult.Value.ToString()); // Check the distance. We want to have the closer to the screen entity, so we want to get the low collisionResult value if (collisionResult.Value < bestValue) { this.currentEntity = auxEntity; bestValue = collisionResult.Value; } } } } if (this.currentEntity != null) { Vector3 entityPosition = this.currentEntity.FindComponent<Transform3D>().Position; Vector3 impulse = entityPosition - this.Camera.Position; this.currentEntity.FindComponent<RigidBody3D>().ApplyLinearImpulse(impulse*FORCE); this.line.StartPoint = ray.Position; this.line.EndPoint = entityPosition; Labels.Add("Entity", this.currentEntity.Name); //Labels.Add("Impulse", impulse.ToString()); //Labels.Add("IsActive", this.currentEntity.FindComponent<RigidBody3D>().IsActive.ToString()); } else { Labels.Add("Entity", "None"); //Labels.Add("Impulse", "0,0,0"); } } //RenderManager.LineBatch3D.DrawLine(ref line); //RenderManager.LineBatch3D.DrawLine(ref line2); }
protected override void Update(TimeSpan gameTime) { touchPanelState = WaveServices.Input.TouchPanelState; bestValue = float.MaxValue; if (touchPanelState.IsConnected && touchPanelState.Count > 0) { // Calculate the ray CalculateRay(); // Look for all entities in the game... for (int i = 0; i < EntityManager.Count; i++) { currentEntity = EntityManager.EntityGraph.ElementAt(i); ; entityCollider = currentEntity.FindComponent<BoxCollider3D>(); // ... but only a collidable entities ( entities which have a boxCollider component) if (entityCollider != null) { // Intersect our calculated ray with the entity's boxCollider collisionResult = entityCollider.Intersects(ref ray); // If any collision if (collisionResult.HasValue) { // Check the distance. We want to have the closer to the screen entity, so we want to get the low collisionResult value if (collisionResult.Value < bestValue) { // Send to the scene the new entity picked name (WaveServices.ScreenContextManager.CurrentContext[0] as MyScene).ShowPickedEntity(currentEntity.Name); bestValue = collisionResult.Value; } } } } } else { (WaveServices.ScreenContextManager.CurrentContext[0] as MyScene).ShowPickedEntity("None"); } }
protected override void Update(TimeSpan gameTime) { touchPanelState = WaveServices.Input.TouchPanelState; if (touchPanelState.IsConnected && touchPanelState.Count > 0) { // Calculate the ray CalculateRay(); // Look for all entities in the game... Entity auxEntity = currentEntity = null; bestValue = float.MaxValue; for (int i = 0; i < EntityManager.Count; i++) { auxEntity = EntityManager.EntityGraph.ElementAt(i); entityCollider = auxEntity.FindComponent <BoxCollider3D>(); // ... but only a collidable entities ( entities which have a boxCollider component) if (entityCollider != null && (auxEntity.Name.Contains("box") || auxEntity.Name.Contains("anchor") || auxEntity.Name.Contains("BigBall"))) { // Intersect our calculated ray with the entity's boxCollider collisionResult = entityCollider.Intersects(ref ray); // If any collision if (collisionResult.HasValue && collisionResult.Value > 0.001f) { //Labels.Add("CollisionResult", collisionResult.ToString()); //Labels.Add("CollisionValue", collisionResult.Value.ToString()); // Check the distance. We want to have the closer to the screen entity, so we want to get the low collisionResult value if (collisionResult.Value < bestValue) { this.currentEntity = auxEntity; bestValue = collisionResult.Value; } } } } if (this.currentEntity != null) { Vector3 entityPosition = this.currentEntity.FindComponent <Transform3D>().Position; Vector3 impulse = entityPosition - this.Camera.Position; this.currentEntity.FindComponent <RigidBody3D>().ApplyLinearImpulse(impulse * FORCE); this.line.StartPoint = ray.Position; this.line.EndPoint = entityPosition; Labels.Add("Entity", this.currentEntity.Name); //Labels.Add("Impulse", impulse.ToString()); //Labels.Add("IsActive", this.currentEntity.FindComponent<RigidBody3D>().IsActive.ToString()); } else { Labels.Add("Entity", "None"); //Labels.Add("Impulse", "0,0,0"); } } //RenderManager.LineBatch3D.DrawLine(ref line); //RenderManager.LineBatch3D.DrawLine(ref line2); }