SDL platformer, 'moving platform' issue

Started by
6 comments, last by hcastillo 12 years, 10 months ago
Hello again guys!

I'm currently putting together a platformer which I hope to be my first released game. Part of this platformer will include moving platforms which the player can jump onto. When standing on the platform, I want the player to move with the platform. Currently I have devised the following piece of code to handle this.

void Player::correct_pos()
{
int x = find_rect( playerBox );

int dist = playerBox.x - ground[x].x; //distance from player.x to ground.x
playerBox.x = dist + ground[x].x;

}


When a collision is detected this function is run. My platform Rects (named ground) are all members of the 'ground' global array, making building levels an easier task. I search to find which platform is in contact with the playerBox rect. My intention was to find the length between ground.x and playerBox.x, and then add that length to ground.x, meaning were that Rect moving it would update my player too.

The problem is none of the above does anything to my program. When I jump onto the moving platform, the playerBox remains unaffected. At first I thought it was a problem with the function that finds the rect but even when I manually enter a number to replace x, nothing happens. I've tried putting this function in main too, so it updates playerBox.x before it displays it on screen, which again didn't work.

Is there something obvious I'm missing here?

Thanks.
Advertisement
Are you sure the function is actually being called? There may be some issues in your collision detection routines.
I trust exceptions about as far as I can throw them.
The function you posted does nothing. You're calculating the offset to the platform, and then immediately moving the player to that offset, where it already was, meaning that it just stays in the same place.

What you need to do is store the relative position of the player before the platform moves.
I trust exceptions about as far as I can throw them.
I don't quite understand. Within my program, the platform moves before any calculation involving the player is calculated.
Look carefully at the code you posted. Try plugging in dist and see what it simplifies to.
I trust exceptions about as far as I can throw them.
AH, I see it now! And may I say, I feel silly XD Thanks a lot man, I'll have to plan out my intentions again.
Did you fix the problem? I'm also working on a 3D sidescroller game and every time i jumped on a platform it moves because I add the velocity to the player but it's really buggy so I'm trying a better way of doing it, i thought on finding the difference in position for when the platform moves and then adding that to the players position but I'm not sure if that's gonna work.
Yeah man. Like Storyyeller said the code needs to be relative during the calculations, so I made a separate function that takes the value of the platform when the character lands on it. I then use that value in the code below.


void Player::store()
{
if( jump == true )
{
jump = false;
int x = find_rect( playerBox );

store_offset_x = ground[x].x;
}
}

void Player::correct_pos()
{
int x = find_rect( playerBox );

int distX = ground[x].x - store_offset_x;
playerBox.x += distX;

store_offset_x = ground[x].x;

}





When a collision is detected I run store() to store ground.x . Then, I use the code I had in my first post only after I run it I update ground.x again (this prevents the size of distX doubling when the function is called next time).

I hope this helps man.

It was really helpful thanks a lot.

This topic is closed to new replies.

Advertisement