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 - krrice

Pages: [1] 2
1
General / Re: Help with Collision
« on: November 02, 2013, 04:59:15 pm »
Thanks for all the replies. Finally figured it out,here is the updated code.

#include <SFML/Graphics.hpp>
#include <iostream>

bool Collision(sf::RectangleShape square,sf::RectangleShape square2)
{
        if(square.getPosition().x + square.getSize().x > square2.getPosition().x &&
                square.getPosition().x < square2.getPosition().x + square2.getSize().x &&
                square.getPosition().y + square.getSize().y > square2.getPosition().y &&
                square.getPosition().y < square2.getPosition().y + square2.getSize().y)
        {
                std::cout << "collide" << std::endl;
                return true;
        }
        else
                return false;
}

int main()
{
        sf::RenderWindow window(sf::VideoMode(800,600), "SFML");
        window.setFramerateLimit(60);
        sf::Clock frameTime;

        sf::RectangleShape square(sf::Vector2f(40.f,40.f));
        square.setFillColor(sf::Color::Red);
        square.setPosition(200.f,500.f);

        sf::RectangleShape square2(sf::Vector2f(40.f,100.f));
        square2.setFillColor(sf::Color::Green);;
        square2.setPosition(275.f,275.f);
       
        sf::Vector2f speed(150.f,150.f);

        while(window.isOpen())
        {
                float dt =  frameTime.restart().asSeconds();
               
                sf::Event event;

                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        square.move(speed.x * dt,0.f);
                        if(Collision(square,square2))
                        {
                                square.setPosition(square2.getPosition().x - square.getSize().x,square.getPosition().y);
                        }
                }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        square.move(-speed.x * dt,0.f);
                        if(Collision(square,square2))
                        {
                                square.setPosition(square2.getPosition().x + square2.getSize().x,square.getPosition().y);
                        }
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        square.move(0.f,speed.y * dt);
                        if(Collision(square,square2))
                        {
                                square.setPosition(square.getPosition().x,square2.getPosition().y - square.getSize().y);
                        }
                }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        square.move(0.f,-speed.y * dt);
                        if(Collision(square,square2))
                        {
                                square.setPosition(square.getPosition().x,square2.getPosition().y + square2.getSize().y);
                        }
                }

                window.clear();
                window.draw(square);
                window.draw(square2);
                window.display();
        }

        return 0;
}
 

2
General / Re: Help with Collision
« on: October 31, 2013, 12:37:02 am »
I've always found it's better to check collision first then move if not colliding than to move first, check collision and move back if you are.

Basically create whatever temp variables you need to check the collision, move the temps, check for collision and only allow the REAL object to move if there is no collision.  If a collision is detected for the temp object then you can calculate the difference of the distance between the two objects and move your REAL object that amount, thus having your real object move directly up against the other object.

This approach generally stops the 'My object1 got stuck inside object2 and now can't get out because my checking is always returning true and thus nothing gets moved" scenario

Thanks, I will try to implement it that way.

3
General / Re: Help with Collision
« on: October 30, 2013, 12:59:34 pm »
The "general problem" I was describing is what happens when you have all four cases uncommented.  I've already said what to do about it.

I have tried it with all cases uncommented. I can get it to work with getting previous position then after collision setting it back but it is not consistent. Sometimes after collision I am not right next to the rectangle.
I am at work right now later I will do the previous position code and post it. Thanks for helping me try to figure this out.

4
General / Re: Help with Collision
« on: October 30, 2013, 03:23:30 am »
Well, after some testing, I still think that third case is testing for a collision from the bottom, not from the top, and that's why it's "not working" when you try a collision from the top.  You do need that fourth/top case.

But there's a more general problem.  Because no collision detection happens during the initial movement, your collision logic doesn't know what the square's previous position was, which means any way you tweak this you're likely to have it guessing wrong and making the square warp around the object it collides with.  Movement and collision should be dealt with at the same time, after input is handled.

I commented out the first and second case and top collision works and if I add

if(square.getPosition().y < square2.getPosition().y + square2.getSize().y)
                                square.setPosition(square.getPosition().x,square2.getPosition().y + square2.getSize().y);
 

the bottom works.

5
General / Re: Help with Collision
« on: October 30, 2013, 02:53:05 am »
You appear to be checking for collisions from the left, right, and bottom...but not the top.  See if simply adding the fourth case fixes it for you.

I was trying to get the top working first, I had the fourth case but removed it for now.

6
General / Help with Collision
« on: October 30, 2013, 02:28:59 am »
Hi, I am having problems with my collisions. I know how 2 check if a collision has happened but having trouble with the sprite repositioning. I some minimal code  to show what I have. Right now the code is working for side collisions but for the top it is not. I know why the commented code isn't working, one the first 2 conditions will be true. Is there something simple that I am not seeing or do I need a lot more code to get this done. I have searched for answers but what I found is way different than what I have.

