0

I have a pawn/actor that is a sphere (a snowball). When I roll on snow, it grows (I scale it and add mass).

Problem: I have a weird behavior, my ball rolls but sometimes "jumps" (or bounce up).

The reason seems to be that the ball become bigger and overlap with the ground, causing a big collision.

I tried to change it's position with an offset (Ball->SetAllPhysicsPosition(Ball->RelativeLocation + FVector(0, 0, newRadius-pastRadius);, but I still have the same problem. (It "works" with a big hardcoded offset, but not with any size).

Would you have an idea on how to fix this behavior?

This is the code I use to make the ball gets bigger. I add torque to move it around.

void ASnowballBall::NotifyHit(...)
{
    float resizeValue = 0.0f;

    //If the snowball collides with snow, it grows
    if (Other->GetName().StartsWith("Snow", ESearchCase::IgnoreCase) && Ball->BodyInstance.GetUnrealWorldVelocity().Size() > 50.0f)
    {
        // Max size based on mass
        if (Ball->GetMass() < 10000.0f){
            resizeValue = (20.0f / Ball->GetMass()));
            resizeValue = FMath::Clamp(resizeValue, 0.0f, 0.003f);

            Ball->SetRelativeScale3D(Ball->RelativeScale3D + resizeValue);
            Ball->SetMassScale("None", Ball->BodyInstance.MassScale);
        }

    }
David Gourde
  • 3,709
  • 2
  • 31
  • 65

1 Answers1

1

Don't change the scale, change the radius. Then, because you know the new radius, you can do a line trace straight down from the ball center to the ground, and move the ball up (or down) by the difference between the line distance and the new radius.

Also if physics is important for the ball in your game, tick CCD in the actor properties to enable better collision detection per frame. This can get expensive, however, so make sure you profile your game many times.

JonS
  • 401
  • 2
  • 5