# How to have my sphere go downhill/uphill smoothly?

Discussion in 'Physics' started by xred13, Nov 20, 2018.

1. ### xred13

Joined:
Jul 14, 2018
Posts:
66
Hi, so i am making this game, where i've got this ondular generated terrain and a sphere to which i add forward force if i click and it is grounded or downwards force if i click and it is on the air... here's a video:

i've added a physics mat to the terrain and the player sphere with no bouncing and no friction

Problem: sometimes the sphere kind of bounces, ex: time 0:21 or 0:50
how do i have it smoothly stick to the ground? I am using addforce with ForceMode.acceleration... is this the wrong way for what i want?

EDIT:

Here's a link to the project: https://files.fm/u/nqyd5xp4
this is my first project, so i'd appreciate any input!

Scene is SampleV2 and all the stuff is under the folder SampleV2
If you click on a plane on the editor it'll check on the x axis of the clicked plane for diferences in the height of the vertices and it will print out the height (y value) of the vertices on the diferent z values

if anyone would like to help out, it'd be great. i commented the code and i think it is clean to an extent. a plane/tile/however i call it is a 3d plane, each wave uses a set number of planes. you can change the terrain to a low poly version or high poly by:
- default right now is 5 dedicated tiles in TerrainGenerator and plane Z scale == 1
- low poly for example, you could put 1 in dedicated tiles (1 tile/plane for each wave) and change the plane Z scale value to 5, high poly would be for example 10 dedicated tiles and a z scale on the plane of 0.5

There are some parameters for ex: timeForceClickEnableTime and minimumSecondsOnAir that were added to try and fix a bit the scoring because of all the jumps of the ball, dont pay attention to those

i use a normal distribution function to get the waves , and i get random heights / widths (the width is more like how fast/slow we get more/less height, not the actual size of the waves)... i'd appreciate other suggestions to doing a terrain like this

Last edited: Nov 29, 2018

Joined:
Jul 14, 2018
Posts:
66
bump

3. ### xred13

Joined:
Jul 14, 2018
Posts:
66
I have checked the terrain height of each vertice line and there are no bumps whatsoever... so I am really confused as to why the sphere jumps.. some input / ideas are appreciated! All the forces that act on the sphere are in the forward and down direction and they are Forcemode.acceleration.

4. ### N3V-Developer

Joined:
Jun 7, 2014
Posts:
19
• You should try lower speed. Check from what speed it starts to bounce.
• Look at the Settings/Physics and try some changes. Broadphase Type etc..
Think about Phys can not handle endless high speed correctly. This cause miscalculation. Could be possible that the ball.y axis curve points is below the terrain at any situation. Then Phys try to compensate by placing the ball over the terrain again.

5. ### tjmaul

Joined:
Aug 29, 2018
Posts:
10
Hey there,

I guess since you created the surface procedurally, you probably also know its slope. Try projecting the velocity of the rigidbody onto the slope and set it to that speed. For example if you have the normal for a given point where the ball touches the plane, do:

``rb.velocity = Vector3.ProjectOnPlane(rb.velocity, normal)``

6. ### xred13

Joined:
Jul 14, 2018
Posts:
66
Thanks for the input... but the player has to lift off and get back to the ground and i feel like that isn't the way to go about this.. i just can't see why the sphere bounces... i have added a link to the project zipped if you'd like to take a look, its commented and i think its clean. it is my first project and i'd appreciate any tips

7. ### tjmaul

Joined:
Aug 29, 2018
Posts:
10
Ok I see now what you mean. The problem is not the bounciness of the physics material, but the actual shape of the mesh collider. You don't really have a nice and smooth surface (like its shading suggests), but a piecewise linear object. If the ball is fast enough, the angle between two adjacent linear pieces is big enough to make the ball float for a bit and then fall again.

I played a bit with your implementation and made a feedback controller, that keeps the ball floating in mid-air. Using the normal of a SphereCast, you can calculate the slope at the point where the floating ball now "touches" (it doesn't, it floats in mid air, invisible to the player) the terrain to simulate the reaction forces so that it rolls down hills.

The controller is made to smooth out the piecewise linear chunks so they're unnoticeable. You might get better results if you increase the amount of faces. Be aware that you're working with non-convex colliders, which might be tricky or to avoid altogether.

Furthermore I had to lower the acceleration numbers on your player controller. It doesn't make sense to multiply the acceleration with deltaTime, so I removed that.

You might also get better results if you use a wheel collider,.. which is kinda doing the same thing as my code anyways, but WheelCollider is way more advanced.

One more word:
Sorry, I kinda broke your gameplay though, .. you have to implement the forward force at the proper place, but the code might be good to start from. To actually get it running, you have to move the start position of the ball between the first two hills

8. ### tjmaul

Joined:
Aug 29, 2018
Posts:
10
sorry, i removed the attached files, I uploaded the wrong version and now I'm getting an error posting them again because of "spam"

File size:
5.8 KB
Views:
13
File size:
21.5 KB
Views:
14
9. ### xred13

Joined:
Jul 14, 2018
Posts:
66
Thanks for the input! That might be a nice fix!
However i just made a scene with multiple planes in a row, changing their z position incrementing it by 10 each time and had a sphere roll on them. when i got was a weird similar jumping effect. however when i roll it on a terrain, it moves normally.

Might it be because of the horizontal line of vertices that the planes share? Still, i used combine mesh to combine the meshes of all planes and then destroyed and added a mesh collider and it kept the bouncing behaviour.

Joined:
Jul 14, 2018
Posts:
66