Kirk Backus
Kirk Backus

Reputation: 4866

Efficient way to move many Canvas Objects

The code below gets all of the positions and angles of the objects from the physics engine, and updates each object accordingly. It slows down once there is a lot of activity among the objects.

I know it is NOT the physics engine because I have the exact same thing written using OpenGL, but I can't use it for my specific application.

int bodyId = 0;
foreach (Shape shape in shapes)
{
    //Don't update sleeping objects because they are sleeping.
    //Waking them up would be terrible...
    //IGNORE THEM AT ALL COSTS!!
    if (physics.IsSleeping(bodyId))
    {
        ++bodyId;
        continue;
    }

    //Rotate transform
    RotateTransform rot = new RotateTransform(physics.GetAngle(bodyId));
    rot.CenterX = shape.Width / 2;
    rot.CenterY = shape.Height / 2;

    //Set the shape to rotate
    shape.RenderTransform = rot;

    //Body position
    Point bodyPos = physics.GetPosition(bodyId);
    Canvas.SetLeft(shape, bodyPos.X - shape.Width / 2);
    Canvas.SetTop(shape, bodyPos.Y - shape.Height / 2);

    ++bodyId;
}

I want to know if there is a more efficient way in doing the above, broken down into a couple questions.

  1. If I call Canvas.SetLeft, and Canvas.SetTop is there ever a time that the UI will start rendering inside the loop? Or if that isn't clear, how exactly do the Canvas.SetLeft and Canvas.SetTop functions work?

  2. Would it be better to use a TranslateTransform since I am already using a RotateTransform?

Upvotes: 1

Views: 965

Answers (1)

toxicate20
toxicate20

Reputation: 5410

  1. Yes, SetTop() and SetLeft() are called every time for every element you have. If you wanted to make the everything more efficient I would do the following. First you use an update function that updates the logic, i.e. you calculate where your objects should be, afterwards you handle the drawing by just looping through the objects and only use setLeft() and setTop() once instead of every update.
  2. Yes, it will save you one operation.

Maybe there is something else that could be optimized. Instead of using a matrix operation like RotateTransform() which is very CPU intensive, you could save a lot of computing power by saving the current rotational status in the shape object. Also using vectors would probably help you improve performance.

Upvotes: 1

Related Questions