【问题标题】:Align shape to vector using Dgame framework and D language使用 Dgame 框架和 D 语言将形状与矢量对齐
【发布时间】:2015-10-02 00:49:01
【问题描述】:

我一直在使用 Dgame 框架进行简单的模拟。

我需要移动物体与它们的速度矢量对齐。

如何使用 Dgame 做到这一点?

我看到 shape 对象有 setRotation 和 setRotationCenter。不知道如何使用这些来达到效果。我意识到默认值是围绕原点旋转。这会导致对象随时间漂移。

示例代码

struct GameObject {
Point **position;
// array of pointers to object points
Point *acceleration;
Point *velocity;
double max_speed;
double max_force;
}

shape = new Shape(Geometry.Quads,
Vertex(object.position[0].x, object.position[0].y),
Vertex(object.position[1].x, object.position[1].y),
Vertex(object.position[2].x, object.position[2].y),
Vertex(object.position[3].x, object.position[3].y))

// rotate shape to face velocity here
shape.move(object.velocity.x, object.velocity.y);

【问题讨论】:

  • 这是你想做的吗? i.webfreak.org/DE8632
  • @WebFreak001 这就是我想要做的
  • 我猜只是旋转而没有位置?
  • @WebFreak001 是的,只是旋转

标签: graphics 2d coordinates d 2d-games


【解决方案1】:

您可以使用atan2 实现此目的。根据纹理的方向,您可能需要稍微更改值或添加 90/-90 度。

对于面朝上的纹理:

rotation = atan2(-velocity.x, velocity.y);

对于面向右侧的纹理:

rotation = atan2(-velocity.y, -velocity.x);

您可能需要将结果从弧度转换为度数或其他方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2014-04-17
    • 2015-08-18
    相关资源
    最近更新 更多