C# 旋转后,如何沿精灵面对的方向移动精灵?
我可以通过船精灵上下左右移动,以及旋转它。然而,当按下“向上”键时,我希望船沿着船头的方向移动,而不是总是向上移动。我想我可以在update方法中的某个地方粘贴一个vector2变量,该变量始终指向精灵的顶端,但我无法理解如何“硬编码”精灵上的坐标 我将在下面发布我的船舶等级,我认为事情有点混乱,任何关于清理代码的建议都是欢迎的C# 旋转后,如何沿精灵面对的方向移动精灵?,c#,monogame,C#,Monogame,我可以通过船精灵上下左右移动,以及旋转它。然而,当按下“向上”键时,我希望船沿着船头的方向移动,而不是总是向上移动。我想我可以在update方法中的某个地方粘贴一个vector2变量,该变量始终指向精灵的顶端,但我无法理解如何“硬编码”精灵上的坐标 我将在下面发布我的船舶等级,我认为事情有点混乱,任何关于清理代码的建议都是欢迎的 using System; using System.Collections.Generic; using System.Linq; using System.Text
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
//width = 62 pixels
//height = 179 pixels
namespace my_first_game
{
class Ship
{
private Vector2 position = new Vector2(300, 300);
private int radius = 50;
private bool ismoving = false;
private Dir direction = Dir.down;
private int health = 3;
private float speed = 100;
public float angle = 0;
public double turntime = .5d;
private float velocity = 10f;
private float drifttime = 0f;
// Constructor for the ship class
public Ship(int shiphealth)
{
shiphealth = health;
}
//getter/setter for angle
public float Get_angle
{
get { return angle; }
set { value = angle; }
}
//get and set health
public int Health
{
get { return health; }
set { value = health; }
}
// get and set the ship's position
public Vector2 Position
{
get{ return position; }
set { value = position; }
}
public void SetAngle (float f)
{
angle = MathHelper.ToDegrees(f);
}
//update loop for the ship's state
public void update_ship(GameTime gameTime)
{
KeyboardState kstate = Keyboard.GetState();
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
float rotation = 0.02f;
//Vector2 Direction = new Vector2((float)Math.Cos(angle + 45), (float)Math.Sin(angle));
//Direction.Normalize();
//if keys are pressed, the ship moves until the keys are unpressed
if (kstate.IsKeyDown(Keys.W))
{
direction = Dir.up;
ismoving = true;
}
if (kstate.IsKeyDown(Keys.A))
{
direction = Dir.left;
ismoving = true;
}
if (kstate.IsKeyDown(Keys.D))
{
direction = Dir.right;
ismoving = true;
}
if (kstate.IsKeyDown(Keys.S))
{
direction = Dir.down;
ismoving = true;
}
if(kstate.IsKeyDown(Keys.W) && kstate.IsKeyDown(Keys.D))
{ direction = Dir.up_right;
ismoving = true;
}
if(kstate.IsKeyDown(Keys.W) && kstate.IsKeyDown(Keys.A))
{
direction = Dir.up_left;
ismoving = true;
}
if (kstate.IsKeyDown(Keys.S) && kstate.IsKeyDown(Keys.D))
{ direction = Dir.down_right;
ismoving = true;
}
if (kstate.IsKeyDown(Keys.S) && kstate.IsKeyDown(Keys.A))
{
direction = Dir.down_left;
ismoving = true;
}
//"ismoving" is used as a flag to move the ship if keys are held
//angle also rotates the boat when "A" and "D" are pressed
if (ismoving) {
switch (direction)
{
case Dir.up:
position.Y -= speed * dt;
ismoving = false;
break;
case Dir.down:
position.Y += speed * dt;
ismoving = false;
break;
case Dir.left:
angle -= rotation;
ismoving = false;
break;
case Dir.right:
angle += rotation;
ismoving = false;
break;
case Dir.up_right:
position.Y -= speed * dt;
position.X += speed * (float)turntime * dt;
angle += rotation;
ismoving = false;
break;
case Dir.up_left:
position.Y -= speed * dt;
position.X -= speed * (float)turntime * dt;
angle -= rotation;
ismoving = false;
break;
case Dir.down_right:
position.Y += speed * dt;
position.X += speed * (float)turntime * dt;
angle += rotation;
ismoving = false;
break;
case Dir.down_left:
position.Y += speed * dt;
position.X -= speed * (float)turntime * dt;
angle -= rotation;
ismoving = false;
break;
default: break;
}
}
}
}
}
以下是我的船的相关绘制方法:
//create a new rectangle around the ship_sprite
Rectangle ship_rectangle = new Rectangle(0, 0, ship_sprite.Width,
ship_sprite.Height);
Vector2 origin = new Vector2(31, 160);
spriteBatch.Draw(ship_sprite, ship.Position, ship_rectangle,
Color.White, ship.angle, origin, 1.0f, SpriteEffects.None, 1);
我怀疑这个问题是否有一个“正确”的答案,但我要做的是使用“角度”属性来确定按下“向上”按钮时的移动
我沿着三角线思考-你的半径就是船的速度。然后你会按照(伪代码!)的思路做一些事情:
这假设角度=0对应于正常平面上0度的标准解释(因此面向右侧)。如果不是这样,您需要进行相应的调整
警告:我自己还没有编写过类似的代码,所以这只是我对如何实现的估计。我鼓励您进行试验,看看不同的方法是如何工作的。经过一番修补,我找到了一个适合我的解决方案
Vector2 direction = new Vector2((float)Math.Cos(angle + 30),
(float)Math.Sin(angle + 30));
然后,当按下“向上”键时,您可以简单地将船的位置设置为其当前位置加上船的角度。这两者之和可以乘以时间差和船的速度
if(kstate.IsKeyDown(Keys.W))
{
position = position + direction * dt * speed;
}
代码清理建议:注意两个条件块(if块和big Switch()块)都包含在每个分支中执行的代码,特别是isMoving=true和isMoving=false。建议把它放在条件句后面,而不是分支里面。哇,是的,我想我根本不需要那个switch语句。谢谢。在某些情况下,拥有一个“完美”的解决方案并不重要,但只要这个问题不会导致基于观点的答案,那么一个有助于解决问题的答案已经足够有用了。
if(kstate.IsKeyDown(Keys.W))
{
position = position + direction * dt * speed;
}