- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一艘宇宙飞船,那艘宇宙飞船可以 360 度在太空中移动。
宇宙飞船需要 2d 推力动画。信任动画需要位于飞船的底部中线。我有以下变量。
_Rotation
_Spaceship.width
_Spaceship.height
_Spaceship.Position(x,y)
我也上传了我的问题的图片,以防人们不理解我的错误解释:
两个动画都像这样渲染:
this._itemAnimation.render(this.getPosition(), canvas, this._degrees);
this._thrustAnimation.render(this.thrustPosition(), canvas, this._degrees);
到目前为止我已经尝试过但失败了:
_thurstPosition.set(((int)_object_x + Math.cos(_degrees) * _itemAnimation.getWidth() / 2) ,
((int)_object_y + Math.sin(_degrees) * _itemAnimation.getWidth() / 2));
我失败了,有人帮助我。
--- 更新 ---
我更新了代码,以便更好地理解:
int SpaceshipCenterX = getPosition().x + (_SpaceshipAnimation.getWidth() / 2) - (_thrustAnimation.getWidth() / 2);
int SpaceshipCenterY = getPosition().y + ((_SpaceshipAnimation.getHeight() / 2) - (_thrustAnimation.getHeight() / 2));
double OffSetCos = Math.cos(_spaceshipDegrees);
double OffSetSin = Math.sin(_spaceshipDegrees);
_thurstPosition.set
(
(int)(SpaceshipCenterX + (SpaceshipAnimation.getWidth() / 2) * OffSetCos)
,
(int)(SpaceshipCenterY + (SpaceshipAnimation.getHeight() / 2) * OffSetSin)
);
我仍然无法让它发挥作用。它绕着飞船旋转,但速度非常快,并且到处闪烁。
--- 更新 2 ---
这几乎可以工作,但还差得太远:
int xOffset = -1 * (_itemAnimation.getWidth() / 2);
double DegreeToRadien = Math.toRadians(_degrees);
_thurstPosition.set
(
(int)(((xOffset) * Math.cos(DegreeToRadien)) + getPosition().x),
(int)(((xOffset) * Math.sin(DegreeToRadien)) + getPosition().y)
);
最佳答案
假设您正在使用此坐标/角度系统:
90 (pi/2) - Up
^
|
Left - 180 (pi) <----|----> 0 - Right
|
v
Down - 270 (3pi/2)
你的宇宙飞船将以 0 度向右行驶
>[ } 0
然后,对于任何方向,您需要相对于飞船中心平移推力,假设我们在 x 方向上平移
offset = -1 * width/2;
然后将其旋转飞船的角度,最后将其平移飞船的位置。
要计算此变换,请以相反顺序将 3 个变换写为矩阵,然后将它们相乘,变换从 (0,0) 开始的点
[1 0 spaceshipX] [cos(dir) -sin(dir) 0] [1 0 -width/2] [0]
[0 1 spaceshipY] [sin(dir) cos(dir) 0] [0 1 0 ] [0]
[0 0 1 ] [ 0 0 1] [0 0 1 ] [1]
这样你就会得到推力的位置
thrustX = (-width/2)cos(dir) + spaceshipX
thrustY = (-width/2)sin(dir) + spaceshipY
所以我想您只是错过了需要减去 width/2 而不是添加它的事实。
我已使用正确且更易读的语法对其进行了编辑。到处使用下划线确实会损害可读性。我假设你有一个宇宙飞船类,并且宇宙飞船有宽度、高度、x 位置、y 位置和旋转。您有一个推进器类,它也有宽度、高度、x 位置、y 位置和旋转。 (它们可以从 Sprite 抽象类继承)。为了设置推进器对象的位置,我们调用 Thruster.setPosition(x,y);
int xOffset = -1 * (spaceship.width / 2);
thruster.setPosition(
(int)(((xOffset) * Math.cos(spaceship.rotation)) + spaceship.x),
(int)(((xOffset) * Math.sin(spaceship.rotation)) + spaceship.y)
);
希望这能让您清楚需要在何处设置哪些值。如果没有看到更多内容、这些变量的声明位置以及它们的实际含义,我就无法解读您的代码。
只是作为结论,我想您可能已经发现了。 Math.cos 和 Math.sin 要求角度以弧度为单位,而不是度数。我在这里给出的解决方案是正确的,并且我已经展示了如何通过执行矩阵计算来计算任何相对定位的对象的位置。您只需记住 spaceship.rotation 必须以弧度为单位,或者您必须在将其传递给 Math.cos() 或 Math.sin() 之前将其从度数转换为弧度。
int xOffset = -1 * (spaceship.width / 2);
double radians = Math.toRadians(spaceship.rotation);
thruster.setPosition(
(int)(((xOffset) * Math.cos(radians)) + spaceship.x),
(int)(((xOffset) * Math.sin(radians)) + spaceship.y)
);
关于java - 设置移动和旋转物体上的推力位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10018173/
大家好,本文实现了相机碰撞检测,使相机不穿墙壁、物体,并给出了思路和代码,感谢大家~ 关键词:数字孪生、three.js、Web3D、WebGL、相机碰撞、游戏相机 我正在承接Web3D数字孪生项
我有一个大小为 12*30 的字符串的图像。我想创建一个动画,让它给人一种拉伸(stretch)字符串的感觉。我通过缩放图像来做到这一点,但我面临的问题是缩放图像没有发生碰撞。它仅出现在原始图像大小的
我的对象列表是如此初始化: $( function() { var $container = $('div.hikashop_products'); $container.isotop
我听说 swing 默认情况下是双缓冲的。我不想让 Swing 双缓冲。我正在使用双缓冲,我想添加一些 Swing 对象(现在只是添加到 JPanel 中的 JButton,然后再添加到 JFrame
几天来我一直在思考最好的解决方案,但似乎无法找到正确的想法。 我有一 block (物体),我想将它们放入尽可能小的空间中。我最终寻找的是这样的东西 http://i.stack.imgur.com/
我的纹理不仅仅是一个盒子或圆形,我的 body 需要与这个形状相同,所以我想结合多个 body 来达到我想要的形状,这甚至可能吗?或者有更好的方法吗?我正在使用带有 libgdx 框架的 java。
我遇到的情况是,我有很多计算机并且需要有唯一的 ID。 他们会通过 API 请求发送其 uniqueId。该对象看起来像 class ID { long timestamp; int id; }
我正在尝试检测一张卡片,但问题是有时图像不好并且有多个背景,如下所示: 没有很好地定义边缘 ![没有很好地定义边缘][1] 示例背景 ![示例背景][2] 我这样做了: gray = cv2.cvtC
我正在尝试从仅包含一辆车和简单背景的图像中分割汽车,如 但是我从我的实现中得到的是这个 和 分别 但它非常容易处理几乎已经分割的图像,例如。 给出类似 的结果 我使用的代码是 import cv2 i
我正在开发一个项目,在该项目中我从另一个对象/函数中引用一个变量。然而我总是返回 false。我不确定我是否正确调用它。 这是验证函数: app.validation = function(){
数组只是伪装的对象吗?为什么/为什么不呢?他们以什么方式(这样/不是)? 我一直认为 JS 中的数组和对象本质上是相同的,主要是因为访问它们是相同的。 var obj = {'I': 'me'}; v
我正在使用 PlayN 构建一个涉及石头的游戏,用户必须在物理世界中移动(通过重力等)。我希望用户能够使用触摸板直接操纵石头,并通过以下方式给它们一个速度拖拽并扔掷它们。 现在我有一个实现,其中每个石
http://jsfiddle.net/goldrunt/jGL84/42/这是来自这个 JS fiddle 的第 84 行。通过取消注释第 141-146 行,可以对球应用 3 种不同的效果。 'b
我学习Linux平台下的OpenGL。最近,我尝试使用 glutBitmapCharacter() 创建的文本作为 glu 或 glut 提供的一些二次对象的纹理。但是,glutBitmapChara
我正在使用 AndEngine 创建一个带有 box2d 扩展名的游戏。我想实现一个条件,当两个物体碰撞时,它们应该被移除或重生。最初我尝试使用 if(sprite1.collidesWith(spr
我制作了一个小型 3d 引擎。 但我在旋转功能方面遇到了一些问题。它们使物体不时拉伸(stretch)。这是数学: this.rotateX = function(angle) { var c
我在 Canvas 上制作了一个矩形 mask ,我需要 mask 外的任何东西都具有 0.8 的不透明度,因此 mask 外的所有对象都被视为不透明请看一下 fiddle 。 http://jsfi
我是 Direct3D 的新手,我在一个项目中从网络摄像头拍照并在它前面绘制一些 3D 对象。 我能够使用正交投影将网络摄像头图像渲染为背景。 //init matrix D3DXMatri
我是一名优秀的程序员,十分优秀!