Programming a smooth change of thrust from current velocity vector to a target vector

后端 未结 7 1684
北海茫月
北海茫月 2021-02-04 16:47

TL;dr: \"I am not sure how to calculate a smooth transition of thrust between one vector and another.\"

I am programming a simple game where an enemy chases after the pl

相关标签:
7条回答
  • 2021-02-04 17:49

    There are just a few pointers to get this right and easy. 1) it's easiest and most general to work with vectors rather that writing everything two or three times. 2) things will look right if you control the force (which is effectively the acceleration since A=F/mass) and then dynamically evolve the velocity and position.

    Your basic loop for realistic motion looks like (where the CAPs are vectors and dt is your timestep):

    while (motion) {
       A = get_acceleration(X, V, A, X_target, V_targer, A_target)
       V += A*dt       // V is the integral of A
       X += V*dt       // X is the integral of V
    }
    

    And really, this is about it for you're dynamic evolution.

    Then you need to decide how to determine your acceleration, i.e. write get_acceleration. There are a number of options here that depend on multiple factors, and real-life chasers employ multiple strategies. For example, if you have a lot of thrust relative to your mass (i.e. high acceleration) you probably just want to head straight at the target; but if you have a lot of mass relative to your thrust you probably want to make an interception course. If you want to slow down as you approach the target, you could reverse the acceleration when |X-X_target| becomes small (i.e. they get close) and/or their velocities are close. Also, damping can help things not oscillate, and for this, add a term to the acceleration something like -c*(V-V_target). I suggest you play around with these until you get something that matches the physical look and feel that you're aiming for.

    0 讨论(0)
提交回复
热议问题