Example #1
0
        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);
                                }
                            }
                        }
                    }
                }
            }
        }