본문 바로가기
Golf/Physics

Simulation - golf2

by wenect 2023. 6. 30.

고려되지 않은 요소를 추가하려면 복잡한 물리 모델을 구현해야합니다. 여기서는 골프공의 회전과 바람의 효과를 추가해보겠습니다. 이들 요소를 추가하기 위해선, 각각에 대해 어떻게 계산할지를 결정해야합니다. 공의 회전은 매그누스 효과를 통해 리프트를 생성합니다. 바람은 공의 경로에 변화를 주는 추가적인 힘을 생성합니다.

다음은 골프공의 움직임을 시뮬레이션하는 코드에 이 요소들을 추가한 버전입니다.

using System;

class GolfBall
{
    // Constants
    private const double Gravity = 9.81;
    private const double Mass = 0.045;  // approx mass of a golf ball
    private const double DragCoefficient = 0.47;  // drag coefficient for a sphere
    private const double LiftCoefficient = 0.5;  // lift coefficient for a spinning sphere
    private const double AirDensity = 1.225;  // at sea level and at 15 °C
    private const double BallRadius = 0.02135;  // standard radius of a golf ball
    private const double TimeStep = 0.01;  // time step for the simulation
    private const double SpinRate = 3000.0;  // spin rate in RPM
    private const double WindSpeed = 5.0;  // wind speed in m/s

    // Variables
    private double posX, posY;  // position of the golf ball
    private double velX, velY;  // velocity of the golf ball

    // Constructor
    public GolfBall(double initialSpeed, double launchAngle)
    {
        // Initial conditions
        posX = 0;
        posY = 0;
        velX = initialSpeed * Math.Cos(launchAngle);
        velY = initialSpeed * Math.Sin(launchAngle);
    }

    // Update the position and velocity of the golf ball
    public void Update()
    {
        // Calculate the drag force
        double speed = Math.Sqrt(velX * velX + velY * velY);
        double dragForce = 0.5 * DragCoefficient * AirDensity * speed * speed * Math.PI * BallRadius * BallRadius;
        double dragForceX = -dragForce * (velX - WindSpeed) / speed;
        double dragForceY = -dragForce * velY / speed;

        // Calculate the lift force due to spin (Magnus effect)
        double liftForce = 0.5 * LiftCoefficient * AirDensity * SpinRate * BallRadius * speed;
        double liftForceX = liftForce * velY / speed;
        double liftForceY = -liftForce * (velX - WindSpeed) / speed;

        // Calculate the force of gravity
        double gravityForceY = -Mass * Gravity;

        // Update velocity
        velX += ((dragForceX + liftForceX) / Mass) * TimeStep;
        velY += ((dragForceY + liftForceY + gravityForceY) / Mass) * TimeStep;

        // Update position
        posX += velX * TimeStep;
        posY += velY * TimeStep;
    }

    // Get the position of the golf ball
    public (double, double) GetPosition()
    {
        return (posX, posY);
    }
}

class Program
{
    static void Main()
    {
        // Create a golf ball with an initial speed of 70 m/s and a launch angle of 45 degrees
        GolfBall golfBall = new GolfBall(70, Math.PI / 4);

        // Continue the simulation until the golf ball hits the ground
        while (true)
        {
            // Update the golf ball
            golfBall.Update();

            // Get the position of the golf ball
            (double posX, double posY) = golfBall.GetPosition();

            // Print the position of the golf ball
            Console.WriteLine($"Position: ({posX}, {posY})");

            // Stop the simulation when the golf ball hits the ground
            if (posY < 0)
            {
                break;
            }
        }
    }
}

이 코드에서 공의 회전은 공에 리프트 힘을 생성하며, 이는 공이 조금 더 높게 상승하게 합니다. 이 효과는 매그누스 효과라고 알려져 있습니다. 또한 바람은 공의 궤적에 변화를 줍니다. 바람이 공을 따라 불면 공의 속도가 증가하고, 반대 방향으로 불면 공의 속도가 감소합니다.

다시 말하지만, 이 코드는 여전히 단순화된 모델을 사용하고 있습니다. 복잡한 공기 저항 계수, 공의 디자인, 공의 각도 변화 등은 고려되지 않았습니다. 더 정확한 시뮬레이션을 위해선 이런 요소들을 모두 고려한 상세한 물리 모델이 필요합니다.

'Golf > Physics' 카테고리의 다른 글

수학-알파지오메트리  (0) 2024.01.21
Simulation - golf3  (0) 2023.06.30
Simulation - golf1  (0) 2023.06.30

댓글