Help with Collision

Jul 18, 2009 at 6:05 AM

Hey guys,

I'm trying to get collision working properly with my game. I have setup my bodys and geoms, but I don't know how to set it up so the collision will happen with the edges of my player texture instead of the origin point. I don't know if I'm explaning this right, but I'm trying my best.

Also my code is pasted here: http://www.ziggyware.com/codepaste.php?pasteID=1798 so you guys can look at and tell me what I'm doing wrong.

If anyone can help, that would be awsome.

Thanks,

YellowShadow

Jul 18, 2009 at 12:23 PM
Edited Jul 18, 2009 at 12:23 PM

hi you nearly got it all working properly just change this

 

spriteBatch.Draw(dummy, dummyBody.Position + dummyOrigin, null, Color.White, dummyBody.Rotation, dummyOrigin, 1, SpriteEffects.None, 0);

 

spriteBatch.Draw(platform, platformBody.Position + new Vector2(platform.Width / 2, platform.Height / 2), null, Color.White, platformBody.Rotation, new Vector2(platform.Width / 2, platform.Height / 2), 1, SpriteEffects.None, 0);

to

spriteBatch.Draw(dummy, dummyBody.Position, null, Color.White, dummyBody.Rotation, dummyOrigin, 1, SpriteEffects.None, 0);

spriteBatch.Draw(platform, platformBody.Position, null, Color.White, platformBody.Rotation, new Vector2(platform.Width / 2, platform.Height / 2), 1, SpriteEffects.None, 0);

you just draw at the normal position but rotate around the centere. this should fix your problem

Jul 18, 2009 at 6:15 PM

That works except everything draws from the origin now.

Right now it draws like this: http://img23.imageshack.us/img23/1729/oddscreenshot1.png

But I want it to draw like this: http://img122.imageshack.us/img122/8504/oddscreenshot2.png

Know how I can do this and still keep collision with the edges of the objects?

Coordinator
Jul 18, 2009 at 8:09 PM
Edited Jul 18, 2009 at 8:11 PM

Positions are relative to the centroid of the geometries. The centroid is the real center of a geoemtry (and not just width/2 and hight/2) so you need to draw your geometries with the centroid as the origin.

When you position your geometries you also need to take into account that things are position relative to the origin. In case of the ground in your first picture you need to add half a length and half a hight to the geometry to position it like in your second picture.

Edit: You can also take a look in our manual. Look at bullet number 3. (under known issues)

Jul 18, 2009 at 9:26 PM

I don't quite understand what you mean by adding half a length and half a height to the geometry.

Do you mean something like this:

platformGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSim, platformBody, platform.Width + platform.Width / 2, platform.Height + platform.Height / 2); ?

Because if I do that, it still doesn't draw right, and messes up the collision again.

Coordinator
Jul 18, 2009 at 10:34 PM
Edited Jul 18, 2009 at 10:36 PM

Try taking a look at our samples. They show exactly how it should be done.

As for your sample, this is how it should look:

platform = Content.Load("GFX\Platform");

platformBody = BodyFactory.Instance.CreateRectangleBody(physicsSim, platform.Width, platform.Height, 1);
platformBody.Position = new Vector2(1280 / 2, 720 - (platform.Height / 2));
platformBody.IsStatic = true;

platformGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSim, platformBody, platform.Width, platform.Height);

And in the drawing part:

spriteBatch.Draw(platform, platformBody.Position, null, Color.White, platformBody.Rotation, new Vector2(platform.Width / 2, platform.Height / 2), 1, SpriteEffects.None, 0);
You can use platform.Width /2 and platform.Height /2 in the origin since the geometry is symmetrical on both axes. You are doing it correctly with the polygon geometry, you are using the centroid.
I noted you added the geometry and bodies to the physics simulator twice. If you use the CreateRectangleGeom() method or CreateRectangleBody() that takes in a physics simulator, then you don't need to add them to the engine by yourself.
Beware that I've not tested this code and it might not be correct. Try it out and see if it works.
Jul 18, 2009 at 11:37 PM

Thanks, that works perfectly.