移动多个物体 [英] moving more than one object around

查看:52
本文介绍了移动多个物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用使用C#的XNA2D.我是C#的新手
我正在尝试移动一组对象.到目前为止,我一次只能移动一个对象.
这是我的代码.

I am using XNA2D which uses C#. I am new to C#
i am trying to move a group of objects around. So far, i am able to move only one object at a time.
Here is my code.

if (input == null)
{
    throw new ArgumentNullException("input"");
}

mouseStateCurrent = Mouse.GetState();
drag = Vector2.Zero;

foreach (ALU a in ALU.List)
{
    if (a.Rectangle.Contains(mouseStatePrevious.X, mouseStatePrevious.Y) && 
        !dragging && 
        mouseStateCurrent.LeftButton == ButtonState.Pressed)
    {
        selectedALU = a;
        dragging = true;
        drag.X = selectedALU.Position.X - mouseStatePrevious.X;
        drag.Y = selectedALU.Position.Y - mouseStatePrevious.Y;
    }

    if (mouseStateCurrent.LeftButton == ButtonState.Released)
    {
        if (dragging)
        {
            selectedALU.Position.X = (float)((Math.Round(selectedALU.Position.X / 64)) * 64);
            selectedALU.Position.Y = (float)((Math.Round(selectedALU.Position.Y / 64)) * 64);
        }

        dragging = false;
    }
    mouseStatePrevious = mouseStateCurrent;
}

推荐答案


有很多不错的方法可以解决此问题,最好的IMO是,如果您使用的是子分组"方法,则可以从质心(例如光标)映射临时分组,并使用添加到光标中的映射中的增量绘制位移..这可能是最好的方法,因为它将允许对象自己仍然具有行为",并让您将选择移动"向量视为扰动,而不是显式地对待.

(如果需要,可以使用这种方法使对象在选择组中仍然相对彼此移动)

或者,您可以考虑应用转换,这可能有助于从代码角度清理方法,但执行速度会稍慢.
EdMan196 is correct there is no defacto way to move multiple objects "simultaniously".. that said....

There are a number of decent ways to approach this, the best IMO is if you are using a "subgrouping" approach you can map a temporary grouping from a centroid (say the cursor) and draw using the deltas from the mapping added to your cursor displacement.. this is probably the best approach as it will allow for the object to still have "behavior" on their own and lets you treat the "selection moved" vector as a perturbation rather then explicitly.

(using this approach you could get the objects to still move relative to one another inside your selection group if you so desired)

alternatively you could look at applying a transform, which might help clean up the approach from a code perspective but will be slightly slower in execution.


这篇关于移动多个物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