Exemplo n.º 1
0
        public List <AxisAlignedBb> GetBlockCollisionBoxes(AxisAlignedBb box)
        {
            List <AxisAlignedBb> blocks = new List <AxisAlignedBb>();

            AxisAlignedBb bb = box.Union(box);

            var air = BlockRegistry.GetBlock <BlockAir>();

            for (int x = (int)bb.Min.X, maxX = (int)bb.Max.X; x < maxX; x++)
            {
                for (int y = (int)bb.Min.Y, maxY = (int)bb.Max.Y; y < maxY; y++)
                {
                    for (int z = (int)bb.Min.Z, maxZ = (int)bb.Max.Z; z < maxZ; z++)
                    {
                        BlockPos   pos   = new BlockPos(x, y, z);
                        BlockState state = SharpCraft.Instance.World.GetBlockState(pos);
                        if (state.Block == air || state.Block.Material.CanWalkThrough)
                        {
                            continue;
                        }

                        blocks.Add(state.Block.BoundingBox.Offset(pos.ToVec()));
                    }
                }
            }

            return(blocks);
        }
Exemplo n.º 2
0
 public List <AxisAlignedBb> GetIntersectingEntitiesBBs(AxisAlignedBb with)
 {
     return((from entity in Entities
             where !(entity is EntityItem)
             select entity.GetEntityBoundingBox()
             into bb
             where bb.IntersectsWith(with)
             select bb).ToList());
 }
Exemplo n.º 3
0
        protected Chunk(ChunkPos pos, World world)
        {
            Pos          = pos;
            World        = world;
            _loadManager = World.LoadManager;
            BoundingBox  = new AxisAlignedBb(Vector3.Zero, Vector3.One * ChunkSize + Vector3.UnitY * 240).Offset(Pos.ToVec());

            //Load();
        }
Exemplo n.º 4
0
        public BlockLadder() : base(Material.GetMaterial("wood"))
        {
            SetUnlocalizedName("sharpcraft", "ladder");
            IsFullCube = false;

            BoundingBox = new AxisAlignedBb(0, 0, 0.8f, 1, 1, 1);

            Hardness = 32;
        }
Exemplo n.º 5
0
        public BlockTulipRed() : base(Material.GetMaterial("tallgrass"))
        {
            SetUnlocalizedName("sharpcraft", "tulip_red");

            IsFullCube    = false;
            IsReplaceable = true;

            BoundingBox = new AxisAlignedBb(0.85f).Offset(new Vector3(0.075f, 0, 0.075f));

            Hardness = 0;
        }
Exemplo n.º 6
0
        protected BlockSlab(Material mat) : base(mat) //TODO
        {
            IsFullCube = false;

            var size = Vector3.One;

            size.Y = 0.5f;

            BoundingBox = new AxisAlignedBb(size);

            Hardness = 64; //TODO - set based on the state
        }
Exemplo n.º 7
0
        protected Particle(World world, Vector3 pos, Vector3 motion, float scale, int textureId, Vector2 uVmin, Vector2 uVmax) : base(world, pos, motion)
        {
            LastParticleScale = ParticleScale = scale / 10;

            TextureId = textureId;
            UVmin     = uVmin;
            UVmax     = uVmax;

            CollisionBoundingBox = new AxisAlignedBb(ParticleScale);
            BoundingBox          = CollisionBoundingBox.Offset(pos - (Vector3.UnitX * CollisionBoundingBox.Size.X / 2 + Vector3.UnitZ * CollisionBoundingBox.Size.Z / 2));

            ParticleMaxAge = (int)MathUtil.NextFloat(10, 50);

            Gravity = 0.9f;
        }
