1
Graphics / 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:
And this is the terrain's :
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;
}
#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;
}
#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;
}