Apr 26, 2010 at 9:31 AM
Edited Apr 26, 2010 at 9:33 AM
I just happened to look into creating Chains in the engine, and while I managed to get a simple one going successfully, I've got a weird visual artifact on my chain's sprite (just a black square per body for now).
Here's a video showing exactly what I mean:
The weird "banding" effect that is seen is kinda proportional to the size of the sprite. If I scale down the size of the black squares, the banding is less noticeable, but still very much present.
Here's the code that I'm using to achieve what's seen in the video:
private Path chain;
private Sprite chainSprite;
private List<Sprite> chainSpriteList;
private int i = 0;
In the Initialise() method:
chain = ComplexFactory.Instance.CreateChain(physicsSimulator, new Vector2(0.0f, 15.0f), new Vector2(0.0f, 5.0f), 15, 0.5f, LinkType.RevoluteJoint);
Joint chainPin = JointFactory.Instance.CreateFixedRevoluteJoint(physicsSimulator, chain.Bodies.First(), chain.Bodies.First().Position);
foreach (Body body in chain.Bodies)
chainSprite = SpriteManager.AddSprite("Content/Sprites/Dummy");
chainSprite.ScaleX = 0.2f;
chainSprite.ScaleY = 0.2f;
And then in Update():
foreach (Sprite sprite in chainSpriteList)
if (i < chain.Bodies.Count())
sprite.X = chain.Bodies.ElementAt(i).Position.X;
sprite.Y = chain.Bodies.ElementAt(i).Position.Y;
sprite.RotationZ = chain.Bodies.ElementAt(i).Rotation;
i = 0;
So everything seems pretty much fine and the chain behaves the way I want it to, but its just that weird texture artifact that I'm interested in getting rid off, as for my actual game, I'll probably use a real texture of a rope or chain, and I don't really
want it looking weird like in the video.
Anyone with any ideas/suggestions, I'd be glad to hear from you!
Thanks in advance.
Apr 26, 2010 at 7:27 PM
Edited Apr 26, 2010 at 7:30 PM
Hah! All fixed up! :D
So the problem lies in the way I was updating the sprites for each of the link bodies of the chain.
Here's how the update should be done to remove the banding effect:
for (int i = 0; i < chain.Bodies.Count; i++)
chainSpriteList[i].X = chain.Bodies[i].Position.X;
chainSpriteList[i].Y = chain.Bodies[i].Position.Y;
chainSpriteList[i].RotationZ = chain.Bodies[i].Rotation;
I'm currently using the FlatRedBall XNA Engine for this project, but for anyone with similar interests/problems, the code from the above post and this one should be pretty much adaptable to regular XNA as well. Just ignore the update code from the first post
and use this one instead.