Example #1
0
        public void AddForce(Vector3 force)
        {
            //Check();
            //World world;
            //entity. AddForce(new JVector(force.X, force.Y, force.Z));
            float scale = 0.1f;

            entity.ApplyImpulse(XVector3.Zero, new XVector3(force.X * scale, force.Y * scale, force.Z * scale));
            //Check();
        }
Example #2
0
 /// <summary>
 /// Applies a force directly to the physics entity's body, at a specified relative origin point.
 /// The origin is relevant to the body's centerpoint.
 /// The further you get from the centerpoint, the more spin and less linear motion will be applied.
 /// Note: this is a force, not a velocity. Mass is relevant.
 /// This will activate the entity.
 /// </summary>
 /// <param name="force">The force to apply.</param>
 public void ApplyForce(Location origin, Location force)
 {
     if (Body != null)
     {
         Vector3 ori = origin.ToBVector();
         Vector3 vec = force.ToBVector();
         Body.ApplyImpulse(ref ori, ref vec);
         Body.ActivityInformation.Activate();
     }
     else
     {
         // TODO: Account for spin?
         LVel += force / Mass;
     }
 }
Example #3
0
        public override void Click(Entity ent, ItemStack item)
        {
            if (!(ent is PlayerEntity))
            {
                // TODO: update to generic entity
                return;
            }
            PlayerEntity  player = (PlayerEntity)ent;
            Location      eye    = player.GetEyePosition();
            Location      forw   = player.ForwardVector();
            RayCastResult rcr;
            bool          h = player.TheRegion.SpecialCaseRayTrace(eye, forw, 5, MaterialSolidity.FULLSOLID, player.IgnoreThis, out rcr);

            if (h)
            {
                bool hitIt = (player.TheRegion.GlobalTickTime - player.LastBlockBreak) > 0.5f; // TODO: Arbitrary constant!
                if (hitIt && rcr.HitObject != null && rcr.HitObject is EntityCollidable && ((EntityCollidable)rcr.HitObject).Entity != null)
                {
                    BEPUphysics.Entities.Entity hitent = ((EntityCollidable)rcr.HitObject).Entity;
                    Vector3 force = forw.ToBVector() * 75; // TODO: Arbitrary constant!
                    hitent.ApplyImpulse(ref rcr.HitData.Location, ref force);
                    // TODO: Damage
                    player.LastBlockBreak = player.TheRegion.GlobalTickTime;
                    return;
                }
                if (!player.Mode.GetDetails().CanBreak)
                {
                    return;
                }
                bool     breakIt = false;
                Location block   = (new Location(rcr.HitData.Location) - new Location(rcr.HitData.Normal).Normalize() * 0.01).GetBlockLocation();
                if (block != player.BlockBreakTarget)
                {
                    player.BlockBreakStarted = 0;
                    player.BlockBreakTarget  = block;
                }
                Material mat = player.TheRegion.GetBlockMaterial(block);
                if (player.Mode.GetDetails().FastBreak)
                {
                    breakIt = player.TheRegion.GlobalTickTime - player.LastBlockBreak >= 0.2; // TODO: Arbitrary constant!
                }
                else
                {
                    if (player.BlockBreakStarted <= 0)
                    {
                        player.BlockBreakStarted = player.TheRegion.GlobalTickTime;
                    }
                    double          bt         = mat.GetBreakTime();
                    MaterialBreaker breaker    = GetBreaker();
                    MaterialBreaker matbreaker = mat.GetBreaker();
                    if (matbreaker == MaterialBreaker.NON_BREAKABLE)
                    {
                        return;
                    }
                    // TODO: arbitrary constants!
                    if (breaker == MaterialBreaker.PICKAXE)
                    {
                        if (matbreaker == MaterialBreaker.PICKAXE)
                        {
                            bt *= 0.3f;
                        }
                        else if (!mat.GetBreaksFromOtherTools())
                        {
                            return;
                        }
                        else if (matbreaker == MaterialBreaker.AXE)
                        {
                            bt *= 0.7f;
                        }
                        else if (matbreaker == MaterialBreaker.SHOVEL)
                        {
                            bt *= 1.2f;
                        }
                        else if (matbreaker == MaterialBreaker.HAND)
                        {
                            bt *= 0.87f;
                        }
                    }
                    // else: hand! Default values!
                    breakIt = (player.TheRegion.GlobalTickTime - player.BlockBreakStarted) > bt;
                }
                if (breakIt)
                {
                    if (player.TheRegion.IsAllowedToBreak(player, block, mat))
                    {
                        player.TheRegion.BreakNaturally(block);
                        player.Network.SendPacket(new DefaultSoundPacketOut(block, DefaultSound.BREAK, (byte)mat.Sound()));
                        player.LastBlockBreak = player.TheRegion.GlobalTickTime;
                    }
                    player.BlockBreakStarted = 0;
                }
            }
        }
