🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How do improvise my code for the player's movement and jumping?

Started by
4 comments, last by Thaumaturge 3 years, 7 months ago

Hi guys, I need help with my character's movement. As of now, the character is able to move and jump however it can't jump over a platform. I'm not sure if that bug comes from the movement, jumping, or if I should add another method to implement it.

static void movement(void){
	position_goal = CP_Vector_Set(0.0f, 0.0f);
	float dt = CP_System_GetDt();
	player_speed = speed_modifier * dt * 30;

	if (CP_Input_KeyDown(KEY_LEFT)) {
 		position_goal = CP_Vector_Add(position_goal, CP_Vector_Set(-player_speed, .0f));
	}

	if (CP_Input_KeyDown(KEY_RIGHT)) {
 		position_goal = CP_Vector_Add(position_goal, CP_Vector_Set(player_speed, .0f));
	}

	position = CP_Vector_Add(position, position_goal);
}

static void jump(void){
	CP_BOOL on_ground = position.y >= ground;
	if (CP_Input_KeyTriggered(KEY_UP) && on_ground) {
 		velocity_goal.y = -jump_power;
	}

	if (position.y + jump_limit < ground) {
 		if(velocity_goal.y <= 0) 
  			velocity_goal.y++;
 		if (velocity_goal.y > 0)
  			velocity_goal.y = 0;
	}

	jump_velocity.y = velocity_goal.y * CP_System_GetDt() * 250;

	if (!on_ground){
 		jump_velocity.y += gravity.y * CP_System_GetDt() * 200;
	}
	position = CP_Vector_Add(position, jump_velocity);
}
Advertisement

When you say “it can't jump over a platform” what do you mean? It doesn't jump at all? It gets stuck on the platform? It doesn't jump far enough?

Additionally, some comments in your code might help us understand what each part is intended to do.

This video tutorial is very good: Создание игр на C++: Марио (платформер) It doesn't matter if you don't understand Russian. Just read code. Author uses SFML but your can apply the ideas in your project. For example, I translated his code to OpenGL and WebGL.

@Kylotan So currently, my sprite is able to jump over the platform. However, it can't move and got stuck when there is a collision with the bottom platform. My thought is that the problem lies on the ground as, the ground i set is a set value (which shouldn't be) so it will always get pulled by gravity. since it gets pulled by gravity, the character's next position will always get collided with the block below, preventing the character from moving. or are there any other possible situation to it?

Perhaps it might help to set the value of “on_ground” to be “true” when the character impacts a platform, and to set it to “false” when the character falls or jumps. (As well as making it a non-local variable in order that the vaue be preserved between method-calls.) That might allow you system to respond regardless of the height of the current platform.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

This topic is closed to new replies.

Advertisement