if shape1 move in a really fast speed for some reason (fps drops,etc), it will most probably miss the collisions. since it depends in a tiny 10 pixels collision check system, won't it?
1) This can happen with pretty much any collision method, and that's why people use fixed timesteps. You can search this forum or the internet for loads of info on that.
2) But this collision code is also fairly problematic. Miscellaneous comments to hint toward a better approach:
- First, learn all the sf::Rect methods like contains() and intersects(), and all the arithmetic/comparison operators on sf::Vector2<T>. You can very often avoid comparing/editing individual points/components, which makes your code simpler and more readable.
- The sizes of your shapes are listed in multiple places, which is bad. Your collision logic should be asking the shapes what their sizes are (probably with getSize()).
- You can determine what "side" of shape1 that shape2 is currently on by inspecting the coordinates of their centers. This can be done separately from checking if they actually collide.
- It's generally best to check for collisions before actually moving things. For instance, if the shapes are currently at (0,10) and (20,20), before moving the first shape, you compute that it wants to move to (25,20), then you check if
there would be a collision if it moved there, and if not then you actually move it. If there is a collision then you do things like checking which side its on, what the final position should actually be given the obstacle and so on.
i think that maybe this is really inefficient
Efficiency is not a problem at all. That code should be perfectly performant since only two entities are involved. The problems it does have are of correctness, readability and maintainability.