Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 86 additions & 6 deletions Asteroid.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
#include "Asteroid.h"

Asteroid::Asteroid() {

//set collision status
possible_collision = false;

// Check movementSpeed setting when star background is working
movementSpeed = 363.0;

// Create the asteroid
// Can be exchanged with other functions to create other ships
setMercuryPoints();

setPosition(-300, -200);
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.setOrigin(radius, radius);
hitradius.setPosition(getPosition().x, getPosition().y);
hitradius.setRadius(radius);
hitradius.setFillColor(sf::Color::Transparent);
hitradius.setOutlineColor(sf::Color::Green);
hitradius.setOutlineThickness(3.f);

}




// 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));
Expand Down Expand Up @@ -121,22 +163,42 @@ 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(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));
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);
hitbox.scale(0.35f, 0.35f);
}



// Overridden draw function
void Asteroid::draw(sf::RenderTarget& target, sf::RenderStates states)const {
states.transform *= getTransform();

target.draw(hitradius);
if (possible_collision) {
target.draw(hitbox);
}

target.draw(Body, states);

}


Expand All @@ -151,3 +213,21 @@ 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;
}
}
22 changes: 20 additions & 2 deletions Asteroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ class Asteroid : public GameObject {
Asteroid();
void draw(sf::RenderTarget& target, sf::RenderStates states)const;
void update(sf::Time dt);
//void move(sf::Time dt);
void move(sf::Time dt);
void setHitboxPoints();


// prototype collision methods
float getRadius();
void setPossibleCollision(bool possible);




private:

Expand All @@ -21,6 +30,15 @@ class Asteroid : public GameObject {
sf::ConvexShape Body;
float movementSpeed;
sf::Vector2f movement;



// protoype collision data
float radius = 30;
sf::CircleShape hitradius;
bool possible_collision;
sf::ConvexShape hitbox;
int num_hitbox_points;
sf::Vector2f* hitbox_points;

};
#endif
37 changes: 37 additions & 0 deletions PrototypeScene.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "PrototypeScene.h"
#include "SystemClass.h"
#include <string>
#include <math.h>



Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -119,4 +123,37 @@ 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 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 = 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);
}
else {
ship.setPossibleCollision(false);
asteroid.setPossibleCollision(false);
}
}
17 changes: 15 additions & 2 deletions PrototypeScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
const int SYSTEMOBJECTS = 13;





class Background : public GameObject {
public:
Background();
Expand All @@ -35,16 +38,26 @@ class PrototypeScene : public Scene {
void draw(sf::RenderWindow& window);
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;
Ship ship;
AlienShip* alien;
Star* sun;
Planet** planetarySystemObjects;

// collision type objects
Ship ship;
Asteroid asteroid;
sf::Vector2f center;

sf::Vector2f center;
sf::View view;

bool debug = true;

};
#endif
Loading