Free movement and collision

Started by
1 comment, last by munchor 12 years, 4 months ago
First of all, this is partially a Maths/Physics question, and partially an SDL question, hence I'm not sure on which forum to post this. If it's wrong, feel free to move it.

I was developing a game based on a grid system, and the player could only move 25 pixels per click, and when the player held the key for too long, it would simply move one tile, and then stay in the same place, instead of moving more tiles. It's a maze game, so I'm not sure of how to proceed.

I would like the player to be able to move freely, since it always looks better and enhances gameplay, but I'm not sure of how to combine a grid map (with things on the floor, doors, etc.) and a free movement. In fact, I'm not even sure if it's possible.

Here's some of my code (possibly impossible to run on its own)


int main(int argc, char *argv[])
{
/* Initialize SDL */
SDL_Surface *screen;
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
SDL_WM_SetCaption("test", "test");

int map[MAP_HEIGHT][MAP_WIDTH];
SDL_Event event;

load_map();

while (true)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
SDL_Quit();
return 0;
}

if (event.type == SDL_KEYDOWN)
{
/* This is where I get key input, no acceleration, just
"player_y = player_y - TILE_SIZE"
*/
}
(...)



On the one hand, I would like a way of moving freely like in a first person shooter game, or Animal Crossing (most commercial games with a moving player actually), but on the other hand, can I still have a map grid? I'm not sure and I need help both with deciding what to use in a maze game, and with the code (continuous key pressing).

Actually and ideally, how to move one tile at a time, but when he keeps pressing the key for longer, the player will keep moving more tiles? Thanks.
Advertisement
Do you want to remove the grid like movement mechanics altogether, or would you be happy to merely animate the player moving from tile to tile, such that motion appears smooth but when the player stops moving they finish exactly on a tile?


In fact, I'm not even sure if it's possible.
[/quote]
It is certainly possible. It is more complex though.


Actually and ideally, how to move one tile at a time, but when he keeps pressing the key for longer, the player will keep moving more tiles? Thanks.
[/quote]
Look into SDL_EnableKeyRepeat().
EnableKeyRepeat made it much better, if I don't succeed with a better way, I'll just use this, amazing!

Do you want to remove the grid like movement mechanics altogether, or would you be happy to merely animate the player moving from tile to tile, such that motion appears smooth but when the player stops moving they finish exactly on a tile? I would like to keep the grid, on the one hand, because it makes collision so much simpler, and level design too (using a text editor if text files (like right now), or an image editor).

So I would like the second option (animating the player), but if he keeps pressing the button, would it be possible to make him land on another tile? And how would I go about doing collision this way?

This is how I'm going about right now:


/* When the user presses the up key */
int wanted_position = map[player_y / TILE_SIZE - 1][player_x / TILE_SIZE];

if (not_block (wanted_position)
player_y = player_y - TILE_SIZE;

This topic is closed to new replies.

Advertisement