Jump to content
  • Advertisement
Sign in to follow this  
HexDump

Keep same speed on straight segments and slopes.

This topic is 2522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I'm using unity3d for my upcoming game. I'm trying to create a little platformer using the built-in physics engine ( PhysX) but I'm having problems with the character controller. I know that unity has a built in character controller but it doesn't fit my needs. I want to create simple old-school 2d plat former mechanics.

One of the problem I'm facing right now is to keep the speed on straight segments and slopes constant.

I'm applying a constant force to my rigid body but when it starts going up a slope it decreases its speed and when it is going down a slope it increases its speed relative to the speed it has when on a straight segment.

I have seen a lot of games with slopes and they don't seem to have any problem with it. It seemed a simple thing, but it seems it isn't so obvious.

For my player I'm using a sphere collider to allow it to travel on slopes.

If anyone needs any more information let me know and I would be really pleased to provide it.

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
I found a pretty good solution for projecting motion onto a slope. My problem was in 3d, i wanted the character to cover the same amount of surface distance no matter the slope of the terrain.

For that i took his original horizontal input velocity vector and i shot a ray down from the tip of it into the plane that he was standing on. (not a real raycast, but just the "find t" equation for a ray and a plane.

I then constructed his projected velocity from the ray intersection point back to his origin, and set its length to the magnitude of the original input.

Then, no matter what slope, up down left right, he was running on, he always ran straight and covered the same distances on the surface.

http://en.wikipedia.org/wiki/Line-plane_intersection

The plane is instantaneous derivative of the surface you are standing on, and the ray is in the world -Y direction, starting at the tip of your original velocity, based at your origin.

If you are literally just applying a force, and not considering your current velocity, then you will need to a least consider the acceleration from gravity. If your slop was nearly vertical, you would need to apply a lot of extra force to combat gravity. I would suggest against using forces. My controller takes a "desired translation" and resolves it against collision constraints before applying it to the body.

Share this post


Link to post
Share on other sites

I would suggest against using forces. My controller takes a "desired translation" and resolves it against collision constraints before applying it to the body.


So, you do the collision detection by yourself?.

Thanks for the informative answer.

Share this post


Link to post
Share on other sites
The controller is part of the physics system, and runs after contacts have been found, but before objects move. so it clamps the desired translation based on contacts and then applies the remaining translation when it's finished.

Share this post


Link to post
Share on other sites
Just use the surface normal (which is precomputed, or can be fast bilinearly interpolated, for a triangle mesh) and dot it with your character's "up vector" to find the angle of inclination. Then use that to split your velocity correctly into vertical and horizontal components.
In fact, usually you have velocity in vector form anyway.
You find your new position by simply computing old_pos + t * velocity, where t is determined by speed and time step.

Share this post


Link to post
Share on other sites
Domii, what if you're running along the side of a hill, and your inclination is computed to be 45 degrees? your velocity inclination should be zero.

If you were running straight up or down the hill, your method would work, but that is rarely the case. Dotting the velocity direction against the surface normal may do something. In fact it may turn out to be exactly the same math as what i suggested.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!