bool IsLocationBlocked(GameObject2D _object, PointF _location, List <GameObject2D> _collidingObjects, out GameObject2D _other) { for (int y = 0; y < _collidingObjects.Count; y++) //Check collisions agains other objects { if (_collidingObjects[y] != _object) { if (_collidingObjects[y].colliding) { if ((_location.X >= _collidingObjects[y].boundingBox.min.X + _collidingObjects[y].gameObjectLocation.X && _location.Y >= _collidingObjects[y].boundingBox.min.Y + _collidingObjects[y].gameObjectLocation.Y) && (_location.X <= _collidingObjects[y].boundingBox.max.X + _collidingObjects[y].gameObjectLocation.X && _location.Y <= _collidingObjects[y].boundingBox.max.Y + _collidingObjects[y].gameObjectLocation.Y)) { _other = _collidingObjects[y]; return(true); } } } } _other = null; return(false); }
public void ApplyCollisionWithVelocity() { if (gameScenes.Count > 0) { List <GameObject2D> collidingObjects = new List <GameObject2D>(); foreach (GameObject obj in gameScenes[currentActiveScene].gameObjects) //Populate colliding objects list { GameObject2D ob = obj as GameObject2D; if (ob != null) { collidingObjects.Add(ob); } } foreach (GameObject2D obj in collidingObjects) //Apply velocity { if (obj.objectVelocity != new Point(0, 0)) { if (obj.objectTag == "ball") { if (obj.gameObjectLocation == new Point(200, 100)) { Console.Out.WriteLine("bazaras seni"); } } GameObject2D other = null; float newLocX = obj.gameObjectLocation.X + obj.objectVelocity.X; float newLocY = obj.gameObjectLocation.Y + obj.objectVelocity.Y; if (obj.colliding) { float biggerVelocity = Math.Abs(obj.objectVelocity.X); //Choose bigger velocity if (Math.Abs(obj.objectVelocity.Y) > Math.Abs(obj.objectVelocity.X)) { biggerVelocity = Math.Abs(obj.objectVelocity.Y); } int startingX = obj.gameObjectLocation.X; int startingY = obj.gameObjectLocation.Y; for (float i = 0.1f; i <= 1; i += 0.1f) { float velocityX = startingX + obj.objectVelocity.X * i; float velocityY = startingY + obj.objectVelocity.Y * i; if (!IsLocationBlocked(obj, new PointF(obj.boundingBox.max.X + velocityX, obj.boundingBox.max.Y + velocityY), collidingObjects, out other) && !IsLocationBlocked(obj, new PointF(obj.boundingBox.min.X + velocityX, obj.boundingBox.min.Y + velocityY), collidingObjects, out other) && !IsLocationBlocked(obj, new PointF(obj.boundingBox.min.X + velocityX, obj.boundingBox.max.Y + velocityY), collidingObjects, out other) && !IsLocationBlocked(obj, new PointF(obj.boundingBox.max.X + velocityX, obj.boundingBox.min.Y + velocityY), collidingObjects, out other) ) { ///If not colliding move up newLocX = velocityX; newLocY = velocityY; } else //Colliding { if (!obj.ignoreCollisionTags.Contains(other.objectTag) && !other.ignoreCollisionTags.Contains(obj.objectTag)) { obj.Collision(other); other.Collision(obj); break; //Break loop if theres and obstacle in the way of objects path depending on velocity } else //Dont mind collision, objects are in ignore list { newLocX = velocityX; newLocY = velocityY; } } } obj.gameObjectLocation.X = (int)Math.Round(newLocX); obj.gameObjectLocation.Y = (int)Math.Round(newLocY); } /* * if (other == null) //If no collision then move up * { * obj.gameObjectLocation.X = newLocX; * obj.gameObjectLocation.Y = newLocY; * } */ } } } }
public void Collision(GameObject2D Collider) { OnCollision?.Invoke(this, Collider); }