#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(800,600), "SFML");
        window.setFramerateLimit(60);
        sf::Clock frameTime;

        sf::RectangleShape square(sf::Vector2f(40.f,40.f));
        square.setFillColor(sf::Color::Red);
        square.setPosition(200.f,500.f);

        sf::RectangleShape square2(sf::Vector2f(40.f,100.f));
        square2.setFillColor(sf::Color::Green);;
        square2.setPosition(275.f,275.f);
       
        sf::Vector2f speed(150.f,150.f);

        while(window.isOpen())
        {
                float dt =  frameTime.restart().asSeconds();
               
                sf::Event event;

                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        square.move(speed.x * dt,0.f);
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        square.move(-speed.x * dt,0.f);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        square.move(0.f,speed.y * dt);
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        square.move(0.f,-speed.y * dt);
               

                if(square.getPosition().x + square.getSize().x > square2.getPosition().x &&
                        square.getPosition().x < square2.getPosition().x + square2.getSize().x &&
                        square.getPosition().y + square.getSize().y > square2.getPosition().y &&
                        square.getPosition().y < square2.getPosition().y + square2.getSize().y)
                {        
                        //left side of square 2 collision
                        if(square.getPosition().x < square2.getPosition().x )
                                square.setPosition(square2.getPosition().x - square2.getSize().x,square.getPosition().y);
                        //right side of square2 collision
                        if(square.getPosition().x > square2.getPosition().x)
                                square.setPosition(square2.getPosition().x + square2.getSize().x,square.getPosition().y);
                        //top of square2 collision
                        //if(square.getPosition().y + square.getSize().y > square2.getPosition().y)
                                //square.setPosition(square.getPosition().x,square2.getPosition().y - square.getSize().y);
                       
                }

                window.clear();
                window.draw(square);
                window.draw(square2);
                window.display();
        }

        return 0;
}
 

7
General discussions / Re: SFML + Box2D
« on: April 09, 2013, 08:15:30 pm »
Thanks for the info everyone. What I get from the replies is that most of you like Box2D but don't really use it for your in game physics.

8
General discussions / SFML + Box2D
« on: April 06, 2013, 02:49:04 am »
I was just wondering how may people in the forum use Box2D for physics? I have been learning C++ , Allegro for about 1 1/2 years and sfml 2 for about 6 months. Just wanted some kind of idea on weather I should learn to code my own or use an existing engine like Box2D? Any pointers will be appreciated.

9
General discussions / What physics engine to use?
« on: April 05, 2013, 03:54:27 pm »
Hi,I was wondering what physics engine do you recommend using with SFML 2? I have been learning SFML 2 and Allegro 5 for a while now and would like to start using a physics engine. I do not have any experience with any engine but would like to start.

