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

Author Topic: collision system for a platform rpg  (Read 1350 times)

0 Members and 1 Guest are viewing this topic.

jonas

  • Newbie
  • *
  • Posts: 3
    • View Profile
collision system for a platform rpg
« on: September 17, 2011, 11:45:25 am »
hello everybody, my second topic over here :P

i've got a question about collision: I need a good working collision sytem for a platform rpg like the titel says. I've wrote my own collision system, but it doesn't work if i use more than 1 object in the game. So if i draw 1 object, a platform, in the game, i can walk on it , jump on it, and let the gravity do it's work. This works perfect...But not with more than 1 object or platform, so when i draw platforms, the collision will only work on 1 of the 2 platforms. If i have platform1 and platform2, it will only work for example on platform2. I'll be able to jump and walk on platform 2, but not on platform1. I just go through the platform number1...

this is the code i wrote:
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

int main()
{
    //integers voor de coördinaten bij te houden
    int spritePosX = 20;
    int spritePosY = 25;
    int floorPosX = 0;
    int floorPosY = 160;
    int platformPosX = 250;
    int platformPosY = 140;
    //bool voor het springen als de grond geraakt wordt
    bool canJump = true;
    //bool voor de collision
    bool isCollision = false;

    //window renderen
    sf::RenderWindow game(sf::VideoMode(640, 320, 32), "engine test");

    //maak een rood plaatje voor de speler
    sf::Image imgRed;
    //maak er een vierkantje van
    imgRed.Create(32, 32, sf::Color(0xff, 0x00, 0x00));
    //maak een rode texture
    sf::Texture textureRed;
    //laad het plaatje in de rode texture
    textureRed.LoadFromImage(imgRed);
    //maak een sprite van de rode texture
    sf::Sprite player(textureRed);
    //zet de player op positie
    player.SetX(20);
    player.SetY(25);

    //maak een blauwe ondergrond
    sf::Image imgBlue;
    //maak er een rechthoek van
    imgBlue.Create(640, 160, sf::Color(0x00, 0x00, 0xff));
    //maak een blauwe texture
    sf::Texture textureBlue;
    //laad de ondergrond in de blauwe texture
    textureBlue.LoadFromImage(imgBlue);
    //maak een sprite van de ondergrond
    sf::Sprite floor(textureBlue);
    //zet de sprite op positie
    floor.SetX(0);
    floor.SetY(160);

    //maak een blauw platje voor een platform
    sf::Image imgPlatform;
    //maak er een rechthoekje van
    imgPlatform.Create(50, 10, sf::Color(0x00, 0x00, 0xff));
    //maak een blauwe texture
    sf::Texture texturePlatform;
    //laad het platform in de texture
    texturePlatform.LoadFromImage(imgPlatform);
    //maak een sprite van het platform
    sf::Sprite platform(texturePlatform);
    //zet de sprite op positie
    platform.SetX(250);
    platform.SetY(140);


    //clock instellen om ervoor te zorgen dat het lopen op elke computer even snel gaat
    sf::Clock clock;

    while (game.IsOpened())
    {
        if (clock.GetElapsedTime() >= 5)
        {
            if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Up) && canJump == true)
            {
                player.Move(0, -60);
                spritePosY -= 60;
                canJump = false;
            }
            if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Left))
            {
                player.Move(-2, 0);
                spritePosX -= 2;
            }
            if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Right))
            {
                player.Move(2, 0);
                spritePosX += 2;
            }

            //de collision met de ondergrond
            if (spritePosX <= floorPosX + 640 && spritePosX + 32 >= floorPosX && spritePosY <= floorPosY + 160 && spritePosY + 32 >= floorPosY)
            {
                isCollision = true;
                canJump = true;
            }
            else
            {
                isCollision = false;
                player.Move(0, 2);
                spritePosY += 2;
            }

            //de collision met het platform
            if (spritePosX <= platformPosX + 50 && spritePosX + 32 >= platformPosX && spritePosY <= platformPosY + 10 && spritePosY + 32 >= platformPosY)
            {
                isCollision = true;
                canJump = true;
                player.Move(0, 0);
            }
            else
            {
                isCollision = false;
            }

            clock.Reset();
        }



        //scherm clearen
        game.Clear();
        //sprites tekenen
        game.Draw(floor);
        game.Draw(player);
        game.Draw(platform);
        //geef de window weer
        game.Display();
    }
    return 0;

}
 


de commments with the code are written in Dutch, but i think it's clear to understand wath happens in the code, because it isn't that difficult code.

In the code there is the player, a platform and a floor. I think it was the platform where i can stand on, but i don't remember which one it was. But i do know it was one of the two that doesn't work...So does one of you guys know what's wrong with this code? I allready tried to make a variable  and get the changes of the player in that variable and than let the player move to the changed coordinates that are in that one variable, but it doesn't work at all, so i'll do not post that code here.

If someone knows what's wrong with the code here, please tell it to me, because it's very important for my game to have a good collision system...

And if someone has another code for collision, you can show it too :) I've already seen the collision codes here on the forum, but it isn't really what i ws looking or...It has to work with bounding boxes around the sprite, but here i'm still using some squares for the tests.

Thank you already,

Jonas.
i love it to program with sfml and t, but still have problems installing it on a mac...