Пример #1
0
        private void ApplyWind(Vector2 position)
        {
            Vector2 min = Vector2.Subtract(position, new Vector2(0, 100));
            Vector2 max = Vector2.Add(position, new Vector2(300, 100));

            AABB aabb = new AABB(ref min, ref max);

            foreach (Body body in _physicsSimulator.BodyList)
            {
                if (aabb.Contains(body.Position))
                {
                    body.ApplyForce(new Vector2(400,0));
                }
            }
        }
Пример #2
0
        private void ApplyExplosion(Vector2 position)
        {
            Vector2 min = Vector2.Subtract(position, new Vector2(100, 100));
            Vector2 max = Vector2.Add(position, new Vector2(100, 100));

            AABB aabb = new AABB(ref min, ref max);

            foreach (Body body in _physicsSimulator.BodyList)
            {
                if (aabb.Contains(body.Position))
                {
                    Vector2 fv = body.Position;
                    fv = Vector2.Subtract(fv, position);
                    fv.Normalize();
                    fv = Vector2.Multiply(fv, 50000);
                    body.ApplyForce(fv);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Computes the grid.
        /// </summary>
        /// <param name="geometry">The geometry.</param>
        /// <param name="gridCellSize">Size of the grid cell.</param>
        public void ComputeGrid(Geom geometry, float gridCellSize)
        {
            //Prepare the geometry.
            Matrix old = geometry.Matrix;

            //TODO: Assign geometry.Matrix to Matrix.Identity directly
            Matrix identity = Matrix.Identity;
            geometry.Matrix = identity;

            //Copy the AABB to the grid field
            _aabb = new AABB(geometry.AABB);
            _gridCellSize = gridCellSize;
            _gridCellSizeInv = 1/gridCellSize;

            //NOTE: Using double cast instead of converting.
            int xSize = (int) Math.Ceiling((double) (_aabb.Max.X - _aabb.Min.X)*_gridCellSizeInv) + 1;
            int ySize = (int) Math.Ceiling((double) (_aabb.Max.Y - _aabb.Min.Y)*_gridCellSizeInv) + 1;

            //TODO: Possible optimization (normal)! If the shape is symmetric in X and Y axis, don't calculate the points, replicate them.
            _nodes = new float[xSize,ySize];
            _points = new Vector2[xSize*ySize];
            int i = 0;
            Vector2 vector = _aabb.Min;
            for (int x = 0; x < xSize; ++x, vector.X += gridCellSize)
            {
                vector.Y = _aabb.Min.Y;
                for (int y = 0; y < ySize; ++y, vector.Y += gridCellSize)
                {
                    _nodes[x, y] = geometry.GetNearestDistance(vector); // shape.GetDistance(vector);
                    _points[i] = vector;
                    i += 1;
                }
            }
            //restore the geometry
            geometry.Matrix = old;
        }
 public bool Intersect(ref AABB aabb)
 {
     return AABB.Intersect(ref aabb, ref _aabb);
 }
Пример #5
0
        // This is used in my editing application.
        /// <summary>
        /// Gets index of control point if point is inside it.
        /// </summary>
        /// <param name="point">Point to test against.</param>
        /// <returns>Index of control point or -1 if no intersection.</returns>
        public int PointInControlPoint(Vector2 point)
        {
            AABB controlPointAABB;

            foreach (Vector2 controlPoint in _controlPoints)
            {
                controlPointAABB =
                    new AABB(
                        new Vector2(controlPoint.X - (_controlPointSize/2), controlPoint.Y - (_controlPointSize/2)),
                        new Vector2(controlPoint.X + (_controlPointSize/2), controlPoint.Y + (_controlPointSize/2)));

                if (controlPointAABB.Contains(ref point))
                    return _controlPoints.IndexOf(controlPoint);
            }
            return -1;
        }
Пример #6
0
        /// <summary>
        /// Initialize the wave controller.
        /// </summary>
        public void Initialize()
        {
            _xPosition = new float[_nodeCount];
            _currentWave = new float[_nodeCount];
            _previousWave = new float[_nodeCount];
            _resultWave = new float[_nodeCount];

            for (int i = 0; i < _nodeCount; i++)
            {
                _xPosition[i] = MathHelper.Lerp(_position.X, _position.X + _width, (float) i/(_nodeCount - 1));
                _currentWave[i] = 0;
                _previousWave[i] = 0;
                _resultWave[i] = 0;
            }

            _aabb = new AABB(_position, new Vector2(_position.X + _width, _position.Y + _height));
            _singleWaveWidth = _width/(_nodeCount - 1);
        }
Пример #7
0
 /// <summary>
 /// Calculates the grid cell size from AABB.
 /// </summary>
 /// <param name="aabb">The AABB.</param>
 /// <returns></returns>
 private float CalculateGridCellSizeFromAABB(ref AABB aabb)
 {
     return aabb.GetShortestSide() * _gridCellSizeAABBFactor;
 }
Пример #8
0
        /// <summary>
        /// Computes the grid.
        /// </summary>
        /// <param name="geom">The geometry.</param>
        /// <exception cref="ArgumentNullException"><c>geometry</c> is null.</exception>
        public void CreateDistanceGrid(Geom geom)
        {
            if (geom == null)
                throw new ArgumentNullException("geom", "Geometry can't be null");

            //Don't create distancegrid for geometry that already have one.
            //NOTE: This should be used to -update- the geometry's distance grid (if grid cell size has changed).
            if (_distanceGrids.ContainsKey(geom.id))
                return;

            //By default, calculate the gridcellsize from the AABB
            if (geom.GridCellSize <= 0)
                geom.GridCellSize = CalculateGridCellSizeFromAABB(ref geom.AABB);

            //Prepare the geometry. Reset the geometry matrix
            Matrix old = geom.Matrix;
            geom.Matrix = Matrix.Identity;

            //Create data needed for gridcalculations
            AABB aabb = new AABB(ref geom.AABB);
            float gridCellSizeInv = 1 / geom.GridCellSize;

            //Note: Physics2d have +2
            int xSize = (int)Math.Ceiling((double)(aabb.Max.X - aabb.Min.X) * gridCellSizeInv) + 1;
            int ySize = (int)Math.Ceiling((double)(aabb.Max.Y - aabb.Min.Y) * gridCellSizeInv) + 1;

            float[,] nodes = new float[xSize, ySize];
            Vector2 vector = aabb.Min;
            for (int x = 0; x < xSize; ++x, vector.X += geom.GridCellSize)
            {
                vector.Y = aabb.Min.Y;
                for (int y = 0; y < ySize; ++y, vector.Y += geom.GridCellSize)
                {
                    nodes[x, y] = geom.GetNearestDistance(ref vector); // shape.GetDistance(vector);
                }
            }
            //restore the geometry
            geom.Matrix = old;

            DistanceGridData distanceGridData = new DistanceGridData();
            distanceGridData.AABB = aabb;
            distanceGridData.GridCellSize = geom.GridCellSize;
            distanceGridData.GridCellSizeInv = gridCellSizeInv;
            distanceGridData.Nodes = nodes;

            _distanceGrids.Add(geom.id, distanceGridData);
        }
Пример #9
0
 public AABB(AABB aabb)
 {
     min = aabb.Min;
     max = aabb.Max;
 }
Пример #10
0
        /// <summary>
        /// tells you if drawing the texture will actually draw onscreen.
        /// </summary>
        /// <param name="tex">
        /// the texture to check.
        /// </param>
        /// <param name="position">
        /// the position of the texture's center.
        /// </param>
        /// <param name="origin">
        /// a Vector2 equaling half of the texture's size.
        /// </param>
        /// <param name="rotation">
        /// the rotation of the texture, in radians.
        /// </param>
        /// <returns>
        /// a bool indicating whether you should draw this texture.
        /// </returns>
        public bool ShouldDraw(Texture2D tex, Vector2 position, Vector2 origin, float rotation)
        {
            Matrix textureMatrix = Matrix.CreateRotationZ(rotation) * Matrix.CreateTranslation(new Vector3(position, 0));
            Vector2 topLeft = Vector2.Transform(-origin, textureMatrix);
            Vector2 topRight = Vector2.Transform(new Vector2(origin.X, -origin.Y), textureMatrix);
            Vector2 botLeft = Vector2.Transform(new Vector2(-origin.X, origin.Y), textureMatrix);
            Vector2 botRight = Vector2.Transform(origin, textureMatrix);
            AABB texAABB = new AABB();
            if (rotation >= 0 && rotation <= MathHelper.PiOver2)
            {
                Vector2 v1 = new Vector2(botLeft.X, topLeft.Y);
                Vector2 v2 = new Vector2(topRight.X, botRight.Y);
                texAABB = new AABB(ref v1, ref v2);
            }
            else if (rotation > MathHelper.PiOver2 && rotation <= MathHelper.Pi)
            {
                Vector2 v1 = new Vector2(botRight.X, botLeft.Y);
                Vector2 v2 = new Vector2(topLeft.X, topRight.Y);
                texAABB = new AABB(ref v1, ref v2);
            }
            else if (rotation > MathHelper.Pi && rotation <= (MathHelper.Pi + MathHelper.PiOver2))
            {
                Vector2 v1 = new Vector2(botRight.X, topRight.Y);
                Vector2 v2 = new Vector2(botLeft.X, topLeft.Y);
                
                texAABB = new AABB(ref v1, ref v2);
            }
            else if (rotation > (MathHelper.Pi + MathHelper.PiOver2) && rotation <= MathHelper.TwoPi)
            {
                Vector2 v1 = new Vector2(topLeft.X, topRight.Y);
                Vector2 v2 = new Vector2(botRight.X, botLeft.Y);
                texAABB = new AABB(ref v1, ref v2);
            }


            Matrix simpleCameraMatrix = Matrix.Identity *
                Matrix.CreateTranslation(new Vector3(-_position, 0)) *
                Matrix.CreateRotationZ(_rotation) *
                Matrix.CreateTranslation(new Vector3(-_size / 2, 0));
            Vector2 camOrigin = (_size * (1 / _zoom));
            topLeft = Vector2.Transform(-camOrigin, simpleCameraMatrix);
            topRight = Vector2.Transform(new Vector2(camOrigin.X, -camOrigin.Y), simpleCameraMatrix);
            botLeft = Vector2.Transform(new Vector2(-camOrigin.X, camOrigin.Y), simpleCameraMatrix);
            botRight = Vector2.Transform(camOrigin, simpleCameraMatrix);
            AABB camAABB = new AABB();

            if (rotation >= 0 && rotation <= MathHelper.PiOver2)
            {
                Vector2 v1 = new Vector2(botLeft.X, topLeft.Y);
                Vector2 v2 = new Vector2(topRight.X, botRight.Y);
                texAABB = new AABB(ref v1, ref v2);
            }
            else if (rotation > MathHelper.PiOver2 && rotation <= MathHelper.Pi)
            {
                Vector2 v1 = new Vector2(botRight.X, botLeft.Y);
                Vector2 v2 = new Vector2(topLeft.X, topRight.Y);
                texAABB = new AABB(ref v1, ref v2);
            }
            else if (rotation > MathHelper.Pi && rotation <= (MathHelper.Pi + MathHelper.PiOver2))
            {
                Vector2 v1 = new Vector2(botRight.X, topRight.Y);
                Vector2 v2 = new Vector2(botLeft.X, topLeft.Y);
                texAABB = new AABB(ref v1, ref v2);
            }
            else if (rotation > (MathHelper.Pi + MathHelper.PiOver2) && rotation <= MathHelper.TwoPi)
            {
                Vector2 v1 = new Vector2(topLeft.X, topRight.Y);
                Vector2 v2 = new Vector2(botRight.X, botLeft.Y);
                texAABB = new AABB(ref v1, ref v2);
            }

            if (camAABB.Contains(texAABB.Min) || camAABB.Contains(texAABB.Max))
                return true;
            return false;
        }
Пример #11
0
 /// <summary>
 /// Get all intersections between a line segment and an AABB. 
 /// </summary>
 /// <param name="point1">The first point of the line segment to test</param>
 /// <param name="point2">The second point of the line segment to test.</param>
 /// <param name="aabb">The AABB that is used for testing intersection.</param>
 /// <param name="intersectionPoints">An list of intersection points. Any intersection points found will be added to this list.</param>
 public static void LineSegmentAABBIntersect(ref Vector2 point1, ref Vector2 point2, AABB aabb, ref List<Vector2> intersectionPoints)
 {
     LineSegmentVerticesIntersect(ref point1, ref point2, aabb.GetVertices(), ref intersectionPoints);
 }
Пример #12
0
 public override void loadcontent(master master)
 {
     base.textureName = "player";
     shieldtexture = master.content.Load<Texture2D>("shield");
     base.loadcontent(master);
     base.playernumber = base.id.NodeID + 1;
     if (geom.OnCollision == null)
     {
         this.geom.OnCollision += this.oncollide;
     }
     
     //Load the players weapon
     pweapon.loadcontent(master);
     pweapon.playernumber = this.playernumber;
     //Load the players tower
     ptower.loadcontent(master);
     ptower.playernumber = this.playernumber;
     ptower.t_weapon.modifyfirerate(2);
     //custom physics values for the player
     body.IsQuadraticDragEnabled = true;
     body.Mass = 25;
     body.LinearDragCoefficient = body.Mass * 2f;
     body.QuadraticDragCoefficient = body.Mass * .85f;
     body.RotationalDragCoefficient = 1;            
     geom.CollisionGroup = playernumber;
     base.interval = _towerspawnrate;
     Vector2 vec0 = Vector2.Zero;
     Vector2 size = new Vector2(this.texture.Width * 3, this.texture.Height * 3);
     aabb = new AABB(ref vec0, ref size);
 }
Пример #13
0
 //misc
 /// <summary>
 /// Calculates the grid cell size from AABB.
 /// </summary>
 /// <param name="vertices">The vertices.</param>
 /// <returns></returns>
 public float CalculateGridCellSizeFromAABB(Vertices vertices)
 {
     AABB aabb = new AABB(vertices);
     return aabb.GetShortestSide()*_gridCellSizeAABBFactor;
 }
Пример #14
0
        // This is used in my editing application.
        /// <summary>
        /// Gets index of control point if point is inside it.
        /// </summary>
        /// <param name="point">Point to test against.</param>
        /// <returns>Index of control point or -1 if no intersection.</returns>
        public int PointInControlPoint(Vector2 point)
        {
            AABB controlPointAABB;
            Vector2 temp1;
            Vector2 temp2;

            foreach (Vector2 controlPoint in _controlPoints)
            {
                temp1 = new Vector2(controlPoint.X - (_controlPointSize / 2), controlPoint.Y - (_controlPointSize / 2));
                temp2 = new Vector2(controlPoint.X + (_controlPointSize / 2), controlPoint.Y + (_controlPointSize / 2));

                controlPointAABB = new AABB(ref temp1, ref temp2);

                if (controlPointAABB.Contains(ref point))
                    return _controlPoints.IndexOf(controlPoint);
            }
            return -1;
        }
Пример #15
0
 public AABBFluidContainer(AABB aabb)
 {
     _aabb = aabb;
 }
Пример #16
0
        /// <summary>
        /// Check if 2 AABB's intersects
        /// </summary>
        /// <param name="aabb1">The aabb1.</param>
        /// <param name="aabb2">The aabb2.</param>
        /// <returns></returns>
        public static bool Intersect(AABB aabb1, AABB aabb2)
        {
            if (aabb1.min.X > aabb2.max.X || aabb2.min.X > aabb1.max.X)
            {
                return false;
            }

            if (aabb1.min.Y > aabb2.Max.Y || aabb2.min.Y > aabb1.Max.Y)
            {
                return false;
            }
            return true;
        }
Пример #17
0
 public bool Intersect(AABB aabb)
 {
     return AABB.Intersect(aabb, _aabb);
 }
Пример #18
0
 /// <summary>
 /// Get all intersections between a line segment and an AABB.
 /// </summary>
 /// <param name="p1">The first point of the line segment to test</param>
 /// <param name="p2">The second point of the line segment to test.</param>
 /// <param name="aabb">The AABB that is used for testing intersection.</param>
 /// <param name="points">An list of intersection points. Any intersection points found will be added to this list.</param>
 public static void LineSegmentAABBIntersect(ref Vector2 p1, ref Vector2 p2, AABB aabb, ref List <Vector2> points)
 {
     LineSegmentVerticiesIntersect(ref p1, ref p2, aabb.GetVertices(), ref points);
 }