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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - drinkadriu

Pages: [1]
1
General / Re: Implementing Collision Detection ?
« on: January 24, 2016, 01:41:59 pm »
The basic collision detection and response is done by using sf::FloatRect:


sf::RectangleShape player;
sf::RectangleShape object;

sf::FloatRect  player_bounds = player.getGlobalBounds();
sf::FloatRect  object_bounds = object.getGlobalBounds();

//  check if two entities collides and make an adequate response:

if (object_bounds.intersects(player_bounds)
{
    // Response
}

// for single pixels, use ".contains" - usually used for checking if mouse is inside object.

sf::Vector2f   mouse_pos;
if (object_bounds.contains(mouse_pos)
{
    // Response;
}

 

Also, keep in mind that bounds of entities always needs to be updated, so after you have moved your entities elsewhere, you need also to call .getGlobalBounds() to it's respective sf::FloatRect again.

That is not the problem im affraid, i know how to check if the player is colliding with object, what i want to do is stop the player from going through other objects !

2
General / Implementing Collision Detection ?
« on: January 23, 2016, 04:09:10 pm »
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;
        }

3
General / deltaTime changes drastically when window is resized ?
« on: January 19, 2016, 10:36:45 pm »
I've been working in a platformer and until now everything is working fine.I wanted the game to be something new so i added a feature where you can resize the window to resize the camera, here are some screenshots:
http://imgur.com/gallery/DAHRV/new
but the problem is that when i resize the window deltaTime changes drastically, and i don't want that because it changes the gameplay(like the jumpheight, speed, etc), i don't know how to fix this and here is the source code:
#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <Windows.h>

int main()
{
        sf::RenderWindow window(sf::VideoMode(600, 600), "Platformer !");
        sf::Clock deltaClock;
        sf::View view(window.getDefaultView());
        float deltaTime;

        // Start

        float Gravity = 0;
        float NormalGravity = 2.6;
        float SlowGravity = 0.87;
        float DownGravity = 25;
        float JumpHeight = -1.0;
        float Acceleration = 1;
        bool IsGrounded = false;
        bool Walltouching = false;
        bool WalltouchingRight = false;
        bool WalltouchingLeft = false;
        sf::Vector2f Velocity = sf::Vector2f(0, 0);
        sf::Vector2f Position = sf::Vector2f(0, 0);
        sf::Vector2f Size = sf::Vector2f(50, 50);
        float Speed = 800;
        float Horizontal = 0;
        float Vertical = 0;
        sf::RectangleShape Player;
        Player.setSize(Size);
        Player.setFillColor(sf::Color(150, 40, 70, 200));
        sf::RectangleShape BLOK;
        BLOK.setSize(Size + Size);
        BLOK.setFillColor(sf::Color(250, 130, 40, 150));
        BLOK.setPosition(sf::Vector2f(500, 900));

        // Game Loop
        while (window.isOpen())
        {
                sf::Event event;
                sf::Time dt = deltaClock.restart();
                deltaTime = dt.asSeconds();

                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

                        // Horizontal Input

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
                                Horizontal = -1;
                                if (Acceleration <= 2)
                                        Acceleration += 0.4*deltaTime;
                        }
                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
                                Horizontal = 1;
                                if (Acceleration <= 2)
                                        Acceleration += 0.*deltaTime;
                        }
                        else {
                                if (Acceleration >= 0.01)
                                        Acceleration = 1;
                                if (Horizontal > 0)
                                        Horizontal -= 2*deltaTime;
                                else if (Horizontal < 0)
                                        Horizontal += 2*deltaTime;
                                else
                                        Horizontal = 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-50){
                                //IsGrounded = true;
                                Position.x = window.getSize().x - 50;
                                Walltouching = true;
                                WalltouchingRight = true;
                                Gravity = SlowGravity;
                        }
                        else {
                                Walltouching = false;
                                WalltouchingRight = false;
                                WalltouchingLeft = false;
                        }

                        // COLLISION

                        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){
                                printf("\nTOUCHING !");
                        }
                        else{
                                //system("cls");
                        }

                        // Gravity and down collision

                        if (Position.y >= window.getSize().y - 50){
                                IsGrounded = true;
                                Velocity.y = 0;
                        }
                        else if (Walltouching){
                                IsGrounded = true;
                                Velocity.y += Gravity*deltaTime;
                        }
                        else {
                                IsGrounded = false;
                                Velocity.y += Gravity*deltaTime;
                        }

                        // Jumping Input
                       
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
                                if (IsGrounded){
                                        Velocity.y = JumpHeight;
                                }
                        }
                       
                        // Movement Calculations

                        //14JumpHeight = -(window.getSize().x / window.getSize().x / 1.5);
                        Velocity.x = Horizontal*Acceleration*Speed*deltaTime;
                        Player.setPosition(Position);
                        Position += Velocity;
                        printf("\nPOSITION Y:     %f", Position.y);

                        // Draw
                        window.clear();
                        window.draw(Player);
                        window.draw(BLOK);
                        window.display();
                }

                return 0;
        }

4
General / Re: Can't fix SFML linker errors
« on: January 17, 2016, 03:02:53 pm »
You most likely defined SFML_STATIC even though you're linking SFML dynamically. You need to follow the tutorial by word not just by image. ;)

Also you should just upgrade to VS2015 Community while you're at it.

lol i should've read the instructions carefully, minutes after i posted this i fixed it but i couldnt remove this post.. But still thanks

5
General / Can't fix SFML linker errors[SOLVED]
« on: January 12, 2016, 09:12:21 pm »
hello fellow programmers i found out about sfml 2 weeks ago, and it amazed me with simplicity and efficiencie, today i installed VS2013 Community version and i wanted to install SFML 2.3.2.I used the tutorial on the official sfml page, i did every step right, everything and my code doesn't have compilier errors, it has linker errors, i searched but i couldn't find a solution, i get these errors:


Error   1   error LNK2001: unresolved external symbol "public: static class sf::RenderStates const sf::RenderStates::Default" (?Default@RenderStates@sf@@2V12@B)   c:\Users\drin-_000\documents\visual studio 2013\Projects\SFMLGAME\SFMLGAME\Main.obj   SFMLGAME
Error   2   error LNK2001: unresolved external symbol "public: static class sf::Color const sf::Color::Green" (?Green@Color@sf@@2V12@B)   c:\Users\drin-_000\documents\visual studio 2013\Projects\SFMLGAME\SFMLGAME\Main.obj   SFMLGAME
Error   3   error LNK1120: 2 unresolved externals   c:\Users\drin-_000\documents\visual studio 2013\Projects\SFMLGAME\Debug\SFMLGAME.exe   1   1   SFMLGAME

i have no idea whatsoever what to do, please help me !

Pages: [1]
anything