Jump to content

Character Controller Ramp Bounce


gamecreator
 Share

Recommended Posts

Hello. How do you avoid the bounce of a character controller on a ramp? The problem is twofold:

 

1. It looks bad to have a character bounce down a ramp when they should be running

2. ControllerAirborne is usually 1 which means that if you use that to detect if you can jump, you're out of luck. Most of the time the character can't.

 

Below is a quick video and the code:

 

 

#include "engine.h"

#pragma warning(disable:58) 

int WINAPI WinMain( HINSTANCE hInstance,
				HINSTANCE hPrevInstance,
				LPSTR lpCmdLine,
				int nShowCmd ) 
{
if(!Initialize()) return 1;

RegisterAbstractPath("D:/Programming/Leadwerks Engine SDK");

// Set graphics mode
if(!Graphics(640,480))
{
	MessageBoxA( 0,"Failed to set graphics mode.","Error",0);
	return 1;
}

// Create framework object and set it to a global object so other scripts can access it
TFramework fw = CreateFramework();
if(fw==NULL)
{
	MessageBoxA(0,"Failed to initialize engine.","Error",0);
	return 1;
}

// Set Lua framework object
SetGlobalObject("fw",fw);

// Set Lua framework variable
BP lua=GetLuaState();
lua_pushobject(lua,fw);
lua_setglobal(lua,"fw");
lua_pop(lua,1);

// Get framework main camera
TCamera camera = GetLayerCamera(GetFrameworkLayer(0));
PositionEntity(camera,Vec3(13,5,-12));
RotateEntity(camera,Vec3(15,0,0));

TLight light=CreateDirectionalLight();
RotateEntity(light,Vec3(45,45,45));

SetWorldGravity(Vec3(0,-80,0));

TModel level=LoadModel("abstract::level.gmf");
if (!level)
{
	MessageBoxA(0,"Error","Failed to load mesh.",0);
	goto exitapp;	
}
EntityType(level,2);

TController player=CreateController(1.8,0.4,0.5,45.01);
EntityType(player,1);
SetBodyMass(player,10);
PositionEntity(player,Vec3(0,1,0));

float move=0.0;
float strafe=0.0;

TEntity         backwall = CreateBodyBox(50,50,1,0);
MoveEntity     (backwall, Vec3(0,0,-1));
SetBodyMass    (backwall, 0);
EntityType     (backwall, 2);

TEntity         frontwall = CreateBodyBox(50,50,1,0);
MoveEntity     (frontwall, Vec3(0,0,1));
SetBodyMass    (frontwall, 0);
EntityType     (frontwall, 2);

Collisions(1,2,true);
Collisions(1,2,true);

DebugPhysics(1);

SetAntialias(1);

while(!KeyHit(KEY_ESCAPE))
{
	TVec3 pos;
	TVec3 vel;

	move=KeyDown(KEY_RIGHT)-KeyDown(KEY_LEFT);
	pos=EntityPosition(player,1);
	PositionEntity(camera,Vec3(pos.X,pos.Y+4,-15));
	PositionEntity(backwall, Vec3(pos.X,pos.Y,-1));
	PositionEntity(frontwall, Vec3(pos.X,pos.Y,1));

	move*=10;

	float jump=0.0;
	if (KeyHit(KEY_S)) {
		if (!ControllerAirborne(player)) {
			jump=30.0;
		}
	}

	UpdateController(player,270.0,move,0.0,jump,50);

	UpdateFramework();
	RenderFramework();

	DrawText(0,20,"move: %f",move);
	DrawText(0,40,"airborne: %d",ControllerAirborne(player));


	Flip(0);
}

return Terminate();
}

 

I really need to guarantee that the controller stays flat on the ground whenever it's moving downhill. Thanks for any help.

Link to comment
Share on other sites

The bouncing comes from the fact, that the player is moving forward, and not downward. You could add a downforce to the player to make it move also downward.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

True. Gravity doesn't pull it down fast enough. Now, since forces and positioning don't work on character controllers, what are my options? Some sort of hack with a negative jump?

 

Imagine if the character was going even faster (something like Sonic the Hedgehog) but it still needed to stay on the ground.

 

I'd love to get this to work as I really like that the sliding joints work with character controllers (for moving platforms and such) but I may have to code the physics and collision myself.

Link to comment
Share on other sites

I tried the FPS demo which I found somewhere, but it was based on this tutorial:

http://www.leadwerks.com/werkspace/page/Documentation/LE2/tutorials/_/programming/cpp/character-controllers-r15

 

There you can walk down the ramps and it doesn't bounce at all, so you should check your parameters that you have similar values.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Their move value is 1 while walking, 2 while running. Mine is 10. I guess one solution could be to scale down the world and player to a 10th of their size and then use smaller values. But I suspect this issue will still come up with steeper ramps.

 

I wish Leadwerks handled this. I appreciate the help Metatron.

Link to comment
Share on other sites

I will work with steeper ramps too, because then the player slides automatically down, and you don't need to move forward at all.

See the example I posted, there you have 3 different steep ramps to test all situations.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

You can't really use that big size, because it will mess up all physics behaviour too, and you can see directional light shadows only like up to 50 meter far away (500 meter with the default scale).

The get the best functionality of the engine, you should use the default scale. Smaller works too, but not much smaller.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

You should just use the parameters like in the tutorial and everything will work fine. In your example you have set the gravity 40 times bigger, the mass of the player 10 times bigger, and other things, so it can never work normal that way.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

As expected, setting everything to the tutorial values made the controller move incredibly slow. I had the move speed at 2 (the run speed in the tutorial). Setting it to 3 already made the controller bump off the ramp. (I even got rid of the side backwall and frontwall entities just in case there was friction there.)

Link to comment
Share on other sites

  • 2 weeks later...

It's behaving correctly.

 

-You are applying horizontal force to the player, even when they are airborne. You should greatly reduce their movement when airborne.

 

-Your movement is quite strong compared to gravity. Turn the movement down or increase the gravity, or both.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

It sounds like you want it travelling at the same velocity, perpindicular to the normal of the ground. Take the vertical velocity and use that to create a vector with the horizontal movement to calculate how much the horizontal movement should be reduced by,

 

You should definitely multiply the movement by something like 0.1 when the controller is airborne.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Hello GameCreator, try this in your movement code loop:

 

SetBodyVelocity(yourcontroller, Vec3( GetBodyVelocity(yourcontroller).X,- 10.0f, GetBodyVelocity(yourcontroller).Z) );

 

use it only if the controller is not airborne and it should fix your issue ;)

You guys are going to be the death of me. Josh
Link to comment
Share on other sites

I plan on writing my own collision detection so there's no need to solve these problems on my account but

 

1. When the character goes down the ramp, it is often still considered airborne so you can't jump (even if it looks like he's on the ground). A lower velocity might solve this.

2. When jumping on the ramp, sometimes the character falls straight through it now.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...