Exemple #1
0
        private void OnInputEvent(ReadOnlyCollection <SInputButtonEvent> buttonevents, string textinput)
        {
            if (World.IsPlaying)
            {
                return;
            }

            foreach (var buttonEvent in buttonevents)
            {
                if (buttonEvent.button == EInputButton.MouseLeftButton && buttonEvent.buttonEvent == EButtonEvent.Pressed)
                {
                    if (ImGui.GetIO().WantCaptureMouse)
                    {
                        return;
                    }
                    CViewManager viewManager = World.ViewManager;
                    int          mouseAbsX   = System.Windows.Forms.Cursor.Position.X - (int)viewManager.ScreenLeft;
                    int          mouseAbsY   = System.Windows.Forms.Cursor.Position.Y - (int)viewManager.ScreenTop;

                    if (mouseAbsX < 0 || mouseAbsY < 0 || mouseAbsX > viewManager.ScreenWidth || mouseAbsY > viewManager.ScreenHeight)
                    {
                        return;
                    }

                    viewManager.GetViewInfo(out SSceneViewInfo viewInfo);

                    if (m_gizmo.IsHovered)
                    {
                        return;
                    }

                    Ray pickRay = Ray.GetPickRay(mouseAbsX, mouseAbsY, new ViewportF(0, 0, viewManager.ScreenWidth, viewManager.ScreenHeight), viewInfo.ViewMatrix * viewInfo.ProjectionMatrix);
                    if (CRenderer.Instance.ActiveScene.RayIntersection(pickRay, out CRenderNode node, out STriangle tri, out float dist, MarkPickedObjects > 0))
                    {
                        if (node.Outer is CSceneComponent sceneComponent)
                        {
                            CSceneComponent rootComponent = sceneComponent.Owner.RootComponent;

                            m_gizmo.IsActive = true;
                            m_gizmo.SetControlledTransform(rootComponent.Transform);

                            OnComponentPicked?.Invoke(rootComponent);
                        }
                    }
                    else
                    {
                        m_gizmo.IsActive = false;
                        OnComponentPicked?.Invoke(null);
                    }
                }
        private void OnInputEvent(ReadOnlyCollection <SInputButtonEvent> buttonevents, string textinput)
        {
            foreach (var buttonEvent in buttonevents)
            {
                if (buttonEvent.button == EInputButton.MouseMiddleButton && buttonEvent.buttonEvent == EButtonEvent.Pressed)
                {
                    CViewManager viewManager = m_gameWorld.ViewManager;
                    int          mouseAbsX   = System.Windows.Forms.Cursor.Position.X - (int)viewManager.ScreenLeft;
                    int          mouseAbsY   = System.Windows.Forms.Cursor.Position.Y - (int)viewManager.ScreenTop;

                    if (mouseAbsX < 0 || mouseAbsY < 0)
                    {
                        return;
                    }

                    viewManager.GetViewInfo(out SSceneViewInfo viewInfo);
                    Ray pickRay = Ray.GetPickRay(mouseAbsX, mouseAbsY, new ViewportF(0, 0, viewManager.ScreenWidth, viewManager.ScreenHeight), viewInfo.ViewMatrix * viewInfo.ProjectionMatrix);
                    if (m_physicSpace.RayCast(pickRay.ToBepu(), 9999.0f, out RayCastResult result))
                    {
                        float fieldRadius     = 5.0f;
                        float impulseStrength = 20.0f;
                        List <BroadPhaseEntry> queryResults = new List <BroadPhaseEntry>();
                        BoundingSphere         bs           = new BoundingSphere(result.HitData.Location, fieldRadius);
                        m_physicSpace.BroadPhase.QueryAccelerator.GetEntries(bs, queryResults);
                        foreach (var entry in queryResults)
                        {
                            var entityCollision = entry as EntityCollidable;
                            if (entityCollision != null)
                            {
                                var e = entityCollision.Entity;
                                if (e.IsDynamic)
                                {
                                    Vector3 toEntity = e.Position - result.HitData.Location;
                                    float   length   = toEntity.Length();
                                    float   strength = impulseStrength / length;
                                    toEntity.Y = 1.0f;
                                    toEntity.Normalize();
                                    e.ApplyImpulse(e.Position, toEntity * strength);
                                }
                            }
                        }
                    }
                }
            }
        }