Reputation: 4866
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.
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?
Would it be better to use a TranslateTransform since I am already using a RotateTransform?
Upvotes: 1
Views: 965
Reputation: 5410
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