10
Graphics / Getting Sprite to Jump
« on: January 16, 2012, 04:37:37 am »
Quote from: "Mario"
Dont compare floats using equal (chances are good you'll "miss" your value due to missing precision).

I'd do something like this (pseudo code):
Code: [Select]
float dx = 0, dy = 0;

...

if(key_left)
dx = max(dx - ax * frametime, -maxdx); // accelerate
else if(key_right)
dx = min(dx + ax * frametime, maxdx); // deaccelerate
else
dx *= pow(0.9, frametime); // slow down

if(key_jump && !falling && ! jumping)
dy = -djump; // jump
else
dy = min(dy + gravity * frametime, maxdy); // fall

sprite.Move(dx * frametime, dy * frametime)


To make sure you don't forget your time scaling, just think of the real value representations. A velocity is "distance/time", so you have to scale it once. A acceleration is "distance/(time*time)" so you have to scale the value twice in the end. Or in other words: Input some made up values with real si units, then see if you get some distance only - not a time or velocity


Thanks for the reply.I have tried to change my code around but still cant get it right.I can get the sprite to jump up but not fall back down and Probably dont have the jump part just right,do you have a code snippet you can post or does anyone else have some info.I am new to SFML2 and any help would be appreciated.

11
Graphics / Getting Sprite to Jump
« on: January 15, 2012, 07:28:17 pm »
I am having a problem with getting my sprite to jump. I can move the sprite fine but I cant get it to jump. If I use Sprite.move instead of SetPosition it will move across the screen by a greater distance every time I press space
but not vertical.I know i probably dont have the falling part right either but I need to get jumping right first.

Code: [Select]


float velocity = 0;  
float gravity = 0;
float uprate = 0;
bool jumping = false;
bool falling = false;

if(Event.Key.Code == sf::Keyboard::Right)
Sprite2.Move(sprite2Speed * Window.GetFrameTime() /1000.f, 0.f);
if(Event.Key.Code == sf::Keyboard::Left)
Sprite2.Move(-sprite2Speed * Window.GetFrameTime() / 1000.f, 0.f);
if(Event.Key.Code == sf::Keyboard::Up)
Sprite2.Move(0.f, -sprite2Speed * Window.GetFrameTime() / 1000.f);
if(Event.Key.Code == sf::Keyboard::Down)
Sprite2.Move(0.f, sprite2Speed * Window.GetFrameTime() / 1000.f);
if(Event.Key.Code == sf::Keyboard::Space && jumping == false && falling == false)
{
velocity = 50.0f;
gravity = 0;
jumping = true;
}
if(jumping)
{
velocity = velocity - uprate;
Sprite2.SetPosition(Sprite2.GetPosition().x,Sprite2.GetPosition().y - (velocity));
if(velocity == -50.0f)
{
jumping = false;
falling = true;
gravity = 2;
}
if(falling)
velocity = velocity + gravity;
Sprite2.SetPosition(Sprite2.GetPosition().x,Sprite2.GetPosition().y+(velocity));
if(Sprite2.GetPosition().y + Sprite2.GetSize().y > Ground.GetPosition().y)
{
falling = false;
velocity =0;
gravity = 0;
}
}

12
General / SFML Complete Game Tutorial
« on: January 14, 2012, 04:57:51 pm »
Quote from: "Serapth"
I haven't ported to 2.0 yet but from my glances tonight, it looks like Keyboard has been added as a global static method, instead of accessed via GetInput() in the RenderWindow

So instead of using Game::GetInput() at all, now you do sf::Keyboard::IsKeyPressed().


So, you get rid of GetInput() completely from Game, and in the case of PlayerPaddle code like this:

 if(Game::GetInput().IsKeyPressed(sf::Keyboard::Right))


You do this:

 if(sf::KeyBoard::IsKeyPressed(sf::Keyboard::Right))


Thanks Serapth, I commented out the GetInput() from Game.cpp
and changed PlayerPaddle.cpp to if(sf::KeyBoard::IsKeyPressed(sf::Keyboard::Right)).It compiles fine now but now I have lost my paddle and ball maybe something simple.

13
General / SFML Complete Game Tutorial
« on: January 14, 2012, 01:53:47 am »
This is what I have changed but it still does not work.


Game.cpp
Code: [Select]


const sf::Keyboard& Game::GetInput()
{
return _mainWindow.GetInput();
}


PlayerPaddle.cpp
Code: [Select]

void PlayerPaddle::Update(float elapsedTime)
{
if(Game::GetInput().IsKeyPressed(sf::Keyboard::Right))
{
this->_velocity+= 5.0f;
}
if(Game::GetInput().IsKeyPressed (sf::Keyboard::Down))
{
this->_velocity= 0.0f;
}

if(_velocity > _maxVelocity)
this->_velocity = _maxVelocity;

if(_velocity < -_maxVelocity)
this->_velocity = -_maxVelocity;

sf::Vector2f pos =this->GetPosition();

if(pos.x <= GetSprite().GetSize().x/2
|| pos.x >= (Game::SCREEN_WIDTH - GetSprite().GetSize().x/2))
{
_velocity = -_velocity; // Bounce by current velocith in opposite direction
}

GetSprite().Move(_velocity * elapsedTime, 0);
}


I get a message in Game.cpp that sf::RenderWindow has no member GetInput[/code]

14
General / SFML Complete Game Tutorial
« on: January 14, 2012, 01:42:06 am »
First I want to thank you for doing this tutorial there is no other like that I've been able to find.

I am using SFML 2 and everything has worked up until this point.I am on part 6 and I have had to do some minor changes to the code because you are using 1.6. The part i am stuck on is Game.cpp,you use sf::Input and 2.0 does not use it anymore now it is sf::Keyboard, sf::Mouse and I dont know how to change the code to make this work. If you or anyone else has any suggestions I would appreciate it.Here is the part of Game.cpp I am stuck on.

 
Code: [Select]


const sf::Input& Game::GetInput()
{
return _mainWindow.GetInput();
}


And this is PlayerPaddle.cpp / I know that in 2.0 its Keyboard and KeyPressed instead of key and IsKeyDown

Code: [Select]


void PlayerPaddle::Update(float elapsedTime)
{

if(Game::GetInput().IsKeyDown(sf::Key::Left))
{
this->_velocity-= 5.0f;
}
if(Game::GetInput().IsKeyDown(sf::Key::Right))
{
this->_velocity+= 5.0f;
}

if(Game::GetInput().IsKeyDown(sf::Key::Down))
{
this->_velocity= 0.0f;
}

if(_velocity > _maxVelocity)
this->_velocity = _maxVelocity;

if(_velocity < -_maxVelocity)
this->_velocity = -_maxVelocity;


sf::Vector2f pos = this->GetPosition();

if(pos.x  < GetSprite().GetSize().x/2
|| pos.x > (Game::SCREEN_WIDTH - GetSprite().GetSize().x/2))
{
_velocity = -_velocity; // Bounce by current velocity in opposite direction
}

GetSprite().Move(_velocity * elapsedTime, 0);
}

15
General discussions / sf::input for sfml2
« on: January 12, 2012, 04:39:22 am »
Quote from: "StormWingDelta"
I almost missed that one to do with input key codes because of the switch he made. :oops:  Take a look at the Game.h and Game.cpp files that are in the form of links instead of windows if the code don't work.

Not sure how to explain the input codes though, all I know is they work and what some of them do but that's about it.


Thanks for the reply
Were you using 2.0 or 1.6?

Pages: [1] 2