C# 如何在移动的画布上绘制精灵
我通常会把这类事情想清楚,但我被难倒了。我怀疑我错过了一个数学组合,但无论如何 我有一个移动的背景,当前从上到下上下移动 我有一个正在移动的对象,它当前以编程方式从画布的中心左右移动 这就是问题所在,我如何使一个物体在x和y方向相对于画布上的位置移动 以下是我的相关代码:C# 如何在移动的画布上绘制精灵,c#,canvas,xna,sprite,C#,Canvas,Xna,Sprite,我通常会把这类事情想清楚,但我被难倒了。我怀疑我错过了一个数学组合,但无论如何 我有一个移动的背景,当前从上到下上下移动 我有一个正在移动的对象,它当前以编程方式从画布的中心左右移动 这就是问题所在,我如何使一个物体在x和y方向相对于画布上的位置移动 以下是我的相关代码: //Helper method private Vector2 CalculateDirection() { Vector2 calculatedDirection = new Vector2(
//Helper method
private Vector2 CalculateDirection()
{
Vector2 calculatedDirection = new Vector2((float)Math.Cos(direction),
(float)Math.Sin(direction));
calculatedDirection.Normalize();
return calculatedDirection;
}
画布上的对象
public void Update(GameTime gameTime, Vector2 center)
{
this.currentCentre = originalCentre - center;
//movement logic here
Vector2 calculatedDirection = CalculateDirection();
//deltaTime = ((float)gameTime.ElapsedGameTime.TotalMilliseconds) / 15f;
if (speed > 0f || speed < 0f)
{
///TODO: work this out!!
Velocity = calculatedDirection * speed;
float dir = (originalCentre.Y - currentCentre.Y);
position.X += Velocity.X * (1.0f - 0.9f);
position.Y = dir;// *(1.0f - 0.9f);
}
}
画布移动方法
private void determinePitchSize()
{
int newHeight = Convert.ToInt32(pitch.Height * ratio);
this.canvas = new Rectangle(
0, posHeight,
device.PresentationParameters.BackBufferWidth,
newHeight
);
}
public void increasePosHeight()
{
posHeight++;
}
public void decreasePosHeight()
{
posHeight--;
}
private void determineDirection()
{
if (!direction)
{
if (this.canvas.Height + this.canvas.Y <= this.screenY)
direction = true;
}
else
{
if (this.canvas.Y >= 0)
direction = false;
}
}
private void useDirection()
{
this.determineDirection();
if (direction)
this.increasePosHeight();
else decreasePosHeight();
}
如果你需要更多的信息,我可以在这里添加
谢谢好的,多亏了尼科,我才能够回答这个问题
Vector2 Velocity { get; set; }
Vector2 relative { get; set; }
public void Update(GameTime gameTime, Vector2 center)
{
this.currentCentre = center;
Vector2 calculatedDirection = CalculateDirection();
if (speed > 0f || speed < 0f)
{
Velocity = calculatedDirection * speed * 0.1f;
relative = relative - Velocity;
position = currentCentre + relative;
}
}
“速度”创建对象移动,以测试它是否在不同的位置结束
“相对”从中心的0,0处开始,并由速度调整
然后将位置设置为中心加上相对位置。这是由速度设置的。只需在绘图前将画布位置添加到对象位置即可?我想我已经将其过度复杂化了,我将尝试将其剥离一点,看看是否可以正确绘制。我想当我弄明白这一点的时候,我会解决它的。好的,所以在画布中间画它是很容易的。我已经掌握了更新方法中的as position=center。正如我所提到的,保持一个相对位置并执行position=center+relativePosition。你提到的简单案例与0,0的相对位置有关。谢谢,多亏你的帮助,我已经回答了这个问题。我已经从你的问题标题中删除了标记,基于共识是“否”,它们不应该。