c# - Slow, sweeping turns instead of instantly facing the target -


I have a code to face a goal and move on to it:

 < Code> var X: number = _targetPosition.value.x - _position.value.x; Var y: number = _targetPosition.value.y - _position.value.y; Var Target Angel: Number = Mathematics.Tan 2 (Y, X); _angle.value = targetAngle; _velocity.valuestxe (mathematics (target angle) * 90, math sin (target angle) * 90);   

I would rather say a frame instead of a two degree (or quarter radian or whatever you want to use, I can convert it anyway)? Its goal may be increasing, so I can not use the formula that takes time to meditate ... instead of snaping it should be changed to a frame of X degree.

In addition to this, if you can show me how to apply this rotation takes into account a delta time which would be great, but I can try to understand it.

Thank you!

Unless you are compelled to run your program at a fixed time, Even the algorithm which fails to pass the time, is likely to be wrong. Depending on the hardware, framerates may vary wildly; A solution that works on 80 fps on your computer, is not running on the same, which is running at 200 FPS at Supersuper's $ 5000 Alienware.

You need to calculate the difference between the current angle of the object and its target angle, then increase the angle with some amount depending on the difference between those two angles, passing through the previous frame. The time will be multiplied by the time.

Consider the following (untested code above my head):

  // Find the difference in angles, always at least the movement var target = Math Moving in direction .atan2 (x, y); Var difference = target - _angle.value; If (difference> Math.PI) {difference = ((2 * Math. PI) - difference); } If (difference <-Math.PI) {difference = ((2 * Math.PI) + difference); } // Move the character rotation to a set amount of time per unit in var delta = (difference <0)? -Rotation change seconds: Rotation change seconds; _angle.value + = Delta * game time ElapsedGameTimeCollus;   

Whether the goal is running or not is irrelevant. The question you are asking with this code says, "Where is the target where and what do I have to face it?" If the position of the goal changes in the later frame, the direction in which you need to move will also change, and the object is moving in the opposite direction, as I believe you intend.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -