• 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

How do I implement gravity after walking off a tile?

8 posts in this topic

I was finally able to make collision detection in my 2D platformer written in C with SDL 2.0.

 

I then tried to make jumping work, though it's still a bit unrealistic, though not that 2D is ever going to look realistic smile.png

 

7uuhezys.jpg

 

As you can see, my red square which represents the player for development purposes is sitting on top of a soil tile and colliding smile.png. If I jump the player goes up and then back down at a constant speed(for now)

 

This is my jumping code

	if(player.key_press[SDLK_SPACE])
	{
		player.is_jumping = true;
		player.pos.y -= 1000 * prev_frame_tick;
	}
	
	if(player.is_jumping)
	{
		player.pos.y += 200 * prev_frame_tick;
		for(int i = 0; i < Map->tile_num; i++)
		{
			if(Map->tile[i]->type == 2) // Type 2 is a solid tile. Needs renaming.
			{
				if(check_collision_rect(player.pos, *(Map->tile[i])))
				{
					player.pos.y -= 200 * prev_frame_tick;
					player.is_jumping = false;
					break; // if it's colliding with one tile, then that is enough.
				}
			}
		}
	} 

But after jumping and colliding, if I move and there is no solid tile, I just float there

 

smKq3dxs.jpg

 

But I am unsure how to implement gravity to work there, so that I fall on the ground.

 

Also, if it is allowed, I would also like to ask how to fix my collision detection so it does not loop through all the tiles to find the one the player is on or hitting.

The way I currently store my map is using this design

struct Tile {
	int type;
	int y, x;
	int h, w;
	
	int sY, sX; // location on sprite sheet
};

typedef struct _Map {
	struct Tile **tile;
	
	int tile_num;
} _Map;

I store a single tile in it's own structure, allowing me flexibility in modifying the attributes of each tile. But I was told by a person that if I want to be able to use a more CPU-friendly collision detection algorithm I'd need to use a 2D array, but I will then miss out on everything plus how would it work with a 2D int array exactly?

0

Share this post


Link to post
Share on other sites
To give a brief on what others are saying: essentially there's no "pretty" way to handle collision in any game with constant contact terrain. You basically have to assume all the worst case scenarios in terms of collision. You could be standing still and have a bunch of balls come down and hit you, obviously you'd want to detect those. You might want the ground to fall away even when you're standing still.

Using that thinking you can determine that you can't really "assume" that you're on the ground, probably the best way to handle it is to always be checking if the ground under you has stopped being solid. Using that thinking you can have a "jumping" state and a "falling" state if you'd like, in order to effect gameplay like when you can jump or to change movement while in air. Gravity should basically be applied unless you find yourself not in a falling state, and would stay in that state until the ground underneath disappeared or you jump.

To give a little example: say you walk off a cliff after being on flat ground. At that point you could detect the ground is no longer under your character and toggle "falling" at that point you could make gravity affect the player. You could technically leave gravity on but this is kind of a collision resolution waste because it will be trying to pull you back up out of the ground every update loop, it also may cause "jittering" visual glitches if you use something like interpolation.

There's a few articles under the game programming section here that cover things like platformers and how older 2d platformers handled, or how the author assumed they handled collision. You might pick up some ideas by looking!

Oh, but keep in mind in terms of gravity and collision that you'll be wanting to check for collision in other directions as well, i.e. falling into a pit and hitting the side, or something bonking you on the head, as I hinted at before. Edited by Satharis
0

Share this post


Link to post
Share on other sites
I tried to implement this many times and it ends up not working correctly. I figured I only need to check the bottom of my player's rect and the top of the tile below me for collision so I did this
 
int check_collision_below(Rect A, struct Tile B)
{
    int topB;
    int bottomA;
 
    topB = (int)B.y;
    bottomA = (int)A.y + A.h;
 
    if(bottomA <= topB) 
        return 0;
 
    return 1;
}
 
I used this code
 
Rect collision_underneath;
collision_underneath = player.pos; // assign player position.
collision_underneath.y += 1.00; // check one pixel below us.
for(int j = 0; j < Map->tile_num; j++)
{
    if(Map->tile[j]->type == ENUM_TILE_AIR) // if tile is non-solid i.e air for instance
    {
        if(!check_collision_below(collision_underneath, *(Map->tile[j]))) // if there is NO collision, it means we should fall.
        { 
            puts("Nothing there, should fall");
            break;
        }
     }
}
 
but it doesn't work properly and fails to detect if there is ground or not... Actually, it does. But what happens is that when I walk off a tile it doesn't detect it, but when I jump it starts detecting that there is nothing below me till I hit the ground(and depending on my logic it fails detect any solid tiles above the ground when I stand on them), which is weird because the code from jumping and detecting walking off a tile are separate.
 
And if you are wondering about the types, here they are.
 
struct Tile {
	int type;
	int y, x;
	int h, w;
	
	int sY, sX; // location on sprite sheet
	
	int row, col;
	bool solid;
};

typedef struct Rect {
	int h, w;
	float x, y;
	float prev_x, prev_y; // Previous positions
	float velX, velY; // Velocity for X and Y coordinate. 
} Rect;
Edited by farmdve
0

Share this post


Link to post
Share on other sites
The way gravity normally works in game (and incidentally, real life), is that it is *always* pulling objects down. If they happen to be on a solid platform, the platform is continuously pushing the object back up, so it can't fall. Assuming you use Euler Integration (if you don't know, you probably are.), you're storing a velocity vector of the object, then if you get a collision with a solid thing like the ground, you'll want to modify the velocity to remove the component which is going into the ground (in practice, this is something like: if (velocity.y < 0) velocity.y = 0; ) Mark
1

Share this post


Link to post
Share on other sites

For collision, you should be checking whether or not the player's bounding box overlaps with the bounding box of a tile.

 

Use something like this:

bool Collision_check(Rect A, struct Tile B)

{

if (A.x + A.w < B.x) return false; //collision is impossible since the right edge of the player is left of the leftmost edge of the tile
if (A.x > B.x + B.w) return false;
if (A.y + A.h < B.y) return false;
if (A.y > B.y + B.h) return false;

return true; //since none of those conditions were met, collision must of happened, so return true

}
Edited by Ludus
0

Share this post


Link to post
Share on other sites

 

For collision, you should be checking whether or not the player's bounding box overlaps with the bounding box of a tile.

 

Use something like this:

bool Collision_check(Rect A, struct Tile B)

{

if (A.x + A.w < B.x) return false; //collision is impossible since the right edge of the player is left of the leftmost edge of the tile
if (A.x > B.x + B.w) return false;
if (A.y + A.h < B.y) return false;
if (A.y > B.y + B.h) return false;

return true; //since none of those conditions were met, collision must of happened, so return true

 

Thank you, but I have no problem with collision detection except for falling off, I just can't detect that there is no solid tile below me properly.

Edited by farmdve
0

Share this post


Link to post
Share on other sites


Thank you, but I have no problem with collision detection except for falling off, I just can't detect that there is no solid tile below me properly.

 

If acceleration due to gravity is always being applied in each loop, then the player should be constantly checking collision below himself. By default, he will fall due to the constant gravity if nothing is below him.

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