0
\$\begingroup\$

I'm trying to make a simple pong clone and want the ball to curve when the racket was moving on hit. To test out curves I removed all the rackets and just have a ball bouncing around in a rectangle, curving in the positive Y direction.

I tried simulating gravity on the Y axis, but the outcome is weird. The bigger the rectangle, the faster the velocity gets over time. In a small rectangle, I lose velocity and the ball slows down.

I do the following: On each tick I check if I should do a curve, if so, then I increase the curve force and add it to the Y velocity (curves only appear on Y)

if (this->DoCurve)
{
    //check if we don't exceed the maximum curve force
    if (this->CurveForce < this->CurveMaxForce)
        this->CurveForce += this->CurveIncrease*delta;

    //add curve force to Y
    this->Velocity += FVector(0.f, this->CurveForce, 0.f) * delta;
}
//move object to new location
SetLocation(this->CurrentLocation + this->Velocity * delta);

When I hit a wall, I want the curve to stop until I hit the next wall.

//if we are already curving, stop it and reset
if (this->DoCurve)
{
    //subtract the force we added to make it move normal again
    this->Velocity -= FVector(0.f, this->CurveForce, 0.f);
    this->CurveForce = 0;
}

this->DoCurve = !this->DoCurve;

//reflect
FVector ReflectedVelocity = (-2 * FVector::DotProduct(this->Velocity, Hit.Normal) * Hit.Normal + this->Velocity);
//ignore Z axis
ReflectedVelocity.Z = 0.f;

//ReflectedVelocity.Normalize(); // this makes the ball stuck in a wall
this->Velocity = ReflectedVelocity;

Basically I have no idea what I'm doing.

What I want: When the ball hits a racket and it is moving I want the ball to move in a curve fast first, then slow down, like the racket gave it a boost.

My approach is very flawed, what would be the correct way?

\$\endgroup\$
1
\$\begingroup\$

I would express the direction of the ball in an angle. The curve would increase that angle each timestep until a wall or a racket is hit.

A quick pseudo-code to give you an idea of what I mean:

Update(float deltatime)
{ 
    direction+=curveangle*deltatime;
    position.x += cos(direction)*velocity*deltatime;
    position.y += sin(direction)*velocity*deltatime;

    if(wall_is_hit)
    {
        curveangle=0;
        direction=new_wallbounce_direction();
    }
    if(racket_is_hit)
    {
        direction=new_direction();
        curveangle=new_curvature();
        velocity=new_velocity();
    }
}

This gives you a basic curve movement. If you want the curve to get slowly to a straight line, reduce the curveangle each step until it becomes 0 or if you want the curve to become more steep increase it to some defined maximum.

\$\endgroup\$
0
\$\begingroup\$

I'm still learning coding soo this won't be a great answer but I hope I can help. If you set the velocity to increase after hitting a racket as a percentage instead of a flat rate and then set a max with a clause to return it to a normal speed after it hit's said max you might find what you're looking for.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.