• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
mypel16000

Collision in walls - SFML

14 posts in this topic

Hi, I am developing a top-down shooter and I have implemented walls on my map. I want my player to be able to move, but not go through the walls. I tried this method:

Move the player
If there is collision: undo the movement.

Problem is that sometimes my player got stuck, and other times, on touching the wall the player stopped completely (If the player is moving diagonally (up and right for example) and hots a wall on its right, I would like it to still move up like on all other games)

Any ideas on this?

Thank you,

Edit: As my walls are circles, "L" shapes and polygons, I have created a sprite containing the outlines and using pixel perfect collision, but the problem persists. Please, I need a consistent method and some code if possible please. Edited by mypel16000
0

Share this post


Link to post
Share on other sites

Hi, thanks for the info, but I find it is poorly explained. As I have many walls in my room, how would I check for collision on all of them. INCLUDING AN L SHAPE

___

|     |

|     |____

|_______|

????

0

Share this post


Link to post
Share on other sites

As fastcall said - you can decompose an L shape piece into four square pieces.

Alternatively, you can decompose it into two rectangles, like so:

 _
| | __
|_||__|

In general, any polygonal shape consisting of only right angles can be decomposed into a number of rectangles.

With circle, you can also approximate more complex shapes.

 

Hope this clarifies the L shaped collision. If there's any other specific part of my previous post you don't find clear, let me know. :)

2

Share this post


Link to post
Share on other sites

I would suggest moving your player in the x-direction 1st, then checking for collision.  If it collided, move it back to where it was (by just adding the -velocity).  Then move the player in the y-direction and check for collisions.  If it collided, move it back to where it was. This would keep the player always just off the wall.

 

This is a simple method of doing it, but its effective.  it will handle those cases where the player is moving up and right, and he hits a wall to his right.  You will keep moving him right, then placing him back on the x-axis, then move him up, and he won't hit anything bove him, and the y-position will change.

2

Share this post


Link to post
Share on other sites

Though if the L shape is the shape of the inside of the room (and not the wall), you'll want a rectangle for each wall.

 

wallzu.png

0

Share this post


Link to post
Share on other sites

Thank you beernuts! Most helpfull of all.

 

And Servant of the lord, I appreciate your effort, but the room isn't actually L shaped. There is a counter with an L shape inside the room

0

Share this post


Link to post
Share on other sites

I would suggest moving your player in the x-direction 1st, then checking for collision.  If it collided, move it back to where it was (by just adding the -velocity).  Then move the player in the y-direction and check for collisions.  If it collided, move it back to where it was. This would keep the player always just off the wall.

 

This is a simple method of doing it, but its effective.  it will handle those cases where the player is moving up and right, and he hits a wall to his right.  You will keep moving him right, then placing him back on the x-axis, then move him up, and he won't hit anything bove him, and the y-position will change.

interesting, separation of the axis. This would work for rectangle collision, but it won't give accurate results for collision vs a circle. Here's one case, where the y-axis will fail, but the x-axis won't, and you'll end up in the wrong spot (the red arrows are the axis separation, the green is the minimum distance displacement)

 

Worst case I can think of actually ends up where both your x and y axis tests will fail (both will collide with circle) and you'll end up being stuck on the circle.

2

Share this post


Link to post
Share on other sites

This would work for rectangle collision, but it won't give accurate results for collision vs a circle.

 

Oops, he was asking about circles and polygons.  Yes, my solution could cause problems.  I thought it was only rectangles.

 

Be careful using my solution mypel. 

Edited by BeerNutts
1

Share this post


Link to post
Share on other sites


.

Oops, he was asking about circles and polygons.  Yes, my solution could cause problems.  I thought it was only rectangles.

 

Be careful using my solution mypel. 

 

I'm just nitpicking here :) but I also have to point out that with your solution, under certain not so difficult to achieve conditions, the player sprite will 'collide' with the wall before he is physically touching it  - in the case where the player is, say, 0.2 meter from the wall, and your 'move' takes you 0.3 m into the wall, your collision method would stop the player from moving any closer to the wall and he will remain 0.2m from the wall.

 

The faster the movement speeds, as well as the slower the position update, the less accurate the results will be. Of course at 60fps and normal human speeds, this isn't an issue, which is why it's just nitpicking :)

1

Share this post


Link to post
Share on other sites

Collision detetion is fairly easy and straight forward. It's what to do after you've detected a collision thats the hard part.

 

The following link is not meant to solve your issues however it should give you a good deal of insight as to how to go about things. It was written with 3D in mind and quite honestly never quite worked right due to floating point errors (maybe later fixed?) however I think it's still worth reading.

 

As the article is 3D oriented, it can certainly be "dumbed down" for 2D as the principles still apply.

 

Hope this helps.

 

http://www.peroxide.dk/download/tutorials/tut10/pxdtut10.html

0

Share this post


Link to post
Share on other sites

Yes, the article is quite useful! Thumbs up tp you.

@BeerNuts and Milcho: I'll try combining both your methods and instead of moving -velocity.y or -velocity.x, I will calculate the distance to the edge and correct it, while still separating the axis

 

That would be a good idea wouldn't it?

 

If it is, I'll start writing it and then post it here so that everyone can see.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0