根据旋转移动对象
Moving an object based on its rotation
我有这艘船 sprite(目前为原型),我一直试图沿着它的旋转方向移动它,但效果不是很好。
<-- 视频 -->
https://imgur.com/a/gd1ac7O
<-- 我认为应该发生的示例图片 -->
在研究的时候,我看到有时余弦是x位置,符号是y位置,这让我有点不知所措。但有时我看到的不是这样,这让我感到奇怪,因为我认为 x 应该使用正弦而 y 应该使用余弦。
代码:
package com.lance.seajam;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.Gdx;
import java.lang.Math;
public class Player {
private final float rotSpeed;
private final float movementSpeed;
private final Texture texture;
private final int textureWidth;
private final int textureHeight;
private final SpriteBatch batch;
private Vector2 position;
private int rotation;
private float originX;
private float originY;
private int realRot;
private double DEG2RAD = 180 / Math.PI;
public Player(Texture texture, SpriteBatch batch) {
this.texture = texture;
this.batch = batch;
this.rotation = 0;
this.textureWidth = texture.getWidth();
this.textureHeight = texture.getHeight();
this.position = new Vector2(300, 300);
this.originX = textureWidth/2;
this.originY = textureHeight/2;
this.rotSpeed = 3f;
this.movementSpeed = 3f;
}
public void Movement() {
if (Gdx.input.isKeyPressed(Keys.A)) rotation += rotSpeed;
if (Gdx.input.isKeyPressed(Keys.D)) rotation -= rotSpeed;
realRot = rotation;
if (rotation >= 360) realRot = 360;
if (rotation <= 0) realRot = 0;
double xVel = 0;
double yVel = 0;
xVel += Math.cos(realRot) * movementSpeed;
yVel += Math.sin(realRot) * movementSpeed;
if (Gdx.input.isKeyPressed(Keys.W)) {
position.x += xVel;
position.y += yVel;
}
System.out.println(xVel);
}
public void DrawSprite() {
batch.draw (
texture, position.x, position.y,
originX, originY, textureWidth,
textureHeight, 1, 1,
rotation, 1, 1, textureWidth,
textureHeight, false, false
);
}
}
如果您创建一个额外的 Vector2
成员,direction
在您的 Player
class 中,您可以获得 libGDX 通过在 Vector2
上使用 rotateDeg
方法计算 x
和 y
增量:
direction.set(Vector2.X).rotateDeg(realRot);
这会将 direction
设置为 (1, 0),然后按您的旋转量旋转它。
然后你可以将 direction.x
和 direction.y
添加到你的 position
.
position.x += direction.x;
position.y += direction.y;
甚至更短:
position.add(direction);
上述动画 Player
的完整来源是:
public class Player {
private final float rotSpeed;
private final float movementSpeed;
private final Texture texture;
private final int textureWidth;
private final int textureHeight;
private final SpriteBatch batch;
private Vector2 position;
private Vector2 direction = new Vector2(Vector2.X); // Add this
private int rotation;
private float originX;
private float originY;
private int realRot;
private double DEG2RAD = 180 / Math.PI;
public Player(Texture texture, SpriteBatch batch) {
this.texture = texture;
this.batch = batch;
this.rotation = 0;
this.textureWidth = texture.getWidth();
this.textureHeight = texture.getHeight();
this.position = new Vector2(0, 0);
this.originX = textureWidth/2;
this.originY = textureHeight/2;
this.rotSpeed = 3f;
this.movementSpeed = 3f;
}
public void Movement() {
if (Gdx.input.isKeyPressed(Input.Keys.A)) rotation += rotSpeed;
if (Gdx.input.isKeyPressed(Input.Keys.D)) rotation -= rotSpeed;
realRot = rotation;
// You don't really need this
//if (rotation >= 360) realRot = 360;
//if (rotation <= 0) realRot = 0;
// At each update, set the direction vector to point to the right, and then rotate
// it using your readRot variable.
direction.set(Vector2.X).rotateDeg(realRot);
direction.scl(movementSpeed); // Scale the direction by you speed
if (Gdx.input.isKeyPressed(Input.Keys.W)) {
position.x += direction.x;
position.y += direction.y;
}
}
public void DrawSprite() {
batch.draw (
texture, position.x, position.y,
originX, originY, textureWidth,
textureHeight, 1, 1,
rotation, 1, 1, textureWidth,
textureHeight, false, false
);
}
}
我有这艘船 sprite(目前为原型),我一直试图沿着它的旋转方向移动它,但效果不是很好。
<-- 视频 -->
https://imgur.com/a/gd1ac7O
<-- 我认为应该发生的示例图片 -->
代码:
package com.lance.seajam;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.Gdx;
import java.lang.Math;
public class Player {
private final float rotSpeed;
private final float movementSpeed;
private final Texture texture;
private final int textureWidth;
private final int textureHeight;
private final SpriteBatch batch;
private Vector2 position;
private int rotation;
private float originX;
private float originY;
private int realRot;
private double DEG2RAD = 180 / Math.PI;
public Player(Texture texture, SpriteBatch batch) {
this.texture = texture;
this.batch = batch;
this.rotation = 0;
this.textureWidth = texture.getWidth();
this.textureHeight = texture.getHeight();
this.position = new Vector2(300, 300);
this.originX = textureWidth/2;
this.originY = textureHeight/2;
this.rotSpeed = 3f;
this.movementSpeed = 3f;
}
public void Movement() {
if (Gdx.input.isKeyPressed(Keys.A)) rotation += rotSpeed;
if (Gdx.input.isKeyPressed(Keys.D)) rotation -= rotSpeed;
realRot = rotation;
if (rotation >= 360) realRot = 360;
if (rotation <= 0) realRot = 0;
double xVel = 0;
double yVel = 0;
xVel += Math.cos(realRot) * movementSpeed;
yVel += Math.sin(realRot) * movementSpeed;
if (Gdx.input.isKeyPressed(Keys.W)) {
position.x += xVel;
position.y += yVel;
}
System.out.println(xVel);
}
public void DrawSprite() {
batch.draw (
texture, position.x, position.y,
originX, originY, textureWidth,
textureHeight, 1, 1,
rotation, 1, 1, textureWidth,
textureHeight, false, false
);
}
}
如果您创建一个额外的 Vector2
成员,direction
在您的 Player
class 中,您可以获得 libGDX 通过在 Vector2
上使用 rotateDeg
方法计算 x
和 y
增量:
direction.set(Vector2.X).rotateDeg(realRot);
这会将 direction
设置为 (1, 0),然后按您的旋转量旋转它。
然后你可以将 direction.x
和 direction.y
添加到你的 position
.
position.x += direction.x;
position.y += direction.y;
甚至更短:
position.add(direction);
上述动画 Player
的完整来源是:
public class Player {
private final float rotSpeed;
private final float movementSpeed;
private final Texture texture;
private final int textureWidth;
private final int textureHeight;
private final SpriteBatch batch;
private Vector2 position;
private Vector2 direction = new Vector2(Vector2.X); // Add this
private int rotation;
private float originX;
private float originY;
private int realRot;
private double DEG2RAD = 180 / Math.PI;
public Player(Texture texture, SpriteBatch batch) {
this.texture = texture;
this.batch = batch;
this.rotation = 0;
this.textureWidth = texture.getWidth();
this.textureHeight = texture.getHeight();
this.position = new Vector2(0, 0);
this.originX = textureWidth/2;
this.originY = textureHeight/2;
this.rotSpeed = 3f;
this.movementSpeed = 3f;
}
public void Movement() {
if (Gdx.input.isKeyPressed(Input.Keys.A)) rotation += rotSpeed;
if (Gdx.input.isKeyPressed(Input.Keys.D)) rotation -= rotSpeed;
realRot = rotation;
// You don't really need this
//if (rotation >= 360) realRot = 360;
//if (rotation <= 0) realRot = 0;
// At each update, set the direction vector to point to the right, and then rotate
// it using your readRot variable.
direction.set(Vector2.X).rotateDeg(realRot);
direction.scl(movementSpeed); // Scale the direction by you speed
if (Gdx.input.isKeyPressed(Input.Keys.W)) {
position.x += direction.x;
position.y += direction.y;
}
}
public void DrawSprite() {
batch.draw (
texture, position.x, position.y,
originX, originY, textureWidth,
textureHeight, 1, 1,
rotation, 1, 1, textureWidth,
textureHeight, false, false
);
}
}