Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "CollideableBall.h"
- #include "Box.h"
- #include "Reflector.h"
- #include "SlowDown.h"
- #include "SpeedUp.h"
- #include <cmath>
- bool pointInBox(int px, int py, Box* box) {
- Point pos = box->GetPosition();
- int size = box->GetSize();
- return (px >= pos.X && px < pos.X + size && py >= pos.Y && py < pos.Y + size);
- }
- int pointDistance(int x1, int y1, int x2, int y2) {
- return sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
- }
- void CollideableBall::UpdateWallCollisions(Vector screenSize) {
- if (position.X - radius < 0 || position.X + radius >= screenSize.X) speed.X *= -1;
- if (position.Y - radius < 0 || position.Y + radius >= screenSize.Y) speed.Y *= -1;
- }
- int CollideableBall::BoxCollision(void* object) {
- Box* obj = (Box*)object;
- Point pos = obj->GetPosition();
- int size = obj->GetSize();
- if (pointDistance(position.X, position.Y, pos.X + size / 2, pos.Y + size / 2) > (radius + size)* 2) return 0;
- // X collision
- if (pointInBox(position.X + radius, position.Y, obj)) return 1;
- if (pointInBox(position.X - radius, position.Y, obj)) return 1;
- // Y collision
- if (pointInBox(position.X, position.Y + radius, obj)) return 2;
- if (pointInBox(position.X, position.Y - radius, obj)) return 2;
- // Top left corner collision
- if (pointDistance(position.X, position.Y, pos.X, pos.Y) < radius) return 3;
- // Top right corner collision
- if (pointDistance(position.X, position.Y, pos.X + size, pos.Y) < radius) return 3;
- // Bottom left corner collision
- if (pointDistance(position.X, position.Y, pos.X, pos.Y + size) < radius) return 3;
- // Bottom right corner collision
- if (pointDistance(position.X, position.Y, pos.X + size, pos.Y + size) < radius) return 3;
- return 0;
- }
- void CollideableBall::UpdateCollisionReflector(Reflector* object) {
- int collide = BoxCollision(object);
- if (collide == 1 || collide == 3) speed.X *= -1;
- if (collide == 2 || collide == 3) speed.Y *= -1;
- }
- void CollideableBall::UpdateCollisionSlowDown(SlowDown* object) {
- if (BoxCollision(object)) {
- speed.X *= 0.95;
- speed.Y *= 0.95;
- }
- }
- void CollideableBall::UpdateCollisionSpeedUp(SpeedUp* object) {
- if (BoxCollision(object)) {
- speed.X *= 1.05;
- speed.Y *= 1.05;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment