Skip to content

Commit

Permalink
changed collision detection, more realistic
Browse files Browse the repository at this point in the history
  • Loading branch information
YueLi28 committed Nov 12, 2014
1 parent dbfcc37 commit 56edffd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion code/include/sr-circleModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class CircleModel : public sf::CircleShape {
public:
bool _movable;
bool draw;
bool intersects(sf::CircleShape *other);
bool intersects(sf::CircleShape *);
bool intersects(sf::CircleShape *,float);
sf::Vector2f getSpd();
void setSpd(sf::Vector2f);
Animation* getAnimation();
Expand Down
15 changes: 12 additions & 3 deletions code/src/sr-circleModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ void CircleModel::setSpd(sf::Vector2f speed){
bool CircleModel::intersects(sf::CircleShape *other) {
sf::Vector2f diff = getPosition() - other->getPosition();
float radiusSum = getRadius() + other->getRadius();
if (radiusSum < fabs(diff.x) || radiusSum < fabs(diff.y)) return false;
if (radiusSum * radiusSum < diff.x*diff.x + diff.y*diff.y) return false;
return true;
if (radiusSum * radiusSum >= diff.x*diff.x + diff.y*diff.y)
return true;
else
return false;
}

bool CircleModel::intersects(sf::CircleShape *other, float distance) {
sf::Vector2f diff = getPosition() - other->getPosition();
if (distance * distance >= diff.x*diff.x + diff.y*diff.y)
return true;
else
return false;
}

void CircleModel::setPosition(sf::Vector2f pos) {
Expand Down
8 changes: 4 additions & 4 deletions code/src/sr-control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ void Control::update(float timeInterval) {
//Cow
for (int j=0; j < _cows.size(); j++) {
Cow* cow = _cows[j];
if (_ship->intersects(cow)) {
if (_ship->intersects(cow, cow->getRadius())) {
_removeModel(cow);
_hud->setcow(_hud->getcow()+1);
_gsound.collect();
}
}
//Ranch
if (_ship->intersects(_ranch)) {
if (_ship->intersects(_ranch,_ranch->getRadius())) {
_gsound.complete();
std::cout << "space ranch reached" << std::endl;
_levelfinished = true;
Expand Down Expand Up @@ -141,7 +141,7 @@ void Control::update(float timeInterval) {

//Wormhole
for (int j=0; j < _wormholes.size();j++){ //Wormhole implementation
if (_ship->intersects(_wormholes[j])){ //Run into Wormhole
if (_ship->intersects(_wormholes[j], _wormholes[j]->getRadius())){ //Run into Wormhole
if (_wormholes[j]->getOpen() == true){
_wormholes[j]->setOpen(false);
int target;
Expand Down Expand Up @@ -187,7 +187,7 @@ void Control::update(float timeInterval) {
/* cow intersect */
for (int j=0; j < _cows.size(); j++) {
Cow* cow = _cows[j];
if (lasso->intersects(cow)) {
if (lasso->intersects(cow, cow->getRadius())) {
_gsound.collect();
_removeModel(cow);
_hud->setcow(_hud->getcow()+1);
Expand Down
4 changes: 2 additions & 2 deletions code/src/sr-models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void Models::parse(){
case 'S':
width = x * 100 - 50;
height = y * 100;
_circlemodels.push_back(new Ship(sf::Vector2f(width,height), 30, 5));
_circlemodels.push_back(new Ship(sf::Vector2f(width,height), 20, 5));
_circlemodels.push_back(((Ship*) _circlemodels.back())->getLasso());
break;

Expand All @@ -113,7 +113,7 @@ void Models::parse(){
case 'P':
width = x * 100 - 50;
height = y * 100;
_circlemodels.push_back(new Planet(sf::Vector2f(width,height),30,100));
_circlemodels.push_back(new Planet(sf::Vector2f(width,height),30,50));
break;

case 'R':
Expand Down

0 comments on commit 56edffd

Please # to comment.