C
C#10mo ago
robs0271

Why targetRadius should be "43"?

Hi, I am practicing C# by replicating a simple exercise in Monogame: the code draws a ball, and when I click on the mouse, if the cursor is inside the ball the score value increases; otherwise it does not. To achieve this, the ball position was put in relation with a targetRadius, which is "43", and if I change the value it does not work anymore. I am trying to understand what is happening here, why the radius should be "43" to have the things achieved? The position of the ball and the radius are defined as follows: Vector2 targetPosition; const int targetRadius = 43; Then the ball (target) is drawn by subtracting the radius from the coordinates of its position, why is it doing this? _spriteBatch.Draw(target, new Vector2(targetPosition.X - targetRadius, targetPosition.Y - targetRadius), Color.Aquamarine); It works, but I don't understand why..
10 Replies
i like chatgpt
i like chatgpt10mo ago
It is 2D or 3D? Do you have screenshot or something else to illustrate the ball, etc? It looks like 2D, because no Z in your code but you called it a ball.
robs0271
robs0271OP10mo ago
It's 2D
No description
robs0271
robs0271OP10mo ago
yeah, you are right, actually it is a circle, not a ball
i like chatgpt
i like chatgpt10mo ago
Could you send relevant code?
robs0271
robs0271OP10mo ago
public class Game1 : Game { Texture2D target; Vector2 targetPosition; MouseState mouseState; bool mouseReleased = true; int score = 0; const int targetRadius = 43; private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; public Game1() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); target = Content.Load<Texture2D>("target"); } protected override void Update(GameTime gameTime) { mouseState = Mouse.GetState(); Vector2 mousePosition = mouseState.Position.ToVector2(); float mouseTargetDist = Vector2.Distance(targetPosition, mousePosition); if (mouseTargetDist < targetRadius && mouseState.LeftButton == ButtonState.Pressed && mouseReleased == true) { score++; mouseReleased = false; } if (mouseState.LeftButton == ButtonState.Released) { mouseReleased = true; } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { Vector2 targetPosition = new Vector2(100, 100); _spriteBatch.Draw(target, new Vector2(targetPosition.X - targetRadius, targetPosition.Y - targetRadius), Color.Aquamarine); _spriteBatch.End(); base.Draw(gameTime); } } }
Kouhai
Kouhai10mo ago
What doesn't work anymore? The origin in monogame is top left, not bottom left
i like chatgpt
i like chatgpt10mo ago
Based on my investigation, I think the dimension of target of type Texture2D is about 86 x 86 unit. The inscribed circle in target has a diameter of 86 unit or its radius is 86 / 2 = 43. If we want to make the center of target placed at (100, 100) then its top left corner must be placed at (100 - 43, 100 - 43). So your question:
Then the ball (target) is drawn by subtracting the radius from the coordinates of its position, why is it doing this?
Because _spriteBatch of type SpriteBatch must place the top left corner of target of type Texture2D at (100-43,100-43) to make the center of the target placed at (100, 100).
No description
Kouhai
Kouhai10mo ago
I'm not sure what that image is supposed to mean :thonk: Why is the texture's center at 100,100 The circle is drawn using a texture, it's center would depend on how large the texture is It could be 200x200 or 500x500 The overload for Draw they used doesn't specify how large that drawn texture should be
i like chatgpt
i like chatgpt10mo ago
I guess target's size is about 86 x 86 .
No description
i like chatgpt
i like chatgpt10mo ago
radius = 86 / 2 = 43 does make sense.
Want results from more Discord servers?
Add your server