Exemplo n.º 8
0
        public static bool RayIntersectsBB(Vector3 /*ray*/ origin, Vector3 /*ray*/ direction, AxisAlignedBb bb, out Vector3 hitPosition, out Vector3 hitNormal)
        {
            direction   = direction.Normalized();
            hitNormal   = Vector3.One.Normalized();
            hitPosition = Vector3.Zero;

            float   tmin, tmax, tymin, tymax, tzmin, tzmax;
            Vector3 invrd = direction;

            invrd.X = 1.0f / invrd.X;
            invrd.Y = 1.0f / invrd.Y;
            invrd.Z = 1.0f / invrd.Z;

            if (invrd.X >= 0.0f)
            {
                tmin = (bb.Min.X - origin.X) * invrd.X;
                tmax = (bb.Max.X - origin.X) * invrd.X;
            }
            else
            {
                tmin = (bb.Max.X - origin.X) * invrd.X;
                tmax = (bb.Min.X - origin.X) * invrd.X;
            }

            if (invrd.Y >= 0.0f)
            {
                tymin = (bb.Min.Y - origin.Y) * invrd.Y;
                tymax = (bb.Max.Y - origin.Y) * invrd.Y;
            }
            else
            {
                tymin = (bb.Max.Y - origin.Y) * invrd.Y;
                tymax = (bb.Min.Y - origin.Y) * invrd.Y;
            }

            if (tmin > tymax || tymin > tmax)
            {
                return(false);
            }
            if (tymin > tmin)
            {
                tmin = tymin;
            }
            if (tymax < tmax)
            {
                tmax = tymax;
            }

            if (invrd.Z >= 0.0f)
            {
                tzmin = (bb.Min.Z - origin.Z) * invrd.Z;
                tzmax = (bb.Max.Z - origin.Z) * invrd.Z;
            }
            else
            {
                tzmin = (bb.Max.Z - origin.Z) * invrd.Z;
                tzmax = (bb.Min.Z - origin.Z) * invrd.Z;
            }

            if (tmin > tzmax || tzmin > tmax)
            {
                return(false);
            }
            if (tzmin > tmin)
            {
                tmin = tzmin;
            }
            if (tzmax < tmax)
            {
                tmax = tzmax;
            }

            if (tmin < 0)
            {
                tmin = tmax;
            }
            if (tmax < 0)
            {
                return(false);
            }

            float t = tmin;

            hitPosition = origin + t * direction;

            Vector3 AABBCenter = (bb.Min + bb.Max) * 0.5f;

            Vector3 dir = hitPosition - AABBCenter;

            Vector3 width = bb.Max - bb.Min;

            width.X = Math.Abs(width.X);
            width.Y = Math.Abs(width.Y);
            width.Z = Math.Abs(width.Z);

            Vector3 ratio = Vector3.One;

            ratio.X = Math.Abs(dir.X / width.X);
            ratio.Y = Math.Abs(dir.Y / width.Y);
            ratio.Z = Math.Abs(dir.Z / width.Z);

            hitNormal = Vector3.Zero;
            int maxDir = 0; // x

            if (ratio.X >= ratio.Y && ratio.X >= ratio.Z)
            { // x is the greatest
                maxDir = 0;
            }
            else if (ratio.Y >= ratio.X && ratio.Y >= ratio.Z)
            { // y is the greatest
                maxDir = 1;
            }
            else if (ratio.Z >= ratio.X && ratio.Z >= ratio.Y)
            { // z is the greatest
                maxDir = 2;
            }

            if (dir[maxDir] > 0)
            {
                hitNormal[maxDir] = 1.0f;
            }
            else
            {
                hitNormal[maxDir] = -1.0f;
            }

            return(true);
        }
Exemplo n.º 9
0
        public void GetMouseOverObject()
        {
            if (World == null)
            {
                return;
            }

            float radius = 5.5f;

            MouseOverObject final = new MouseOverObject();

            float dist = float.MaxValue;

            Vector3 camPos = Vector3.One * 0.5f + Camera.Pos;

            var air = BlockRegistry.GetBlock <BlockAir>();

            for (float z = -radius; z <= radius; z++)
            {
                for (float y = -radius; y <= radius; y++)
                {
                    for (float x = -radius; x <= radius; x++)
                    {
                        Vector3 vec = camPos;
                        vec.X += x;
                        vec.Y += y;
                        vec.Z += z;

                        float f = (vec - Camera.Pos).LengthFast;

                        if (f <= radius + 0.5f)
                        {
                            BlockPos   pos   = new BlockPos(vec);
                            BlockState state = World.GetBlockState(pos);

                            if (state.Block != air)
                            {
                                AxisAlignedBb bb = state.Block.BoundingBox.Offset(pos.ToVec());

                                bool hitSomething = RayHelper.RayIntersectsBB(Camera.Pos,
                                                                              Camera.GetLookVec(), bb, out Vector3 hitPos, out Vector3 normal);

                                if (hitSomething)
                                {
                                    FaceSides sideHit = FaceSides.Null;

                                    if (normal.X < 0)
                                    {
                                        sideHit = FaceSides.West;
                                    }
                                    else if (normal.X > 0)
                                    {
                                        sideHit = FaceSides.East;
                                    }
                                    if (normal.Y < 0)
                                    {
                                        sideHit = FaceSides.Down;
                                    }
                                    else if (normal.Y > 0)
                                    {
                                        sideHit = FaceSides.Up;
                                    }
                                    if (normal.Z < 0)
                                    {
                                        sideHit = FaceSides.North;
                                    }
                                    else if (normal.Z > 0)
                                    {
                                        sideHit = FaceSides.South;
                                    }

                                    BlockPos p = new BlockPos(hitPos - normal * bb.Size / 2);

                                    if (sideHit == FaceSides.Null)
                                    {
                                        continue;
                                    }

                                    float l = Math.Abs((Camera.Pos - (p.ToVec() + bb.Size / 2)).Length);

                                    if (l < dist)
                                    {
                                        dist = l;

                                        final.hit      = HitType.Block;
                                        final.hitVec   = hitPos;
                                        final.blockPos = p;
                                        final.normal   = normal;
                                        final.sideHit  = sideHit;

                                        final.boundingBox = bb;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            MouseOverObject = final;
        }