PolygonEntity

Topics: User Forum
Dec 19, 2006 at 7:23 PM
Hi!

Why PolygonEntity constructor need PhysicsSimulator:

public PolygonEntity(Vertices vertices, PhysicsSimulator physicsSimulator) {

_rigidBody = new PolygonRigidBody(1, vertices);

//setup some default physics parameters for all rigid body sprites
_rigidBody.RotationalDragCoefficient = 50;
_rigidBody.LinearDragCoefficient = .001f;
_rigidBody.FrictionCoefficient = .8f;
_rigidBody.RestitutionCoefficient = .1f;
}


It is not used there?
Dec 19, 2006 at 7:50 PM
I'm trying to use PolygonEntity:

Vertices treeVerticles = new Vertices();

treeVerticles.Add(new Vector2(0, -123));
treeVerticles.Add(new Vector2(-38, -75));
treeVerticles.Add(new Vector2(-78, -75));
treeVerticles.Add(new Vector2(-13, -130));
treeVerticles.Add(new Vector2(-58, -134));
treeVerticles.Add(new Vector2(0, -186));
treeVerticles.Add(new Vector2(0, -194));
treeVerticles.Add(new Vector2(-94, -194));
treeVerticles.Add(new Vector2(-106, -200));
treeVerticles.Add(new Vector2(-106, -224));
treeVerticles.Add(new Vector2(-142, -224));
treeVerticles.Add(new Vector2(-142, -224));
treeVerticles.Add(new Vector2(-142, -199));
treeVerticles.Add(new Vector2(-157, -194));
treeVerticles.Add(new Vector2(-256, -191));
treeVerticles.Add(new Vector2(-256, -185));
treeVerticles.Add(new Vector2(-195, -133));
treeVerticles.Add(new Vector2(-240, -132));
treeVerticles.Add(new Vector2(-239, -126));
treeVerticles.Add(new Vector2(-239, -126));
treeVerticles.Add(new Vector2(-179, -76));
treeVerticles.Add(new Vector2(-209, -75));
treeVerticles.Add(new Vector2(-210, -70));
treeVerticles.Add(new Vector2(0, -123));

christmasTreeEntity = new PolygonEntity(treeVerticles, null);
christmasTressEntityView = new SpriteEntityView(christmasTreeEntity, @"Content\Textures\tree");

christmasTreeEntity.Mass = 1;
christmasTreeEntity.Position = new Vector2(8, 1f);


SceneManager.Add(christmasTressEntityView);
EntityManager.Add(christmasTreeEntity);

floorEntity = new ScreenBorderEntity(800, 600);
EntityManager.Add(floorEntity);




base.Initialize();


After this code my christmasTree don't collide with floorEntity. Tree is loaded from 256x256 png bitmap, as far as I know when all verticles should be minus when verticle is inside rigidbody.

Is this code correct?
Coordinator
Dec 20, 2006 at 12:05 PM
When you create your polygon. Create it as if you were creating around the 0,0 of the screen. Also, make sure your vertices are defined counter-clockwise.

What you have there looks ok, just check your vertices. You might try a more simple object to make sure you have it working, then try the more complex one once the simple one is working.

Dec 20, 2006 at 12:30 PM
I did this as you said.
I send you project with bitmap and PolygonEntity - can you check it?
I checked all points on Tree using Paint.NET. Maybe I should use ConvertToMeters on each point coordinate?
Dec 20, 2006 at 1:11 PM
This code work better, but still there is problem (3/5 of tree go out of screen)

Vertices treeVerticles = new Vertices();

//Rectangle
treeVerticles.Add(new Vector2(0, - ConvertToMeters(256)));
treeVerticles.Add(new Vector2(-ConvertToMeters(256), -ConvertToMeters(256)));
treeVerticles.Add(new Vector2(-ConvertToMeters(256), 0));
treeVerticles.Add(new Vector2(0, 0));
treeVerticles.Add(new Vector2(0, -ConvertToMeters(256)));

christmasTreeEntity = new PolygonEntity(treeVerticles, null);
christmasTressEntityView = new SpriteEntityView(christmasTreeEntity, @"Content\Textures\tree");

christmasTreeEntity.Mass = 1;
christmasTreeEntity.Position = new Vector2( ConvertToMeters(256), ConvertToMeters(256));


SceneManager.Add(christmasTressEntityView);
EntityManager.Add(christmasTreeEntity);

floorEntity = new ScreenBorderEntity(800, 600);
EntityManager.Add(floorEntity);
Dec 20, 2006 at 4:44 PM
but this same code with RectangleEntity works properly:

christmasTreeEntity = new RectangleEntity(ConvertToMeters(256), ConvertToMeters(256));
christmasTressEntityView = new SpriteEntityView(christmasTreeEntity, @"Content\Textures\tree");

christmasTreeEntity.Mass = 1;
christmasTreeEntity.Position = new Vector2( ConvertToMeters(256), ConvertToMeters(256));
Coordinator
Dec 20, 2006 at 5:15 PM
try centering you polygon rectangle around 0,0

-256,-256 upper left
-256,256 lower left
256,256 bottom right
256, -256 upper right

And yes, you should still convert to meters. If this doesn't work, let me know.

Also, when you create a entity using RectangleEntity, it actually uses more than just the corner vertices. It adds vertices along the edges for better, more accurate collision dection. Not sure if this could be the difference.
Dec 20, 2006 at 7:41 PM
ok - now it is working correct!
Thank you :)