Beispiel #1
0
        void TestRayCast(CollisionObject testObject)
        {
            Vector3 rayFromWorld = testObject.WorldTransform.Origin + new Vector3(0, 0, -2);
            Vector3 rayToWorld   = testObject.WorldTransform.Origin + new Vector3(0, 0, 2);
            var     rayCallback  = new CustomRayCallback(ref rayFromWorld, ref rayToWorld);

            world.RayTest(ref rayFromWorld, ref rayToWorld, rayCallback);
            if (rayCallback.CollisionObject != testObject)
            {
                Console.WriteLine("Raycast FAILED!");
            }

            AddToDisposeQueue(rayCallback);
        }
        public override bool Raycast(GameSystem.GameCore.SerializableMath.Vector3 startPoint, GameSystem.GameCore.SerializableMath.Vector3 endPoint, out GameSystem.GameCore.SerializableMath.Vector3 hitPoint, out CollisionProxy hitObject, int mask = -1)
        {
            // transfer serializable math position to bullet math position
            BulletSharp.Math.Vector3 start = startPoint.ToBullet(), end = endPoint.ToBullet();
            var callback = new ClosestRayResultCallback(ref start, ref end);

            // set group mask filter
            callback.CollisionFilterMask = (short)mask;
            world.RayTest(start, end, callback);
            if (callback.HasHit)
            {
                hitPoint  = callback.HitPointWorld.ToSerializable();
                hitObject = (CollisionProxy)callback.CollisionObject.UserObject;
            }
            else
            {
                hitPoint  = GameSystem.GameCore.SerializableMath.Vector3.Zero;
                hitObject = null;
            }
            return(callback.HasHit);
        }
Beispiel #3
0
        public static List <RayTestResult> RayTest(OpenTK.Vector3 from, OpenTK.Vector3 to, params GameObject[] ignoreObjects)
        {
            var cb = new RayResultCB(ignoreObjects);

            world.RayTest(from, to, cb);
            var res = cb.Results;

            foreach (var r in res)
            {
                r.Position = Vector3.Lerp(from, to, r.Rate);
            }

            res.Sort((r1, r2) => r1.Rate - r2.Rate < 0.0f ? -1 : 1);

            return(res);
        }
Beispiel #4
0
        public bool RayPick(OpenTK.Vector3 from, OpenTK.Vector3 to, out OpenTK.Vector3 col)
        {
            var bfrom = ToBulletVector3(from);
            var bto   = ToBulletVector3(to);

            var cfrom = new BulletSharp.Math.Vector3();
            var cto   = new BulletSharp.Math.Vector3();

            ClosestRayResultCallback rayCb = new ClosestRayResultCallback(ref cfrom, ref cto);

            world.RayTest(bfrom, bto, rayCb);

            // Lerp로 보간해야 결과가 나온다.

            var p = BulletSharp.Math.Vector3.Lerp(bfrom, bto, rayCb.ClosestHitFraction);

            col = ToOpenVector3(p);

            return(rayCb.HasHit);
        }
Beispiel #5
0
        /// <summary>
        /// Called when the game has determined that game logic needs to be processed.
        /// </summary>
        /// <param name="gameTime">Time passed since the last call to this function.</param>
        protected override void Update(GameTime gameTime)
        {
            MouseState mouseState = Mouse.GetState();
            Vector3    rayTo      = getRayTo(mouseState.X, mouseState.Y);

            if (mouseState.LeftButton == ButtonState.Pressed && _prevMouseState.LeftButton == ButtonState.Released)
            {
                shootBox(rayTo);
            }
            if (mouseState.MiddleButton == ButtonState.Pressed && _prevMouseState.MiddleButton == ButtonState.Released)
            {
                if (_world != null)
                {
                    CollisionWorld.ClosestRayResultCallback rayCallback = new CollisionWorld.ClosestRayResultCallback(_camera.Position, rayTo);
                    _world.RayTest(_camera.Position, rayTo, rayCallback);
                    if (rayCallback.HasHit)
                    {
                        RigidBody body = RigidBody.Upcast(rayCallback.CollisionObject);
                        if (body != null)
                        {
                            //other exclusions?
                            if (!(body.IsStaticObject || body.IsKinematicObject))
                            {
                                _pickedBody = body;
                                _pickedBody.ActivationState = ActivationState.DisableDeactivation;

                                Vector3 pickPos = rayCallback.HitPointWorld;

                                Vector3 localPivot = Vector3.Transform(pickPos, XnaDevRu.BulletX.MathHelper.InvertMatrix(body.CenterOfMassTransform));

                                Point2PointConstraint p2p = new Point2PointConstraint(body, localPivot);
                                _world.AddConstraint(p2p);
                                _pickConstraint = p2p;

                                //save mouse position for dragging
                                _oldPickingPos = rayTo;

                                Vector3 eyePos = new Vector3(_camera.Position.X, _camera.Position.Y, _camera.Position.Z);

                                _oldPickingDist = (eyePos - pickPos).Length();

                                //very weak constraint for picking
                                p2p.Settings.Tau = 1.1f;
                            }
                        }
                    }
                }
            }
            else if (mouseState.MiddleButton == ButtonState.Released && _prevMouseState.MiddleButton == ButtonState.Pressed)
            {
                if (_pickConstraint != null && _world != null)
                {
                    _world.RemoveConstraint(_pickConstraint);
                    _pickConstraint = null;
                    _pickedBody.ForceActivationState(ActivationState.Active);
                    _pickedBody.DeactivationTime = 0f;
                    _pickedBody = null;
                }
            }

            if (_pickConstraint != null)
            {
                //move the constraint pivot
                Point2PointConstraint p2p = _pickConstraint as Point2PointConstraint;
                if (p2p != null)
                {
                    //keep it at the same picking distance
                    Vector3 dir = rayTo - _camera.Position;
                    dir.Normalize();
                    dir *= _oldPickingDist;

                    Vector3 newPos = _camera.Position + dir;
                    p2p.PivotInB = newPos;
                }
            }

            _prevMouseState = mouseState;

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                //world.stepSimulation(1.0f/60.0f,0);
                int numObjects = _world.CollisionObjectsCount;

                for (int i = 0; i < numObjects; i++)
                {
                    CollisionObject colObj = _world.CollisionObjects[i];
                    RigidBody       body   = RigidBody.Upcast(colObj);
                    if (body != null)
                    {
                        if (body.MotionState != null)
                        {
                            DefaultMotionState myMotionState = (DefaultMotionState)body.MotionState;
                            myMotionState.GraphicsWorldTransform = myMotionState.StartWorldTransform;
                            colObj.WorldTransform = myMotionState.GraphicsWorldTransform;
                            colObj.InterpolationWorldTransform = myMotionState.StartWorldTransform;
                            colObj.Activate();
                        }

                        //removed cached contact points
                        _world.Broadphase.CleanProxyFromPairs(colObj.Broadphase);

                        if (body != null && !body.IsStaticObject)
                        {
                            RigidBody.Upcast(colObj).LinearVelocity  = new Vector3(0, 0, 0);
                            RigidBody.Upcast(colObj).AngularVelocity = new Vector3(0, 0, 0);
                        }
                    }
                }
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Escape) || GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Exit();
            }
            else
            {
                //world.stepSimulation(1.0f / 60.0f, 1);
            }

            base.Update(gameTime);
        }