From 064451c9dd1a42442435f72e81591f1140f32a41 Mon Sep 17 00:00:00 2001 From: TaylorKNoah <73961491+TaylorKNoah@users.noreply.github.com> Date: Mon, 2 Aug 2021 15:37:59 -0700 Subject: [PATCH 1/5] Add outer hit radius to ship/ast. add hitbox to ast. draw in sys.cpp --- Asteroid.cpp | 33 +++++++++++++++++++++++++++------ Asteroid.h | 5 +++++ Ship.cpp | 14 +++++++++++++- Ship.h | 8 ++++++++ System.cpp | 11 ++++++++--- 5 files changed, 61 insertions(+), 10 deletions(-) diff --git a/Asteroid.cpp b/Asteroid.cpp index bd28662..39a875c 100644 --- a/Asteroid.cpp +++ b/Asteroid.cpp @@ -7,8 +7,17 @@ Asteroid::Asteroid() { // Create the asteroid // Can be exchanged with other functions to create other ships setMercuryPoints(); - - setPosition(0, 0); + setHitboxPoints(); + setPosition(200, 200); + + //circle anchor is at pi rads on circumference (instead of center) + // origin is thus offset by radius and height of gameobject to center around the object + hitradius.setPosition(getPosition().x - radius, getPosition().y-100); + hitradius.setRadius(radius); + hitradius.setFillColor(sf::Color::Transparent); + hitradius.setOutlineColor(sf::Color::Green); + hitradius.setOutlineThickness(3.f); + } @@ -122,21 +131,33 @@ void Asteroid::setMercuryPoints() { Body.setPoint(102, sf::Vector2f(0.f, 58.f)); - - - - Body.setOutlineThickness(3.f); Body.setFillColor(sf::Color(184,115,52,255)); //Body.scale(1f, 1f); } +void Asteroid::setHitboxPoints() { + hitbox.setPointCount(6); + hitbox.setPoint(0, sf::Vector2f(-60.f, 0.f)); + hitbox.setPoint(1, sf::Vector2f(-30.f, 50.f)); + hitbox.setPoint(2, sf::Vector2f(30.f, 50.f)); + hitbox.setPoint(3, sf::Vector2f(60.f, 0.f)); + hitbox.setPoint(4, sf::Vector2f(30.f, -50.f)); + hitbox.setPoint(5, sf::Vector2f(-30.f, -50.f)); + + hitbox.setOutlineThickness(3.f); + hitbox.setFillColor(sf::Color::Transparent); + hitbox.setOutlineColor(sf::Color::Red); +} + // Overridden draw function void Asteroid::draw(sf::RenderTarget& target, sf::RenderStates states)const { states.transform *= getTransform(); target.draw(Body, states); + target.draw(hitbox, states); + target.draw(hitradius); } diff --git a/Asteroid.h b/Asteroid.h index 0caaf01..53082c3 100644 --- a/Asteroid.h +++ b/Asteroid.h @@ -13,14 +13,19 @@ class Asteroid : public GameObject { void draw(sf::RenderTarget& target, sf::RenderStates states)const; void update(sf::Time dt); void move(sf::Time dt); + void setHitboxPoints(); private: //in future: add argument to load different shape asteroids void setMercuryPoints(); sf::ConvexShape Body; + sf::ConvexShape hitbox; float movementSpeed; sf::Vector2f movement; + + float radius = 100; + sf::CircleShape hitradius; }; #endif \ No newline at end of file diff --git a/Ship.cpp b/Ship.cpp index 1fb4ab2..8357285 100644 --- a/Ship.cpp +++ b/Ship.cpp @@ -18,6 +18,17 @@ Ship::Ship(){ hitbox.scale(0.75f, 0.75f); setPosition(0, 0); + + //circle anchor is at pi rads on circumference (instead of center) + // origin is thus offset by radius and height of gameobject to center around the object + hitradius.setOrigin(radius, origin[1]+62); + hitradius.setRadius(radius); + hitradius.setFillColor(sf::Color::Transparent); + hitradius.setOutlineColor(sf::Color::Green); + hitradius.setOutlineThickness(3.f); + hitradius.scale(0.75, 0.75); + + setOrigin(origin[0], origin[1]); } @@ -90,7 +101,8 @@ void Ship::setBlueShipPoints(sf::ConvexShape * shape) { void Ship::draw(sf::RenderTarget& target, sf::RenderStates states)const{ states.transform *= getTransform(); target.draw(body, states); - target.draw(hitbox, states); + target.draw(hitbox, states); + target.draw(hitradius, states); } diff --git a/Ship.h b/Ship.h index 61330e4..57b1b7c 100644 --- a/Ship.h +++ b/Ship.h @@ -20,6 +20,14 @@ class Ship : public GameObject { float movementSpeed; sf::Vector2f movement; sf::ConvexShape hitbox; + + // if two gameObjects are within thier radii it should trigger hitbox checking + // minimizes hitbox checking + float radius = 100.0; + sf::CircleShape hitradius; + //Anchor point of the object + // used in tandum with hitradius + int origin[2] = { 0, 16 }; }; #endif \ No newline at end of file diff --git a/System.cpp b/System.cpp index 9cbd805..8c8dc86 100644 --- a/System.cpp +++ b/System.cpp @@ -71,7 +71,7 @@ void SystemClass::runWindow() { sf::Time timer; //Currently not used // Remove this when done testing ship implementation - //Ship ship; + Ship ship; Asteroid asteroid; while (window.isOpen()) { //This is the game loop @@ -93,11 +93,16 @@ void SystemClass::runWindow() { // Remove the next 8 lines when done testing ship implementation // Draw the ship // This is just here for testing + ship.move(dt); + ship.update(dt); + window.draw(ship); + view.setCenter(ship.getPosition()); + window.setView(view); + asteroid.move(dt); asteroid.update(dt); window.draw(asteroid); - view.setCenter(asteroid.getPosition()); - window.setView(view); + window.display(); } From 6bfd3acfe62c36d698ec77d4afa99edeb6119dd9 Mon Sep 17 00:00:00 2001 From: TaylorKNoah <73961491+TaylorKNoah@users.noreply.github.com> Date: Mon, 2 Aug 2021 17:54:54 -0700 Subject: [PATCH 2/5] add get radius and set collion bool methods to ship/ast --- Asteroid.cpp | 39 +++++++++++++++++++++++++++- Asteroid.h | 9 ++++++- Ship.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++----- Ship.h | 11 +++++++- 4 files changed, 123 insertions(+), 9 deletions(-) diff --git a/Asteroid.cpp b/Asteroid.cpp index 3b2bf54..3c368aa 100644 --- a/Asteroid.cpp +++ b/Asteroid.cpp @@ -1,6 +1,11 @@ #include "Asteroid.h" Asteroid::Asteroid() { + + //set collision status + possible_collision = false; + collision = false; + // Check movementSpeed setting when star background is working movementSpeed = 363.0; @@ -12,7 +17,7 @@ Asteroid::Asteroid() { //circle anchor is at pi rads on circumference (instead of center) // origin is thus offset by radius and height of gameobject to center around the object - hitradius.setPosition(getPosition().x - radius, getPosition().y-100); + hitradius.setPosition(getPosition().x - radius, getPosition().y-30); hitradius.setRadius(radius); hitradius.setFillColor(sf::Color::Transparent); hitradius.setOutlineColor(sf::Color::Green); @@ -148,6 +153,7 @@ void Asteroid::setHitboxPoints() { hitbox.setOutlineThickness(3.f); hitbox.setFillColor(sf::Color::Transparent); hitbox.setOutlineColor(sf::Color::Red); + hitbox.scale(0.35f, 0.35f); } @@ -172,3 +178,34 @@ void Asteroid::update(sf::Time dt) { //void Asteroid::move(sf::Time dt) { // sf::Transformable::move(movement * dt.asSeconds()); //} + + +// getter function for Radius. There is no setter and this function returns a copy. +float Asteroid::getRadius() { + return radius; +} + +// sets the class boolean 'possible_collision' +void Asteroid::setPossibleCollision(bool possible) { + + if (possible) { + possible_collision = true; + } + + else { + possible_collision = false; + collision = false; + } +} + +// sets the class boolean 'collision' +void Asteroid::setCollision(bool was_collision) { + + if (was_collision) { + collision = true; + } + + else { + collision = false; + } +} \ No newline at end of file diff --git a/Asteroid.h b/Asteroid.h index 53082c3..ec6d8d1 100644 --- a/Asteroid.h +++ b/Asteroid.h @@ -14,6 +14,11 @@ class Asteroid : public GameObject { void update(sf::Time dt); void move(sf::Time dt); void setHitboxPoints(); + + // prototype collision methods + float getRadius(); + void setPossibleCollision(bool possible); + void setCollision(bool was_collision); private: @@ -24,8 +29,10 @@ class Asteroid : public GameObject { float movementSpeed; sf::Vector2f movement; - float radius = 100; + float radius = 30; sf::CircleShape hitradius; + bool possible_collision; + bool collision; }; #endif \ No newline at end of file diff --git a/Ship.cpp b/Ship.cpp index 9783fbd..81b9745 100644 --- a/Ship.cpp +++ b/Ship.cpp @@ -2,6 +2,10 @@ Ship::Ship(): movementSpeed(SPEED), accelerating(false) { + // init collisions to false + possible_collision = false; + collision = false; + // Create the ship // setBlueShipPoints(&body); // body.setOutlineThickness(3.f); @@ -13,10 +17,21 @@ Ship::Ship(): movementSpeed(SPEED), accelerating(false) { body.resize(7); // setBlueShipPoints(&hitbox); - // hitbox.setOutlineThickness(3.f); - // hitbox.setFillColor(sf::Color::Transparent); - // hitbox.setOutlineColor(sf::Color::Red); - // hitbox.scale(0.75f, 0.75f); + hitbox.setOutlineThickness(1.f); + hitbox.setFillColor(sf::Color::Transparent); + hitbox.setOutlineColor(sf::Color::Red); + hitbox.scale(2.0f, 2.0f); + hitbox.setOrigin(0, 23); + + // Make Ship hitbox + hitbox.setPointCount(4); + hitbox.setPoint(0, sf::Vector2f(-5.f, 22.f)); + hitbox.setPoint(1, sf::Vector2f(5.f, 22.f)); + hitbox.setPoint(2, sf::Vector2f(5.f, 50.f)); + hitbox.setPoint(3, sf::Vector2f(-5.f, 50.f)); + + + // Make main body of ship body[0].position = sf::Vector2f(0.f, 0.f); @@ -98,14 +113,16 @@ Ship::Ship(): movementSpeed(SPEED), accelerating(false) { //circle anchor is at pi rads on circumference (instead of center) // origin is thus offset by radius and height of gameobject to center around the object - hitradius.setOrigin(radius, origin[1]+62); + hitradius.setOrigin(radius, 23+47); hitradius.setRadius(radius); hitradius.setFillColor(sf::Color::Transparent); hitradius.setOutlineColor(sf::Color::Green); hitradius.setOutlineThickness(3.f); hitradius.scale(0.75, 0.75); - setOrigin(origin[0], origin[1]); + setRotation(180); + + setOrigin(0, 23); } @@ -178,13 +195,25 @@ void Ship::setBlueShipPoints(sf::ConvexShape * shape) { // Overridden draw function void Ship::draw(sf::RenderTarget& target, sf::RenderStates states)const{ states.transform *= getTransform(); + + // for debug use + target.draw(hitradius, states); + // for debug use + // if possible collision show hitbox + + if(possible_collision) target.draw(hitbox, states); + target.draw(body, states); + + target.draw(thruster1, states); target.draw(thruster2, states); if (accelerating) { target.draw(flame1, states); target.draw(flame2, states); } + + } @@ -228,3 +257,35 @@ void Ship::update(sf::Time dt){ //void Ship::move(sf::Time dt){ // sf::Transformable::move(movement * dt.asSeconds()); //} + + +// getter function for Radius. There is no setter and this function returns a copy. +float Ship::getRadius() { + return radius; +} + + +// sets the class boolean 'possible_collision' +void Ship::setPossibleCollision(bool possible) { + + if (possible) { + possible_collision = true; + } + + else { + possible_collision = false; + collision = false; + } +} + +// sets the class boolean 'collision' +void Ship::setCollision(bool was_collision) { + + if (was_collision) { + collision = true; + } + + else { + collision = false; + } +} \ No newline at end of file diff --git a/Ship.h b/Ship.h index fbea667..8373208 100644 --- a/Ship.h +++ b/Ship.h @@ -16,6 +16,11 @@ class Ship : public GameObject { void update(sf::Time dt); //void move(sf::Time dt); + // prototype collision methods + float getRadius(); + void setPossibleCollision(bool possible); + void setCollision(bool was_collision); + private: void setMercuryPoints(sf::ConvexShape * shape); void setBlueShipPoints(sf::ConvexShape * shape); @@ -26,8 +31,12 @@ class Ship : public GameObject { sf::VertexArray flame1; sf::VertexArray flame2; float movementSpeed; + bool accelerating; sf::Vector2f movement; + sf::ConvexShape hitbox; + bool possible_collision; + bool collision; // if two gameObjects are within thier radii it should trigger hitbox checking // minimizes hitbox checking @@ -36,6 +45,6 @@ class Ship : public GameObject { //Anchor point of the object // used in tandum with hitradius - int origin[2] = { 0, 16 }; + //int origin[2] = { 0, 16 }; }; #endif \ No newline at end of file From d9744ec9d105aaa78ced46a1e9b9c5074ff2039d Mon Sep 17 00:00:00 2001 From: TaylorKNoah <73961491+TaylorKNoah@users.noreply.github.com> Date: Mon, 2 Aug 2021 17:56:02 -0700 Subject: [PATCH 3/5] add incomplete collision check method to prototype scene --- PrototypeScene.cpp | 38 ++++++++++++++++++++++++++++++++++++++ PrototypeScene.h | 7 +++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/PrototypeScene.cpp b/PrototypeScene.cpp index 091727c..f5d4a86 100644 --- a/PrototypeScene.cpp +++ b/PrototypeScene.cpp @@ -1,6 +1,7 @@ #include "PrototypeScene.h" #include "SystemClass.h" #include +#include @@ -87,6 +88,9 @@ void PrototypeScene::update(sf::Time dt) { alien->update(dt); asteroid.update(dt); sun->update(dt); + + collision_check(); + sf::Vector2f center = sun->getPosition(); for (int i = 0; i < 9; ++i) { @@ -119,4 +123,38 @@ void PrototypeScene::draw(sf::RenderWindow& window) { // Here so the system class can call something to know where the ship is for window.setCenter() sf::Vector2f PrototypeScene::getCenter(){ return ship.getPosition(); +} + + +// Prototype collision detection - will likely use a parent class array of pointers in the future +void PrototypeScene::collision_check() { + + // Step1: get position of objects + sf::Vector2f ship_pos = ship.getPosition(); + sf::Vector2f ast_pos = asteroid.getPosition(); + + // Step2: determine if objs are within eachothers hit radius + // if r1 + r2 <= ||ab||, where r1, r2 are objA and objB radius, and ||ab|| is the distance from pos_A to pos_B + + float rad_total = ship.getRadius() + asteroid.getRadius(); + + sf::Vector2f ab = ship_pos - ast_pos; + + float ab_length = ab.x * ab.x; + ab_length += ab.y * ab.y; + ab_length = sqrt(ab_length); + + if (rad_total <= ab_length) { + // send signal to objects + ship.setPossibleCollision(true); + asteroid.setPossibleCollision(true); + + //Step 2.1: Call hitbox_detection + + //Step 2.2: Signal objects there is a collision, else signal there is not + } + else { + ship.setPossibleCollision(false); + asteroid.setPossibleCollision(false); + } } \ No newline at end of file diff --git a/PrototypeScene.h b/PrototypeScene.h index 3aabc3f..b7d7de4 100644 --- a/PrototypeScene.h +++ b/PrototypeScene.h @@ -33,17 +33,20 @@ class PrototypeScene : public Scene { void update(sf::Time dt); void draw(sf::RenderWindow& window); + void collision_check(); sf::Vector2f getCenter(); private: Background bg; - Ship ship; AlienShip* alien; Star* sun; Planet** planetarySystemObjects; + + // collision type objects + Ship ship; Asteroid asteroid; - sf::Vector2f center; + sf::Vector2f center; sf::View view; }; From aec3a1d366f95ef4ee5109fd113ff6581be910a8 Mon Sep 17 00:00:00 2001 From: TaylorKNoah <73961491+TaylorKNoah@users.noreply.github.com> Date: Mon, 2 Aug 2021 17:59:33 -0700 Subject: [PATCH 4/5] fix logic error for possible collision --- PrototypeScene.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PrototypeScene.cpp b/PrototypeScene.cpp index f5d4a86..3a81fdd 100644 --- a/PrototypeScene.cpp +++ b/PrototypeScene.cpp @@ -134,7 +134,7 @@ void PrototypeScene::collision_check() { sf::Vector2f ast_pos = asteroid.getPosition(); // Step2: determine if objs are within eachothers hit radius - // if r1 + r2 <= ||ab||, where r1, r2 are objA and objB radius, and ||ab|| is the distance from pos_A to pos_B + // if r1 + r2 >= ||ab||, where r1, r2 are objA and objB radius, and ||ab|| is the distance from pos_A to pos_B float rad_total = ship.getRadius() + asteroid.getRadius(); @@ -144,7 +144,7 @@ void PrototypeScene::collision_check() { ab_length += ab.y * ab.y; ab_length = sqrt(ab_length); - if (rad_total <= ab_length) { + if (ab_length <= rad_total) { // send signal to objects ship.setPossibleCollision(true); asteroid.setPossibleCollision(true); From ed3385f4fb31429701c4a1a76dd8a2e2f965d865 Mon Sep 17 00:00:00 2001 From: TaylorKNoah <73961491+TaylorKNoah@users.noreply.github.com> Date: Tue, 10 Aug 2021 14:42:47 -0700 Subject: [PATCH 5/5] Out hitbox for ship and asterod. ProtoScene communicates collisions --- Asteroid.cpp | 66 ++++++++++++++++++--------- Asteroid.h | 14 ++++-- PrototypeScene.cpp | 11 ++--- PrototypeScene.h | 12 ++++- Ship.cpp | 111 ++++++++++++++++++++++----------------------- Ship.h | 18 ++++---- 6 files changed, 133 insertions(+), 99 deletions(-) diff --git a/Asteroid.cpp b/Asteroid.cpp index 3c368aa..cef480f 100644 --- a/Asteroid.cpp +++ b/Asteroid.cpp @@ -4,7 +4,6 @@ Asteroid::Asteroid() { //set collision status possible_collision = false; - collision = false; // Check movementSpeed setting when star background is working movementSpeed = 363.0; @@ -12,12 +11,35 @@ Asteroid::Asteroid() { // Create the asteroid // Can be exchanged with other functions to create other ships setMercuryPoints(); - setHitboxPoints(); setPosition(200, 200); + hitbox.setOrigin(0, 0); + num_hitbox_points = 6; + hitbox_points = new sf::Vector2f[num_hitbox_points]; + hitbox_points[0] = { -60.f, 0.f }; + hitbox_points[1] = { -30.f, 50.f }; + hitbox_points[2] = { 30.f, 50.f }; + hitbox_points[3] = { 60.f, 0.f }; + hitbox_points[4] = { 30.f, -50.f }; + hitbox_points[5] = { -30.f, -50.f }; + + hitbox.setPointCount(num_hitbox_points); + for (int i = 0; i < num_hitbox_points; ++i) + hitbox.setPoint(i, hitbox_points[i]); + + + hitbox.setPosition(getPosition().x, getPosition().y); + hitbox.setOutlineThickness(3.f); + hitbox.setFillColor(sf::Color::Transparent); + hitbox.setOutlineColor(sf::Color::Red); + hitbox.scale(0.35f, 0.35f); + + + //circle anchor is at pi rads on circumference (instead of center) // origin is thus offset by radius and height of gameobject to center around the object - hitradius.setPosition(getPosition().x - radius, getPosition().y-30); + hitradius.setOrigin(radius, radius); + hitradius.setPosition(getPosition().x, getPosition().y); hitradius.setRadius(radius); hitradius.setFillColor(sf::Color::Transparent); hitradius.setOutlineColor(sf::Color::Green); @@ -30,6 +52,12 @@ Asteroid::Asteroid() { // Sets points for the asteroid void Asteroid::setMercuryPoints() { + + Body.setOutlineThickness(3.f); + Body.setFillColor(sf::Color(184, 115, 52, 255)); + Body.setOrigin(0, 0); + Body.setPosition(0, 0); + Body.setPointCount(103); Body.setPoint(0, sf::Vector2f(0.f, 60.f)); Body.setPoint(1, sf::Vector2f(0.f, 60.f)); @@ -135,14 +163,16 @@ void Asteroid::setMercuryPoints() { Body.setPoint(101, sf::Vector2f(-6.f, 58.f)); Body.setPoint(102, sf::Vector2f(0.f, 58.f)); + + + Body.setScale(0.35f, 0.35f); - Body.setOutlineThickness(3.f); - Body.setFillColor(sf::Color(184,115,52,255)); - Body.scale(0.3f, 0.3f); + + } void Asteroid::setHitboxPoints() { - hitbox.setPointCount(6); + hitbox.setPointCount(num_hitbox_points); hitbox.setPoint(0, sf::Vector2f(-60.f, 0.f)); hitbox.setPoint(1, sf::Vector2f(-30.f, 50.f)); hitbox.setPoint(2, sf::Vector2f(30.f, 50.f)); @@ -161,9 +191,14 @@ void Asteroid::setHitboxPoints() { // Overridden draw function void Asteroid::draw(sf::RenderTarget& target, sf::RenderStates states)const { states.transform *= getTransform(); - target.draw(Body, states); - target.draw(hitbox, states); + target.draw(hitradius); + if (possible_collision) { + target.draw(hitbox); + } + + target.draw(Body, states); + } @@ -194,18 +229,5 @@ void Asteroid::setPossibleCollision(bool possible) { else { possible_collision = false; - collision = false; - } -} - -// sets the class boolean 'collision' -void Asteroid::setCollision(bool was_collision) { - - if (was_collision) { - collision = true; - } - - else { - collision = false; } } \ No newline at end of file diff --git a/Asteroid.h b/Asteroid.h index ec6d8d1..61fc621 100644 --- a/Asteroid.h +++ b/Asteroid.h @@ -14,25 +14,31 @@ class Asteroid : public GameObject { void update(sf::Time dt); void move(sf::Time dt); void setHitboxPoints(); + // prototype collision methods float getRadius(); void setPossibleCollision(bool possible); - void setCollision(bool was_collision); + + + private: //in future: add argument to load different shape asteroids void setMercuryPoints(); sf::ConvexShape Body; - sf::ConvexShape hitbox; float movementSpeed; sf::Vector2f movement; + + // protoype collision data float radius = 30; sf::CircleShape hitradius; bool possible_collision; - bool collision; - + sf::ConvexShape hitbox; + int num_hitbox_points; + sf::Vector2f* hitbox_points; + }; #endif \ No newline at end of file diff --git a/PrototypeScene.cpp b/PrototypeScene.cpp index 3a81fdd..35a5927 100644 --- a/PrototypeScene.cpp +++ b/PrototypeScene.cpp @@ -133,25 +133,24 @@ void PrototypeScene::collision_check() { sf::Vector2f ship_pos = ship.getPosition(); sf::Vector2f ast_pos = asteroid.getPosition(); - // Step2: determine if objs are within eachothers hit radius + // Step2: determine if objs are within eachothers outer hit radius // if r1 + r2 >= ||ab||, where r1, r2 are objA and objB radius, and ||ab|| is the distance from pos_A to pos_B float rad_total = ship.getRadius() + asteroid.getRadius(); sf::Vector2f ab = ship_pos - ast_pos; + // float ab_length = ab.x * ab.x; - ab_length += ab.y * ab.y; + ab_length += (ab.y * ab.y); ab_length = sqrt(ab_length); + // If outtuer hit radius collides do more work if (ab_length <= rad_total) { // send signal to objects + // visual confirmation ship.setPossibleCollision(true); asteroid.setPossibleCollision(true); - - //Step 2.1: Call hitbox_detection - - //Step 2.2: Signal objects there is a collision, else signal there is not } else { ship.setPossibleCollision(false); diff --git a/PrototypeScene.h b/PrototypeScene.h index b7d7de4..cfffea4 100644 --- a/PrototypeScene.h +++ b/PrototypeScene.h @@ -12,6 +12,9 @@ const int SYSTEMOBJECTS = 13; + + + class Background : public GameObject { public: Background(); @@ -33,9 +36,14 @@ class PrototypeScene : public Scene { void update(sf::Time dt); void draw(sf::RenderWindow& window); - void collision_check(); sf::Vector2f getCenter(); + // prototype collision methods + void collision_check(); + bool hitbox_detection(Ship sh, Asteroid ast); + bool hitbox_detection_2(Ship sh, Asteroid ast); + bool hitbox_detection_3(Ship sh, Asteroid ast); + private: Background bg; AlienShip* alien; @@ -49,5 +57,7 @@ class PrototypeScene : public Scene { sf::Vector2f center; sf::View view; + bool debug = true; + }; #endif \ No newline at end of file diff --git a/Ship.cpp b/Ship.cpp index 81b9745..6cbe95f 100644 --- a/Ship.cpp +++ b/Ship.cpp @@ -1,11 +1,12 @@ #include "Ship.h" +#include +#include Ship::Ship(): movementSpeed(SPEED), accelerating(false) { - // init collisions to false - possible_collision = false; - collision = false; - + + + // Create the ship // setBlueShipPoints(&body); // body.setOutlineThickness(3.f); @@ -17,22 +18,10 @@ Ship::Ship(): movementSpeed(SPEED), accelerating(false) { body.resize(7); // setBlueShipPoints(&hitbox); - hitbox.setOutlineThickness(1.f); - hitbox.setFillColor(sf::Color::Transparent); - hitbox.setOutlineColor(sf::Color::Red); - hitbox.scale(2.0f, 2.0f); - hitbox.setOrigin(0, 23); - - // Make Ship hitbox - hitbox.setPointCount(4); - hitbox.setPoint(0, sf::Vector2f(-5.f, 22.f)); - hitbox.setPoint(1, sf::Vector2f(5.f, 22.f)); - hitbox.setPoint(2, sf::Vector2f(5.f, 50.f)); - hitbox.setPoint(3, sf::Vector2f(-5.f, 50.f)); - - - + + + // Make main body of ship body[0].position = sf::Vector2f(0.f, 0.f); body[1].position = sf::Vector2f(-13.f, 0.f); @@ -108,21 +97,48 @@ Ship::Ship(): movementSpeed(SPEED), accelerating(false) { flame2[1].color = sf::Color(232, 48, 3, 255); flame2[2].color = sf::Color(232, 48, 3, 255); - // setScale(sf::Vector2f(4.f, 4.f)); - setPosition(0, 0); + // setScale(sf::Vector2f(4.f, 4.f)) + // note: position is relative to screen, origin is relative to object + setRotation(180); + setOrigin(0,23); // 0 23 + setPosition(0, 0); + + + + // init collisions data + possible_collision = false; //circle anchor is at pi rads on circumference (instead of center) // origin is thus offset by radius and height of gameobject to center around the object - hitradius.setOrigin(radius, 23+47); hitradius.setRadius(radius); + hitradius.setOrigin(radius, radius); + hitradius.setPosition(0, 23); hitradius.setFillColor(sf::Color::Transparent); hitradius.setOutlineColor(sf::Color::Green); hitradius.setOutlineThickness(3.f); - hitradius.scale(0.75, 0.75); - setRotation(180); + num_hitbox_points = 4; + hitbox_points = new sf::Vector2f[num_hitbox_points]; + + hitbox_points[0] = { -10.f, -23.f }; + hitbox_points[1] = { 10.f, -23.f }; + hitbox_points[2] = { 10.f, 23.f }; + hitbox_points[3] = { -10.f, 23.f }; + + hitbox.setOrigin(0, 33); // 0 33 + hitbox.setPosition(0, 62); //0 62 + //hitbox.setRotation(180); - setOrigin(0, 23); + // Make Ship hitbox + hitbox.setPointCount(num_hitbox_points); + hitbox.setPoint(0, hitbox_points[0]); + hitbox.setPoint(1, hitbox_points[1]); + hitbox.setPoint(2, hitbox_points[2]); + hitbox.setPoint(3, hitbox_points[3]); + + hitbox.setOutlineThickness(1.f); + hitbox.setFillColor(sf::Color::Transparent); + hitbox.setOutlineColor(sf::Color::Red); } @@ -195,34 +211,30 @@ void Ship::setBlueShipPoints(sf::ConvexShape * shape) { // Overridden draw function void Ship::draw(sf::RenderTarget& target, sf::RenderStates states)const{ states.transform *= getTransform(); - - // for debug use + target.draw(hitradius, states); - // for debug use - // if possible collision show hitbox + - if(possible_collision) target.draw(hitbox, states); + if (possible_collision) { + target.draw(hitbox, states); + } + + target.draw(body, states); - target.draw(body, states); - - target.draw(thruster1, states); target.draw(thruster2, states); if (accelerating) { target.draw(flame1, states); target.draw(flame2, states); - } - - + } } - // Move or rotate the ship when keys are pressed -void Ship::update(sf::Time dt){ +void Ship::update(sf::Time dt) { accelerating = false; - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::H)) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::H)) { rotate(-230.f * dt.asSeconds()); } @@ -236,17 +248,17 @@ void Ship::update(sf::Time dt){ } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::K)) { - movement += movementSpeed * dt.asSeconds() * sf::Vector2f(-sin(getRotation() * (3.1415 / 180)), - cos(getRotation() * (3.1415 / 180))); + movement += movementSpeed * dt.asSeconds() * sf::Vector2f(-sin(getRotation() * (3.1415 / 180)), + cos(getRotation() * (3.1415 / 180))); accelerating = true; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::J)) { - movement -= movementSpeed * dt.asSeconds() * sf::Vector2f(-sin(getRotation() * (3.1415 / 180)), + movement -= movementSpeed * dt.asSeconds() * sf::Vector2f(-sin(getRotation() * (3.1415 / 180)), cos(getRotation() * (3.1415 / 180))); } - sf::Transformable::move(movement * dt.asSeconds()); + sf::Transformable::move(movement * dt.asSeconds()); } @@ -274,18 +286,5 @@ void Ship::setPossibleCollision(bool possible) { else { possible_collision = false; - collision = false; - } -} - -// sets the class boolean 'collision' -void Ship::setCollision(bool was_collision) { - - if (was_collision) { - collision = true; - } - - else { - collision = false; } } \ No newline at end of file diff --git a/Ship.h b/Ship.h index 8373208..196d1f3 100644 --- a/Ship.h +++ b/Ship.h @@ -19,7 +19,8 @@ class Ship : public GameObject { // prototype collision methods float getRadius(); void setPossibleCollision(bool possible); - void setCollision(bool was_collision); + + private: void setMercuryPoints(sf::ConvexShape * shape); @@ -34,17 +35,14 @@ class Ship : public GameObject { bool accelerating; sf::Vector2f movement; - sf::ConvexShape hitbox; - bool possible_collision; - bool collision; - - // if two gameObjects are within thier radii it should trigger hitbox checking - // minimizes hitbox checking + // prototype collision data float radius = 100.0; sf::CircleShape hitradius; + bool possible_collision; + int num_hitbox_points; + sf::Vector2f* hitbox_points; + sf::ConvexShape hitbox; + - //Anchor point of the object - // used in tandum with hitradius - //int origin[2] = { 0, 16 }; }; #endif \ No newline at end of file