• 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
farmdve

2D collision resolution code not behaving properly.

4 posts in this topic

Long story short, I followed the SDL tutorial on LazyFoo here [url="http://lazyfoo.net/SDL_tutorials/lesson29/index.php"]http://lazyfoo.net/S...son29/index.php[/url] and wrote the game from scratch in C(since the examples were C++), but borrowed some of the functions.

What is happening is that the collision code is working, but not entirely. If I were to hit a wall, and continue to walk towards it(and being unable to pass through it obviously) and at the same time try to move in a different direction(but not the opposite to the wall) I can't move.

This is my if/else statement

[CODE]
if(touches_wall(player.box))
{
if(player.box.x < player.prev_x)
{
player.box.x += GetVelocity(player, 0);
}
else if(player.box.x > player.prev_x)
{
player.box.x -= GetVelocity(player, 0);
}

if(player.box.y > player.prev_y)
{
player.box.y -= GetVelocity(player, 1);
}
else if(player.box.y < player.prev_y)
{
player.box.y += GetVelocity(player, 1);
}
}
[/CODE]

player.box.x and player.box.y are the current positions of the player on the X and Y axis respectively. player.prev_x and player.prev_y are the previous positions.
GetVelocity pretty much returns some static hardcoded values of the velocity for each axis(depending on the second argument being 0 or 1).

My previous collision handling code was this huge one [url="http://pastebin.com/mrFkLTLH"]http://pastebin.com/mrFkLTLH[/url] but while it worked the way I wanted it allowed under some conditions the object to pass through the wall near the corners.

Here's a picture of a more detailed explanation; [url="http://i.imgur.com/Rn7MY.png"]http://i.imgur.com/Rn7MY.png[/url]
Where the arrows are, I basically make the object go left, let it collide and then either go up or down, while still going left, but it will stay still and won't move either way.
With my previous large code that I just pasted on pastebin, where the small blue lines are on the screenshot, that's where the object passed through the wall on some conditions.

So I basically need a solution on how to fix this. I've been working on that since last night, to the point where I wrote the same code over and over again, expecting a different result.

EDIT:It is fairly obvious to me, that since my collision code is inside a single if-statement, and I am indeed colliding with a wall, while trying to move up or down, either of the sub-statements always execute thus preventing me to move. No idea how to fix it though, Edited by farmdve
1

Share this post


Link to post
Share on other sites
If you collide with a wall then rather than undoing that frame's motion you should probably place yourself at a position 'against' the wall.

Rather than stepping back by your velocity, detect the wall edge that you're colliding with and set yourself at the most extreme valid position:

[source lang="ruby"]#ruby flavored pseudocode
def playerMovingLeft(player, amount)
player.box.x -= amount
collidedObject = stage.testCollide(player.box)
if collidedObject != nil #hit the left wall (since we're moving left)
player.box.x = collidedObject.rightEdge #so set position to a valid one
end
end
[/source]

As for your code, another way of writing exactly what you have there would be:

[source lang="cpp"]if(touches_wall(player.box)) { //don't know which direction(s) caused collision.
player.box.x = player.prev_x;
player.box.y = player.prev_y;
}[/source]

Which makes it easier to see the problem. You say in your post that you already know the problem though. I don't understand what you're confused about then. If a single 'if' won't work then don't use a single 'if'.

Get some rest. I think it will come to you when you're freshened up a bit. If you code yourself into a corner taking a break can often simplify difficult problems. Edited by Khatharr
0

Share this post


Link to post
Share on other sites
You could possibly fix this by moving the player in the x direction 1st, and checking for collisions, then move him in the y direction and check collisions then. The issue is you are doing 2 collision checks per update, but, depending on the game, this probably won't be an problem.
0

Share this post


Link to post
Share on other sites
It will end up being either a loop or recursive calls, since you'll have to do the same steps until all your movement is "used up".

-check for _first_ collision (your path might have you collide with more than one object)
-move up to point of collision (or slightly less, because floats can be a pain)
-resolve collision (determine new direction and remaining movement)
-back to step one (unless your collision response is to stop dead, in which case the logic still applies with 0 as remaining movement)
0

Share this post


Link to post
Share on other sites
Thank you guys, after moving the code of each axis after the code where I increase each axis upon movement, I was able to overcome ALL the limitations I had previously. I could not have done it without you guys. Thank you all. Edited by farmdve
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