Example #4
0
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            float         timeDelta  = (float)gameTime.ElapsedGameTime.TotalSeconds;
            KeyboardState keyState   = Keyboard.GetState();
            MouseState    mouseState = Mouse.GetState();

            if (keyState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
            if (keyState.IsKeyDown(Keys.F1))
            {
                if (!didSpawn)
                {
                    Ray ray;
                    ray.Position  = Camera.Position;
                    ray.Direction = Camera.Look;
                    Vector3 point;
                    if (ground.rayIntersects(ray, out point))
                    {
                        point.Y = 10;
                        createFromMesh(point, "fish", 10);
                        didSpawn = true;
                    }
                }
            }
            else if (keyState.IsKeyDown(Keys.F2))
            {
                if (!didSpawn)
                {
                    Ray ray;
                    ray.Position  = Camera.Position;
                    ray.Direction = Camera.Look;
                    Vector3 point;
                    if (ground.rayIntersects(ray, out point))
                    {
                        point.Y = 10;
                        createFromMesh(point, "tube", 1);
                        didSpawn = true;
                    }
                }
            }
            else if (keyState.IsKeyDown(Keys.F3))
            {
                if (!didSpawn)
                {
                    Ray ray;
                    ray.Position  = Camera.Position;
                    ray.Direction = Camera.Look;
                    Vector3 point;
                    if (ground.rayIntersects(ray, out point))
                    {
                        point.Y = 10;
                        createVehicle(point);
                        didSpawn = true;
                    }
                }
            }
            else if (keyState.IsKeyDown(Keys.F4))
            {
                if (!didSpawn)
                {
                    Ray ray;
                    ray.Position  = Camera.Position;
                    ray.Direction = Camera.Look;
                    Vector3 point;
                    if (ground.rayIntersects(ray, out point))
                    {
                        point.Y = 10;
                        createFromMesh(point, "guy", 10);
                        didSpawn = true;
                    }
                }
            }
            else if (keyState.IsKeyDown(Keys.F5))
            {
                if (!didSpawn)
                {
                    Ray ray;
                    ray.Position  = Camera.Position;
                    ray.Direction = Camera.Look;
                    Vector3 point;
                    if (ground.rayIntersects(ray, out point))
                    {
                        point.Y = 25;
                        BepuEntity cog1 = createCog(point, 10, 20);
                        //BepuEntity cog2 = createCog(point, 10, 20);

                        didSpawn = true;
                    }
                }
            }
            else if (keyState.IsKeyDown(Keys.F12))
            {
                resetScene();
                didSpawn = true;
            }
            else
            {
                didSpawn = false;
            }



            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                if (pickedUp == null)
                {
                    Ray ray;
                    ray.Position  = Camera.Position;
                    ray.Direction = Camera.Look;
                    RayCastResult result;
                    bool          didDit = space.RayCast(ray, out result);
                    if (didDit)
                    {
                        pickedUp = ((EntityCollidable)result.HitObject).Entity;
                        if (pickedUp == groundBox)
                        {
                            pickedUp = null;
                        }
                    }
                }
                if (pickedUp != null)
                {
                    float fDistance   = 15.0f;
                    float powerfactor = 50.0f; // Higher values causes the targets moving faster to the holding point.
                    float maxVel      = 40.0f; // Lower values prevent objects flying through walls.

                    // Calculate the hold point in front of the camera
                    Vector3 holdPos = Camera.Position + (Camera.Look * fDistance);

                    Vector3 v = holdPos - pickedUp.Position; // direction to move the Target
                    v *= powerfactor;                        // powerfactor of the GravityGun

                    if (v.Length() > maxVel)
                    {
                        // if the correction-velocity is bigger than maximum
                        v.Normalize();
                        v *= maxVel; // just set correction-velocity to the maximum
                    }
                    pickedUp.LinearVelocity = v;
                }
            }
            else
            {
                pickedUp = null;
            }
            if (keyState.IsKeyDown(Keys.F) & pickedUp != null)
            {
                pickedUp.LinearVelocity = Vector3.Zero;
                pickedUp.ApplyImpulse(pickedUp.Position, Camera.Look * 100.0f);
                pickedUp = null;
            }

            if (keyState.IsKeyDown(Keys.B) & lastFired > 0.25f)
            {
                fireBall();
                lastFired = 0.0f;
            }

            lastFired += timeDelta;


            for (int i = 0; i < children.Count; i++)
            {
                children[i].Update(gameTime);
            }

            cameraCylindar.Position = camera.Position;
            space.Update();


            base.Update(gameTime);
        }