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

Author Topic: General collision condition  (Read 1284 times)

0 Members and 1 Guest are viewing this topic.

Leonhard_Euler

  • Newbie
  • *
  • Posts: 1
    • View Profile
General collision condition
« on: June 22, 2017, 02:46:03 pm »
Hello! I have recently started my firts project, which consists on a platform game. To create platforms, i've created a terrain class which's constructor takes the x and y coordinates and the lenght desired. After that, i have created a player class, with a bool isOnGround(), for using the return in things like jump function or gravity. My terrain class has an intrect variable (call it Platform), so i can put the condition Platform.contains(player' s feet...) for the isOnGround, and I want to know if there is any way to use a generalised condition (if any of the object's rect contains the player). I have tried to write Terrain::Rect.contains (...)

This is the player class' implementation:
 #include "Player.h"
#include "Terrain.h"

#include <SFML/Graphics.hpp>

Player::Player()
{
    Player_Image.setSize(sf::Vector2f(18,23));
    Player_Tex.loadFromFile("Textures/pixel_right.png");
    Player_Image.setTexture(&Player_Tex);
}

sf::RectangleShape Player::DrawThis()
{
    return Player_Image;
}

bool Player::isOnGround()
{
    if (// Here goes the generalised condition)
        return true;
    else return false;
}
 

And this is the terrain's :

#include "Terrain.h"
#include <SFML/Graphics.hpp>

Terrain::Terrain(int X, int Y, int length, int width)
{
    DirtTex.loadFromFile("Textures/dirt.jpg");
    GroundShape.setTexture(&DirtTex);
    GroundShape.setSize(sf::Vector2f(length*32, width*32));
    GroundShape.setPosition(sf::Vector2f(X, Y));
    Platform.left = X;
    Platform.top = Y;
    Platform.height = width*32;
    Platform.width = length*32;
}

sf::RectangleShape Terrain::DrawThis()
{
    return GroundShape;
}

sf::IntRect Terrain::getRect()
{
    return Platform;
}
 


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: General collision condition
« Reply #1 on: July 01, 2017, 04:15:25 pm »
It sounds like you want to know if you can test to see if a rectangle is overlapping another rectangle.
You can use "intersects" instead of "contains":
https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Rect.php#a566740c8f58e01bb052266f47e7e1011
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*