Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- using Engine;
- namespace Collision
- {
- class Level : State
- {
- public override void Create()
- {
- base.Create(); //call the base create function
- //create a new small ball and set its position
- GameObject sCircle = new SmallBall();
- sCircle.Position.X = -200;
- ObjectManager.AddGameObject(sCircle);
- //Create the large circle, set it to the right of the screen
- GameObject Rect2 = new GameObject("Rect2", 64, 128, "RectVertical.png");
- Rect2.Position.X = 200;
- ObjectManager.AddGameObject(Rect2);
- }
- bool CheckCircleCircleCollision(PointF c1Position, float c1Radius,
- PointF c2Position, float c2Radius)
- {
- //Calculate the squared sum of radius1 and radius2
- float radiiSquared = (c1Radius + c2Radius) * (c1Radius + c2Radius);
- //calculate the change in x by subtracting the x positions of the circles
- float deltaX = c1Position.X - c2Position.X;
- //calculate the change in y by subtracting the y positions of the circles
- float deltaY = c1Position.Y - c2Position.Y;
- //calculate the distance squared using pythagoreans theroem (x^2 + y^2 = c^2)
- float distanceSqaured = deltaX * deltaX + deltaY * deltaY;
- //If the distance squared is <= radii squared
- if (distanceSqaured <= radiiSquared)
- {
- //colliding, return true
- return true;
- }
- //otherwise, return false.
- return false;
- }
- bool CheckRectangleRectangleCollision(PointF r1Position, float r1HalfWidth, float r1HalfHeight,
- PointF r2Position, float r2HalfWidth, float r2HalfHeight)
- {
- //Left: Check if r1's right side is further left than r2's left side
- if (r1Position.X + r1HalfWidth < r2Position.X - r2HalfWidth)
- {
- //if it is, there can bo no collision, so return false.
- return false;
- }
- //Right: check if r1's left side is further right than r2's right side
- if (r1Position.X - r1HalfWidth > r2Position.X + r1HalfWidth)
- {
- //if it is, there can be no collision, so return false.
- return false;
- }
- //Bottom: Check if r1's top side is below r2's bottom side
- if (r1Position.Y + r1HalfHeight < r2Position.Y - r2HalfHeight)
- {
- //if it is, there can be no collision so return false.
- return false;
- }
- //Top: Check r1's bottom side is above r2's top side.
- if (r1Position.Y - r1HalfHeight > r2Position.Y + r2HalfHeight)
- {
- //if it is, there can be no collision so return false
- return false;
- }
- //if none of those conditions are true, return true, as anything left is a collision
- return true;
- }
- bool CheckPointCircleCollision(PointF pPos, PointF cPos, float cRadius)
- {
- //Calculate the squared radius.
- float radiusSquared = cRadius * cRadius;
- //subtract the X positions of the point and the circle
- float deltaX = cPos.X - pPos.X;
- //subtract the y positions of the point and circle
- float deltaY = cPos.Y - pPos.Y;
- //Calculate the squared distance
- float distanceSquared = deltaX * deltaX + deltaY * deltaY;
- //if the distance is less than the radius
- if (distanceSquared < radiusSquared)
- {
- //we are colliding
- return true;
- }
- //if we get here, then we are not collding.
- return false;
- }
- public override void Update()
- {
- base.Update();
- //float smallRadius = 32;
- //float largeRadius = 64;
- //half widths and heights of first rectangle
- float halfWidth1 = 64;
- float halfHeight1 = 32;
- //half widths and heights of second rectangle
- float halfWidth2 = 32;
- float halfHeight2 = 64;
- //bool to hold collision return
- bool collision = false;
- //find rect1 and rect2 and store them in my own variables
- GameObject Rect1 = ObjectManager.GetObjectByName("Rect1");
- GameObject Rect2 = ObjectManager.GetObjectByName("Rect2");
- //check for collision between circles
- //collision = CheckCircleCircleCollision(sCircle.Position, smallRadius, lCircle.Position, largeRadius);
- //check for collision between rectangles
- collision = CheckRectangleRectangleCollision(Rect1.Position, halfWidth1, halfHeight1, Rect2.Position, halfWidth2, halfHeight2);
- //check if collision is true or false
- if (collision)
- {
- //reset the position of the small circle
- Rect1.Position.X = -200;
- Rect1.Position.Y = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement