Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Offline mc-kay  
#1 Posted : Wednesday, November 23, 2011 9:02:14 PM(UTC)
mc-kay

Medals: Admin

Joined: 8/24/2011(UTC)
Posts: 138
Location: Hannover

Thanks: 1 times
Was thanked: 12 time(s) in 7 post(s)
Hi,
I want to attach a Material2D graphic to a Farseer rectangle object to simulate a projectile shot.
So I use Physics.CreateRectangle() to create my Farseer object like this:
Code:
Physics.Gravity = new Vector(0f, 0.5f, 0f);
Physics.DebugEnabled = true;
bullet = Physics.CreateRectangle(projectile.Size.Width, projectile.Size.Height, 1f);
bullet.Position2D = new Point(0.205f, 0.66f);
bullet.Friction = 1f;
bullet.DebugColor = Color.Red;
bullet.IsActive = false;

"projectile" is my Material2D object.
Then I try to draw the "projectile" at the current "bullet.Position2D" (with some debugging stuff):
Code:
Rect.DrawOutline(new Rectangle(bullet.Position2D, projectile.Size), Color.Red);
Circle.DrawFilled(bullet.Position2D, 0.005f, Color.Green);
projectile.Draw(new Rectangle(bullet.Position2D, projectile.Size));

The Circle is rendered right, but the "projectile" is not rendered at the same position as the "bullet" object.
Look at the attached picture to see the result.
What am I doing wrong?

Edited by user Wednesday, November 23, 2011 9:04:05 PM(UTC)  | Reason: Not specified

mc-kay attached the following image(s):
projectile.png (3kb) downloaded 16 time(s).

You cannot view/download attachments. Try to login or register.

Wanna join the discussion?! Login to your forum accountregister a new account. Or Connect via Facebook Twitter Google

Offline vickfl  
#2 Posted : Wednesday, November 23, 2011 9:27:01 PM(UTC)
vickfl

Joined: 10/7/2011(UTC)
Posts: 26
Location: Lübeck, SH, Germany

Thanks: 4 times
Was thanked: 2 time(s) in 2 post(s)
The position value of the physicsbody describes the center of the physicsbody, but the position value of the material describes the upper left corner.
All you have to do is to subtract the draw position by the material size divided by two:

Code:
_texture.Draw(new Rectangle(Body.Position2D - _texture.Size / 2, _texture.Size));

Edited by user Wednesday, November 23, 2011 9:33:45 PM(UTC)  | Reason: Not specified

Offline mc-kay  
#3 Posted : Wednesday, November 23, 2011 9:50:42 PM(UTC)
mc-kay

Medals: Admin

Joined: 8/24/2011(UTC)
Posts: 138
Location: Hannover

Thanks: 1 times
Was thanked: 12 time(s) in 7 post(s)
So easy, thank you ThumpUp
Offline Benjamin  
#4 Posted : Wednesday, November 23, 2011 11:48:26 PM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
Florian is right, but the code can even be simpler with the Rectangle.FromCenter method, which works like the physics works internally:

Code:

texture.Draw(Rectangle.FromCenter(Body.Position2D, texture.Size));


Rss Feed  Atom Feed
Users browsing this topic
OceanSpiders 2.0
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2022, Yet Another Forum.NET
This page was generated in 0.046 seconds.