I've been struggling with collision for days now, i couldn't find a proper tutorial that i could understand, i just want my character to stop when it is touching another object, and please don't tell me a algorithm because i just can't implement algorithms into to code i just can't, here is my game:
#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <Windows.h>
#define Accelerate 9
#define Deccelerate 10
int main()
{
sf::RenderWindow window(sf::VideoMode(600, 600), "Platformer !");
sf::Clock deltaClock;
sf::View view(window.getDefaultView());
float deltaTime;
sf::RectangleShape Player;
sf::RectangleShape BLOK;
sf::Vector2f Velocity = sf::Vector2f(0, 0);
sf::Vector2f Position = sf::Vector2f(300, 0);
sf::Vector2f Size = sf::Vector2f(50, 50);
bool IsGrounded = false;
bool UpWalltouching = false;
bool Walltouching = false;
bool WalltouchingRight = false;
bool WalltouchingLeft = false;
bool Objtouching = false;
float Gravity = 0;
float NormalGravity = 700;
float SlowGravity = 350;
float DownGravity = 1500;
float gLimit = 30.f;
float JumpHeight = -12;
float Acceleration = 1;
float accelerator = 1;
float Speed = 100000;
float Horizontal = 0;
float Vertical = 0;
Player.setSize(Size);
Player.setFillColor(sf::Color(150, 40, 70, 200));
BLOK.setSize(Size + Size + Size);
BLOK.setFillColor(sf::Color(250, 130, 40, 150));
BLOK.setPosition(sf::Vector2f(200, 500));
//window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
// Game Loop
while (window.isOpen())
{
//Input input;
sf::Event event;
float Framerate = 1 / deltaClock.getElapsedTime().asSeconds();
sf::Time dt = deltaClock.restart();
deltaTime = dt.asSeconds();
if (deltaTime >= 0.0006f)
deltaTime = 0.0006f;
else if (deltaTime <= 0.0003f)
deltaTime = 0.0003f;
while (window.pollEvent(event)) // Events
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
window.setView(view = sf::View(sf::FloatRect(0.f, 0.f,
static_cast<float>(window.getSize().x),
static_cast<float>(window.getSize().y))));
break;
/*case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Space){
//if (IsGrounded){
Velocity.y = JumpHeight;
printf("JUMPIN");
//}
}*/
//break;
}
}
// Update
//Player.setFillColor(sf::Color(rand() % 255, rand() % 255, rand() % 255, rand() % 255));
// Horizontal Input
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
Horizontal = -1;
accelerator = 1;
if (Acceleration <= 0.35)
Acceleration += Accelerate*deltaTime;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
Horizontal = 1;
accelerator = 1;
if (Acceleration <= 0.35)
Acceleration += Accelerate*deltaTime;
}
else {
if (Acceleration > 0)
Acceleration -= Deccelerate*deltaTime;
else
Acceleration = 0;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
Gravity = DownGravity;
}
else
Gravity = NormalGravity;
// Wall Collision
if (Position.x <= 0){
//IsGrounded = true;
Position.x = 0;
Walltouching = true;
WalltouchingLeft = true;
Gravity = SlowGravity;
}
else if (Position.x >= window.getSize().x-Size.x){
//IsGrounded = true;
Position.x = window.getSize().x - Size.x;
Walltouching = true;
WalltouchingRight = true;
Gravity = SlowGravity;
}
else {
Walltouching = false;
WalltouchingRight = false;
WalltouchingLeft = false;
}
// COLLISION
//if (Position.x > BLOK.getPosition().x-Size.x && Position.x <= BLOK.getPosition().x+Size.x && Position.y >= BLOK.getPosition().y-Size.y)
//Position.x = BLOK.getPosition().x-Size.x;
//if (Position.x <= BLOK.getPosition().x+Size.x && Position.x >= BLOK.getPosition().x-Size.x && Position.y >= BLOK.getPosition().y-Size.y)
// Position.x = BLOK.getPosition().x+Size.x;
//if (Position.y >= BLOK.getPosition().y-Size.y && Position.x > BLOK.getPosition().x-Size.x && Position.x < BLOK.getPosition().x+Size.x )
//Position.y = BLOK.getPosition().y-Size.y;
if (Position.y >= BLOK.getPosition().y-Size.y*3/2)
if (Position.x > BLOK.getPosition().x-Size.x*3/2)
if (Position.x < BLOK.getPosition().x+Size.x*3/2)
Position.y = BLOK.getPosition().y-Size.y*3/2;
/*
if (Position.x < BLOK.getPosition().x + 100 &&
Position.x + 50 > BLOK.getPosition().x &&
Position.y < BLOK.getPosition().y + 100 &&
50 + Position.y > BLOK.getPosition().y){
Velocity = sf::Vector2f(0, 0);
Objtouching = true;
printf("\nTOUCHING !");
}
else
Objtouching = false */
// Gravity and down collision
if (Position.y >= window.getSize().y - Size.y){
IsGrounded = true;
Velocity.y = 0;
}
else if (Walltouching){
IsGrounded = true;
if(Velocity.y <= gLimit)
Velocity.y += Gravity*deltaTime;
}
else {
IsGrounded = false;
if(Velocity.y <= gLimit)
Velocity.y += Gravity*deltaTime;
}
if (Position.y <= (window.getSize().y-window.getSize().y)){
UpWalltouching = true;
Position.y = (window.getSize().y-window.getSize().y);
if(Velocity.y <= gLimit)
Velocity.y += Gravity*deltaTime;
}
else
UpWalltouching = false;
// Jumping Input
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
if (IsGrounded && !UpWalltouching){
Velocity.y = JumpHeight;
}
}
Velocity.x = ((Horizontal*Speed)*(Acceleration*accelerator))*deltaTime;
Player.setPosition(Position);
Position += Velocity;
printf("\n: %f", Framerate);
// Draw
window.clear();
window.draw(Player);
window.draw(BLOK);
window.display();
}
return 0;
}