DavidNorgren

Untitled

Apr 23rd, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #pragma once
  2. #include "CollideableBall.h"
  3. #include "Box.h"
  4. #include "Reflector.h"
  5. #include "SlowDown.h"
  6. #include "SpeedUp.h"
  7. #include <cmath>
  8.  
  9. bool pointInBox(int px, int py, Box* box) {
  10.     Point pos = box->GetPosition();
  11.     int size = box->GetSize();
  12.     return (px >= pos.X && px < pos.X + size && py >= pos.Y && py < pos.Y + size);
  13. }
  14. int pointDistance(int x1, int y1, int x2, int y2) {
  15.     return sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
  16. }
  17. void CollideableBall::UpdateWallCollisions(Vector screenSize) {
  18.     if (position.X - radius < 0 || position.X + radius >= screenSize.X) speed.X *= -1;
  19.     if (position.Y - radius < 0 || position.Y + radius >= screenSize.Y) speed.Y *= -1;
  20. }
  21.  
  22. int CollideableBall::BoxCollision(void* object) {
  23.     Box* obj = (Box*)object;
  24.     Point pos = obj->GetPosition();
  25.     int size = obj->GetSize();
  26.  
  27.     if (pointDistance(position.X, position.Y, pos.X + size / 2, pos.Y + size / 2) > (radius + size)* 2) return 0;
  28.  
  29.     // X collision
  30.     if (pointInBox(position.X + radius, position.Y, obj)) return 1;
  31.     if (pointInBox(position.X - radius, position.Y, obj)) return 1;
  32.  
  33.     // Y collision
  34.     if (pointInBox(position.X, position.Y + radius, obj)) return 2;
  35.     if (pointInBox(position.X, position.Y - radius, obj)) return 2;
  36.    
  37.     // Top left corner collision
  38.     if (pointDistance(position.X, position.Y, pos.X, pos.Y) < radius) return 3;
  39.  
  40.     // Top right corner collision
  41.     if (pointDistance(position.X, position.Y, pos.X + size, pos.Y) < radius) return 3;
  42.  
  43.     // Bottom left corner collision
  44.     if (pointDistance(position.X, position.Y, pos.X, pos.Y + size) < radius) return 3;
  45.  
  46.     // Bottom right corner collision
  47.     if (pointDistance(position.X, position.Y, pos.X + size, pos.Y + size) < radius) return 3;
  48.  
  49.     return 0;
  50. }
  51.  
  52. void CollideableBall::UpdateCollisionReflector(Reflector* object) {
  53.     int collide = BoxCollision(object);
  54.     if (collide == 1 || collide == 3) speed.X *= -1;
  55.     if (collide == 2 || collide == 3) speed.Y *= -1;
  56. }
  57.  
  58. void CollideableBall::UpdateCollisionSlowDown(SlowDown* object) {
  59.     if (BoxCollision(object)) {
  60.         speed.X *= 0.95;
  61.         speed.Y *= 0.95;
  62.     }
  63. }
  64.  
  65. void CollideableBall::UpdateCollisionSpeedUp(SpeedUp* object) {
  66.     if (BoxCollision(object)) {
  67.         speed.X *= 1.05;
  68.         speed.Y *= 1.05;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment