【问题标题】:Creating a projectile trajectory in Allegro在 Allegro 中创建弹丸轨迹
【发布时间】:2019-04-05 22:49:39
【问题描述】:

我正在使用 Allegro 5 用 C 语言开发一个 2D 游戏,其中来自固定位置的敌人向玩家当前位置发射弹丸。我知道我必须根据玩家的位置和敌人的位置来计算一个假想三角形的切线。但是,如何让弹丸根据该值沿直线移动?

【问题讨论】:

  • 你试过什么?什么不起作用?
  • 我不太确定如何编码角度,所以我没有取得太大的成功。
  • 那么没有重力?
  • 我不确定我明白了你的要求,但弹丸应该是直线轨迹,从 a 点(敌人角色)到 b 点(玩家角色),没有任何外部影响。

标签: c allegro allegro5


【解决方案1】:

在这种情况下,使用矢量比使用角度更容易。

一些简单的数学计算出敌人和玩家之间的向量:

# Compute the x and y displacement from the enemy to the player
dx = player_x - enemy_x
dy = player_y - enemy_y

# normalize the displacement to get the direction vector 
distance = sqrt(dx * dx + dy * dy)
projectile.dir_x = dx / distance

```

射弹只需要在更新循环期间跟随那个向量:

projectile.x += projectile.dir_x * projectile.speed * time_elapsed
projectile.y += projectile.dir_y * projectile.speed * time_elapsed

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多