Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML 2.0 AABB Class Help  (Read 7142 times)

0 Members and 1 Guest are viewing this topic.

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
SFML 2.0 AABB Class Help
« on: January 09, 2014, 11:19:23 pm »
Hi. I'm trying to finish my AABB class, but I'm having some issues getting the two objects to actually collide. As it is at the moment I have a player (25x25 little blue block). He is moving down the screen using Euler Integration. When the player hits the block, the collision is meant to return true and the console print out "Collision". Unfortunately it doesn't.

This is my collision.h header.

#ifndef collision_H
#define collision_H

#include "moveable.h"

#include <SFML\Graphics.hpp>

class collision : public moveable
{
protected:
        vector2D min;
        vector2D max;
public:
        collision(); //<! Default Constructor
        bool AABB(collision & other);
        bool collide;
};
#endif

My cpp:

#include "collision.h"
#include <iostream>

collision::collision()
{

}

bool collision::AABB(collision & other)
{
        if(max.x < other.min.x || min.y > other.max.y)std::cout<<"No Collision\n";return false;
        if(max.y < other.min.y || min.y > other.max.y)std::cout<<"No Collision\n";return false;
        std::cout<<"Collision";return true;
}

I call it in my game.cpp with


void game::collision()
{      
        p.AABB(l);
}

I'm quite sure that my syntax in my game.cpp is incorrect.

In each player and level cpp I am setting the min.x and max.y to the playertexture.getSize().x/y.

Any ideas?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML 2.0 AABB Class Help
« Reply #1 on: January 09, 2014, 11:23:31 pm »
Is there a reason why you don't use sf::Rect? This class template represents an AABB in 2D space, and it already provides a function to check for intersection.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: SFML 2.0 AABB Class Help
« Reply #2 on: January 09, 2014, 11:27:46 pm »
Is there a reason why you don't use sf::Rect? This class template represents an AABB in 2D space, and it already provides a function to check for intersection.

It is a University project and I will probably lose marks for doing that. Later on the sprite will be changed for an actual player, just need to design it first.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML 2.0 AABB Class Help
« Reply #3 on: January 09, 2014, 11:33:50 pm »
It is a University project and I will probably lose marks for doing that.
You're supposed to use SFML, but not use it? ???
Don't you have a clear guideline on what you are allowed to do? It's always a bad idea to reinvent the wheel...

At least have a look at the sf::Rect implementation, it's much simpler and cleaner than your code. I don't think you need a whole class hierarchy for the AABB test; the player class is a different story.

An obvious mistake is the lack of {} for the if statements, this wouldn't happen if you indented the code instead of putting everything on one line...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: SFML 2.0 AABB Class Help
« Reply #4 on: January 09, 2014, 11:38:26 pm »
It is a University project and I will probably lose marks for doing that.
You're supposed to use SFML, but not use it? ???
Don't you have a clear guideline on what you are allowed to do? It's always a bad idea to reinvent the wheel...

At least have a look at the sf::Rect implementation, it's much simpler and cleaner than your code. I don't think you need a whole class hierarchy for the AABB test; the player class is a different story.

An obvious mistake is the lack of {} for the if statements, this wouldn't happen if you indented the code instead of putting everything on one line...

For the past couple of months we've been having lectures on AABB, OBB, GJK. It would seem rather bad to not try and at least implement the stuff I'm spending £9000 a year on to study haha. We all learn somewhere and I would feel a lot better to implement this myself rather then the sf::rect. Not to mention I would learn from it too. And fair enough, I did get a little lazy with the braces, bad programming practice which I have rectified.
« Last Edit: January 09, 2014, 11:53:58 pm by Tweezy »

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: SFML 2.0 AABB Class Help
« Reply #5 on: January 10, 2014, 04:35:33 am »
do you get error when compiling this?
or, it can compile, but doesn't work properly
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 AABB Class Help
« Reply #6 on: January 10, 2014, 09:24:53 am »
do you get error when compiling this?
or, it can compile, but doesn't work properly
The problem was already solved:

An obvious mistake is the lack of {} for the if statements, this wouldn't happen if you indented the code instead of putting everything on one line...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: SFML 2.0 AABB Class Help
« Reply #7 on: January 10, 2014, 10:25:22 am »
It isn't solved. The code would always compile, but it doesn't collide. The two if statements are seeing if there aren't any intersections across the min and max points of my block, so if there are it should return true and print "collision". Even in debugging it always returns false.

I've printed out the two min and max points of the objects and they are returning my player and a block i'm colliding him with, but still no anvil. I've also made another attempt where I pass it two objects:
   
Code: [Select]
bool AABB1(collision object1, collision object2);
Code: [Select]

bool collision::AABB1(collision object1, collision object2)
{
if(object1.max.x < object2.min.x || object1.min.x > object2.max.x){return false;}
if(object1.max.y < object2.min.y || object1.min.y > object2.max.y){return false;}
std::cout<<"Collision";
return true;
}

And then I call it in the game.cpp

Code: [Select]

void game::collision()
{
//p.AABB(l);
p.AABB1(p,l);
}
« Last Edit: January 10, 2014, 10:32:20 am by Tweezy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 AABB Class Help
« Reply #8 on: January 10, 2014, 10:44:12 am »
bool collision::AABB1(collision object1, collision object2)
{
        if(object1.max.x < object2.min.x || object1.min.x > object2.max.x){return false;}
        if(object1.max.y < object2.min.y || object1.min.y > object2.max.y){return false;}
        std::cout<<"Collision";
        return true;
}
 
You need to combine the statements otherwise you'll check the collision on the whole X or Y axis instead of just the rectangle.

        if( (object1.max.x < object2.min.x || object1.min.x > object2.max.x) &&
                (object1.max.y < object2.min.y || object1.min.y > object2.max.y) )
                return false;

        std::cout<<"Collision";
        return true;

Here you'll find how SFML implements it.
« Last Edit: January 10, 2014, 10:45:45 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: SFML 2.0 AABB Class Help
« Reply #9 on: January 10, 2014, 10:45:59 am »
I've tried this before and it always returns collision :/ Thanks for the link.
« Last Edit: January 10, 2014, 10:48:15 am by Tweezy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 AABB Class Help
« Reply #10 on: January 10, 2014, 11:01:02 am »
Ah wait, you were right, they don't need to be connected. It was just my brain playing tricks on me. You're checking if it does not collide, which seems a bit counter intuitive for me. ;D
I'd simply go with a check if it does collide instead.

But regardless, do you print out the positions, just to make sure they are actually what you expect them to be?
« Last Edit: January 10, 2014, 11:03:21 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: SFML 2.0 AABB Class Help
« Reply #11 on: January 10, 2014, 11:05:20 am »
Ah wait, you were right, they don't need to be connected. It was just my brain playing tricks on me. You're checking if it does not collide, which seems a bit counter intuitive for me. ;D
I'd simply go with a check if it does collide instead.

But regardless, do you print out the positions, just to make sure they are actually what you expect them to be?

Haha no worries. I suppose I could test for it to collide, but I feel as if I would still be in the same position as I am now. The checking is correct, and the calling it correct so perhaps the issue is that it isn't storing the dimensions of my objects correctly.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 AABB Class Help
« Reply #12 on: January 10, 2014, 11:31:29 am »
The checking is correct, and the calling it correct so perhaps the issue is that it isn't storing the dimensions of my objects correctly.
Yes, just add some nice std::cout or set a break point for all the numbers and check them manually.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: SFML 2.0 AABB Class Help
« Reply #13 on: January 10, 2014, 01:11:12 pm »
The checking is correct, and the calling it correct so perhaps the issue is that it isn't storing the dimensions of my objects correctly.
Yes, just add some nice std::cout or set a break point for all the numbers and check them manually.

Yeah that is my thinking, once again. Cheers

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: SFML 2.0 AABB Class Help
« Reply #14 on: January 10, 2014, 03:12:03 pm »
Eveythink checks out. Inside my player.cpp, level.cpp default constructor I have this:

Code: [Select]
min.x = playerTexture.getSize().x;
max.y = playerTexture.getSize().y;

And
Code: [Select]

        min.x = blockTexture.getSize().x;
max.y = blockTexture.getSize().y;


This is correct, isn't it?

 

anything