C# 如何将向量与Double相乘?
我试着让一个精灵用这个等式来回移动:C# 如何将向量与Double相乘?,c#,xna,C#,Xna,我试着让一个精灵用这个等式来回移动: SpriteTexture sprite; Vector2 position; Vector2 p1 = new Vector2(0, 100), p2 = new Vector2(0, 0); double currentTime = 0, timestep = 0.01; ... protected override void Update(GameTime gameTime) { position = currentTime
SpriteTexture sprite;
Vector2 position;
Vector2 p1 = new Vector2(0, 100),
p2 = new Vector2(0, 0);
double currentTime = 0, timestep = 0.01;
...
protected override void Update(GameTime gameTime)
{
position = currentTime * p1 + (1 - currentTime) * p2;
currentTime += timestep;
if (currentTime >= 1 || currentTime <= 0)
{
timestep *= -1;
}
}
SpriteTexture-sprite;
矢量2位置;
向量2 p1=新向量2(0,100),
p2=新矢量2(0,0);
双电流时间=0,时间步长=0.01;
...
受保护覆盖无效更新(游戏时间游戏时间)
{
位置=当前时间*p1+(1-当前时间)*p2;
currentTime+=时间步长;
如果(currentTime>=1 | | currentTime尝试使用:
Vector2.Multiply Method (Vector2, Single)
此处记录的API:
不能使用*运算符将向量与double相乘,这就是错误的原因。尝试使用:
Vector2.Multiply Method (Vector2, Single)
此处记录的API:
不能使用*运算符将向量与double相乘,这就是错误的原因。Vector2支持浮点乘法,或者可以手动将向量的各个分量与double相乘(将其转换为浮点)
例如:
position = currentTime * (float)p1 + ((float)(1 - currentTime)) * p2;
或者如果你想对一个向量进行单独的乘法
// Assuming myVector is a Vector3:
myVector.X *= (float)someDoubleValue;
myVector.Y *= (float)someDoubleValue;
myVector.Z *= (float)someDoubleValue;
Vector2支持浮点乘法,也可以手动将向量的各个分量乘以double(将其转换为浮点)
例如:
position = currentTime * (float)p1 + ((float)(1 - currentTime)) * p2;
或者如果你想对一个向量进行单独的乘法
// Assuming myVector is a Vector3:
myVector.X *= (float)someDoubleValue;
myVector.Y *= (float)someDoubleValue;
myVector.Z *= (float)someDoubleValue;
这条线引起了问题
position = currentTime * p1 + (1 - currentTime) * p2
在这里,您试图将currentTime
adouble
与p1
aVector
实例相乘
要乘以向量实例,您需要使用这一行导致了问题
position = currentTime * p1 + (1 - currentTime) * p2
在这里,您试图将currentTime
adouble
与p1
aVector
实例相乘
要将向量实例相乘,您需要使用尝试使用double或将其转换为float,然后将Vector2
乘以currentTime
1.
position = Vector2.Multiply(p1, (float)currentTime) +
Vector2.Multiply(p2, (float)(1 - currentTime));
position = (p1 * (float)currentTime) + (p2 * (float)(1 - currentTime));
2.
position = Vector2.Multiply(p1, (float)currentTime) +
Vector2.Multiply(p2, (float)(1 - currentTime));
position = (p1 * (float)currentTime) + (p2 * (float)(1 - currentTime));
尝试使用double或将其转换为float,然后将Vector2
乘以currentTime
1.
position = Vector2.Multiply(p1, (float)currentTime) +
Vector2.Multiply(p2, (float)(1 - currentTime));
position = (p1 * (float)currentTime) + (p2 * (float)(1 - currentTime));
2.
position = Vector2.Multiply(p1, (float)currentTime) +
Vector2.Multiply(p2, (float)(1 - currentTime));
position = (p1 * (float)currentTime) + (p2 * (float)(1 - currentTime));
你能验证它是否在增加值吗?你是如何绘制你的精灵的?我想是这样的,很高兴你修复了它。你能验证它是否在增加值吗?你是如何绘制你的精灵的?我想是这样的,很高兴你修复了它。你有没有考虑过对currentTime和timestep使用浮动代替doublesHave考虑对currentTime和timestep使用浮点,而不是双精度浮点