SFML collision help

Pages: 12
Ok well it sort of works better, it's pretty jittery.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Get Bounds
        if(rectToMove.intersects(redBlockSprite.getGlobalBounds()))
        {
            if(velocity.x < 0 && position.x < redBlockSprite.getPosition().x + redBlockSprite.getGlobalBounds().width)
            {
                cout << "Touched Right side" << endl;
                position.x = redBlockSprite.getPosition().x + characterSprite.getGlobalBounds().width;
            }
            if(velocity.y > 0 && position.y > redBlockSprite.getPosition().y - redBlockSprite.getGlobalBounds().height)
            {
                cout << "Touched Top" << endl;
                position.y = redBlockSprite.getPosition().y - characterSprite.getGlobalBounds().height;
            }

            if(velocity.x > 0 && position.x > redBlockSprite.getPosition().x - redBlockSprite.getGlobalBounds().width)
            {
                cout << "Touched Left Side" << endl;
                position.x = redBlockSprite.getPosition().x - characterSprite.getGlobalBounds().width;
            }
            if(velocity.y < 0 && position.y < redBlockSprite.getPosition().y + redBlockSprite.getGlobalBounds().height)
            {
                cout << "Touched Bottom" << endl;
                position.y = redBlockSprite.getPosition().y + characterSprite.getGlobalBounds().height;
            }
        }


Not sure what else can be done to it really.
If you provide the full source code I can look at what you mean by "jittery."


By the way this may help you understand a rect.
(x, y)-----------------------(x + width, y)
|                                         |
|                                         |
|                                         |
|                                         |
|                                         |
(x, y + height)-----(x + width, y + height)
(taken from http://en.sfml-dev.org/forums/index.php?topic=575.0 ) by Wizzard
Last edited on
ok here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <math.h>
#include <SFML/Graphics.hpp>

using namespace std;

int main()
{
    sf::Vector2f position;
    sf::Vector2f velocity;
    float maxspeed = 4.0f;
    float accel = 4.5f;
    float decel = 0.01f;
    bool vSyncEnabled = true;

    //Setup Window and Framerate
    sf::RenderWindow window(sf::VideoMode(800, 600), "Bounding Box (Collision)");
    window.setFramerateLimit(60);
    window.setVerticalSyncEnabled(vSyncEnabled);

    //Load Texture
    sf::Texture character;
    if(!character.loadFromFile("Resources/Textures/triangle.png"))
    {
        cout << "Error loading resource 'triangle.png'" << endl;
    }
    else
    {
        cout << "triangle.png texture loaded" << endl;
    }

    //Set Sprite for Character Object
    sf::Sprite characterSprite;
    characterSprite.setTexture(character);
    characterSprite.setOrigin(sf::Vector2f(0, 0));
    characterSprite.setPosition(400, 300);

    //Load Red Block Texture
    sf::Texture redBlock;
    if(!redBlock.loadFromFile("Resources/Textures/RedBlock.png"))
    {
        cout << "Error loading 'RedBlock.png" << endl;
    }
    else
    {
        cout << "RedBlock.png texture loaded" << endl;
    }

    //Set Sprite for Red Block Object
    sf::Sprite redBlockSprite;
    redBlockSprite.setTexture(redBlock);
    redBlockSprite.setOrigin(sf::Vector2f(0, 0));
    redBlockSprite.setPosition(200, 200);

    while(window.isOpen())
    {
        window.clear(sf::Color::White);

        window.draw(characterSprite);
        window.draw(redBlockSprite);

        sf::Event event;

        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                }
                break;
                //Dont stretch the window contents when its resized
                case sf::Event::Resized:
                {
                    sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
                    window.setView(sf::View(visibleArea));
                }
                break;
            }
        }

        //WASD Movement
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            velocity.x -= accel;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            velocity.x += accel;
        }
        else
        {
            velocity.x *= decel;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            velocity.y -= accel;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            velocity.y += accel;
        }
        else
        {
            velocity.y *= decel;
        }

        //Make sure the sprite isn't going too fast
        if(velocity.x < -maxspeed) velocity.x = -maxspeed;
        if(velocity.x >  maxspeed) velocity.x =  maxspeed;
        if(velocity.y < -maxspeed) velocity.y = -maxspeed;
        if(velocity.y >  maxspeed) velocity.y =  maxspeed;

        //Update sprite position and move sprite
        position += velocity;

        //Get the window's size
        sf::Vector2u currWSize = window.getSize();

        if(position.x <= 0)
        {
            position.x = 0;
        }
        if(position.y <= 0)
        {
            position.y = 0;
        }
        if(position.x >= currWSize.x - characterSprite.getGlobalBounds().width)
        {
            position.x = currWSize.x - characterSprite.getGlobalBounds().width;
        }
        if(position.y >= currWSize.y - characterSprite.getGlobalBounds().height)
        {
            position.y = currWSize.y - characterSprite.getGlobalBounds().height;
        }

        sf::FloatRect rectToMove(position, {characterSprite.getGlobalBounds().width, characterSprite.getGlobalBounds().width});

        //Get Bounds
        if(rectToMove.intersects(redBlockSprite.getGlobalBounds()))
        {
            if(velocity.x < 0 && position.x < redBlockSprite.getPosition().x + redBlockSprite.getGlobalBounds().width)
            {
                cout << "Touched Right side" << endl;
                position.x = redBlockSprite.getPosition().x + characterSprite.getGlobalBounds().width;
            }
            if(velocity.y > 0 && position.y > redBlockSprite.getPosition().y - redBlockSprite.getGlobalBounds().height)
            {
                cout << "Touched Top" << endl;
                position.y = redBlockSprite.getPosition().y - characterSprite.getGlobalBounds().height;
            }

            if(velocity.x > 0 && position.x > redBlockSprite.getPosition().x - redBlockSprite.getGlobalBounds().width)
            {
                cout << "Touched Left Side" << endl;
                position.x = redBlockSprite.getPosition().x - characterSprite.getGlobalBounds().width;
            }
            if(velocity.y < 0 && position.y < redBlockSprite.getPosition().y + redBlockSprite.getGlobalBounds().height)
            {
                cout << "Touched Bottom" << endl;
                position.y = redBlockSprite.getPosition().y + characterSprite.getGlobalBounds().height;
            }
        }

        characterSprite.setPosition(position);
        velocity *= decel;

        window.display();
    }

    return 0;
}
It might be a problem with the velocity you could try and set it to 0 after a collision. I would also throw in somewhere where if you are colliding on the left you can't move left anymore. I Think that is problem you are talking about. When the x and y velocity are both set. Also you can probably use if/else if since if you hit the left you probably are not going to be able to hit the right.

This video might help too https://www.youtube.com/watch?v=n0U-NBmLj78
Last edited on
It seems to be having a problem when it touches the corners of the square, what can we do for them? also If i go against it and then press another direction it screws up as well.
Last edited on
If i stop it from moving left, how do I make it start moving again?
when it's not touching the wall anymore then you can enable it to move left again. Say you are trying to move right, if you are already hitting a wall on the right then you shouldn't be able to move to the right anymore right? So If you move left, up, or down and are no longer hitting the wall on the right side you can then move right again.

By the way I may need to bow out sorry :( Going to Vegas in a few hours for my brothers wedding and will be there all weekend. If you still need help when I get back I will help then but, until then someone else may be better suited to help.
Last edited on
Alright, I still didnt get it XD but i'm working on other stuff like putting things in separate files, do you think you could help me by telling me what i need to do to make it nicer and correct me?
Topic archived. No new replies allowed.
Pages: 12