Hello,
So I’m making a game similar to Stardew Valley, I’ve implemented a camera that moves all the surroundings while the player remains fixed. I’m trying to make it so that the player can select objects around them but the further away I move from 0,0 Camera position the more inaccurate the select cursor becomes more off from the player position:
The code for the camera position and hit location:
Vector2 camera_pos = Vector2.Transform(Game1.camera_pos, Matrix.Invert(Matrix.CreateScale(Game1.scale_x, Game1.scale_y, 1.0f)));
if (Keyboard.GetState().IsKeyDown(Keys.D))
{
direction = ‘D’;
hitlocation = new Vector2((player_rect.X / 16) * 16 + 16, ((player_rect.Y / 16) * 16) + 16);
hitlocation.Y -= ((int)(camera_pos.Y / 16) * 16);
hitlocation.X -= ((int)(camera_pos.X / 16) * 16);
if (Collision.predict_collision_map(player_collision_rect, 2, 0) == false)
{
Game1.camera_pos.X -= player_walking_speed;
}
}
Fixed Matrix:
scale_x = (float)(graphics.PreferredBackBufferWidth1.25) / 800;
scale_y = (float)(graphics.PreferredBackBufferHeight1.25) / 480;
fixed_matrix = Matrix.CreateScale(scale_x, scale_y, 1.0f);
Drawing the player:
spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: fixed_matrix);
player1.tick();
spriteBatch.End();
Drawing the hit location(green square):
matrix = Matrix.CreateScale(scale_x, scale_y, 1.0f) * Matrix.CreateTranslation(newVector3(camera_pos.X, camera_pos.Y, 0));
spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: matrix);
player1.tick_hit();
spriteBatch.End();
Original Position (0,0):
As I move to the right: