# Making mouse pos realtive to a moving object

I'm coding my first actual game, trying without following tutorials, just trying to use my knowledge so far to make something playable. I'm creating (attempting) a simple side-scrolling shooter. I have a reticle drawn on screen that follows the mouse position, relative to where the player is. I.e. my update method for the reticle

``````mouse = Mouse.GetState();

reticlePostion.X = mouse.X + (player.position.X + (player.texture.Width /2));
reticlePostion.Y = mouse.Y + (player.position.Y + (player.texture.Height /2));
``````

The only problem is that the windows cursor, ends up being about 300-400 pixels away from where the actual reticle is. What I'm trying to accomplish is a reticle that moves with the player (when the player jumps the reticle moves up with him in relative position), but still have it follow the mouse. I hope this makes sense, just lemme know if I need to post more.

-
The casual solution is to hide the system mouse pointer during active gameplay. – Patrick Hughes Mar 3 '12 at 1:02

Is this for providing an aiming guide for your player? If that's the case, then I suppose you want to draw the reticle so that it remains at a fixed distance around the player, but always facing in the mouse's direction. Here's how I would do it, based on what you currently have:

``````// Distance to draw the reticle from the player
float distance = 100f;

// Calculate mouse and player position
Vector2 mousePosition = new Vector2(mouse.X, mouse.Y);
Vector2 playerCenter = player.position + new Vector2(player.texture.Width/2f, player.texture.Height/2f);

// Find direction from player to mouse
Vector2 direction = Vector2.Normalize(mousePosition - playerCenter);

// Calculate final reticle position
reticlePosition = playerCenter  + direction * distance;
``````

Then just tweak the `distance` variable to your liking. And of course you can inline all of those calculations in a single line of code if you wanted. I just presented the steps separatedly so I could comment each of them, and for clarity.

If this is not what you meant, then I don't see how you can have an object that is following both the player and the mouse at the same time, unless it's always drawn half way between them or something similar. Or perhaps the reticle's X coordinate follows the mouse, while the reticle's Y coordinate is fixed relative to the player? That's another possibility.

-
Ah ok. I tried it with the fixed position like you said, worked perfectly. But it kinda looked a little funny in the context of what I was doing. But, basically what I just ended up doing was having the reticle move along with the camera, so it always stays in the same screen space. It doesn't follow the player anymore, but at least it doesn't end looking funny when the player moves around. Thanks for the help :) – bengarrr Mar 4 '12 at 3:17
Edit: Actually it turned out to be a camera problem. Since the camera was already pointing to the center of the player, when I drew the reticle to the center of the player position it was adding it twice. So that's the reason the windows cursor ended up being like 200-300 pixels off. So I just had to add the center of the camera's postion a voila! it works. – bengarrr Mar 4 '12 at 3:48

Off the top of my head -

calculate the vector between the character and the mouse

normalize and multiply the vector to however far you want the reticle and use that point to draw it.

``````reticlePosition = Vector2.Normalize((mouse.x - player.x, mouse.y - player.y)) * 100
``````

Or similar

You could also provide a subtle mouse cursor so the player can see where they've left the mouse.

-