0
\$\begingroup\$

I'm curently working on a system that make the player character look towards the mouse pointer. My system is effective but i'm having an issue while displaying the rectangle.

In the Following code i test the mouse position by translating it in the world and then i test if the world pos of the mouse is greater than the position of the player or not. if the mouse is on left side of the boundary, the player will rotate to look at it. To make the rotation, i put the X coordinate on the other side of the position and i make the width negative. THis normaly should emulate the rotation. but my rectangle does not shows up.

Point MousePos = Vector2.Transform(Mouse.GetState().Position.ToVector2(), Matrix.Invert(Camera.GetViewMatrix())).ToPoint;
if (MousePos.X > Position.ToPoint().X)
{
    EntityDisplayRectangle.X = Position.ToPoint().X - (int)EntitySize.X * (int)TileSize.X * _displayResizeFactor / 2;

    EntityDisplayRectangle.Width = (int)EntitySize.X * (int)TileSize.X * _displayResizeFactor;
}else{
    EntityDisplayRectangle.X = Position.ToPoint().X + (int)EntitySize.X * (int)TileSize.X * _displayResizeFactor / 2;

    EntityDisplayRectangle.Width = -((int)EntitySize.X * (int)TileSize.X * _displayResizeFactor);
}
EntityDisplayRectangle.Y = (int)Position.Y - ((int)EntitySize.Y + 1);

here is two screenshots of what i curently have:

enter image description here enter image description here

the red dot is a point drawn by this line

spriteBatch.DrawPoint(new Vector2(getEntityDisplayRectangle().X, getEntityDisplayRectangle().Y), Color.Red, 3);

So, is there an other way to do or is it even possible?

The player texture is from a tileSet and i use a source rectangle to pick the correct image from it

here is the code that draws the player:

spriteBatch.Draw(TileSet, EntityDisplayRectangle, TileSetOffsetRectangle, Color.White);

Up:

here is the rectangle drawn with a draw rectangle

enter image description here enter image description here

Note: the red box is the collision box. it's showwn just for debug purpose

it seems that the rectangle struggle to exist when it's width is negative.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Answer:

Very simple.

i've just created a public bool variable called facing.

in the ancient code i've made i replaced everything by facing=true or =false according to the position of the mouse. true for right and false for left.

then when i draw the sprite i test for this variable and depending on the direction i Apply a different effect.

when it's true the effect is empty ( new SpriteEffects()) but when it's false, i Apply the effect SpriteEffects.FlipHorizontally

this give the Following code (at the draw method of the entity class):

SpriteEffects effect;

if (facing)
{
    effect = new SpriteEffects();
}
else
{
    effect = SpriteEffects.FlipHorizontally;
}

spriteBatch.Draw(TileSet, destinationRectangle: EntityDisplayRectangle, sourceRectangle: TileSetOffsetRectangle, color: Color.White, rotation: 0, origin: new Vector2(0, 0), effects: effect, layerDepth: 0);

so with this method, no need to actually flip the rectangle. but only to flip at drawing.

i've found the answer in this stackex question

I Hope it helped people in the same situation.

P.S. the code to detect mouse pos is now really small:

Point MousePos = Vector2.Transform(Mouse.GetState().Position.ToVector2(), Matrix.Invert(Camera.GetViewMatrix())).ToPoint();
if (MousePos.X > Position.ToPoint().X)
{
    facing = true;
}
else
{
    facing = false;
}
\$\endgroup\$
2
  • \$\begingroup\$ Funny thing, the code could be even shorter by replacing the if-statement with facing = (MousePos.X > Position.ToPoint().X) \$\endgroup\$
    – Steven
    Commented Jun 15, 2020 at 11:07
  • \$\begingroup\$ Ah yes didn’t thought about it, thanks \$\endgroup\$ Commented Jun 15, 2020 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .