You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is easy to confuse the direction of the inequality sign. As a quick mental test, think of how the math works when the origin of two circles are at the same position, i.e., when the *squared distance* is zero.
60
60
61
-
To calculate the squared distance between two points, MonoGame provides the [**Vector2.DistanceSquared**](<xref:Microsoft.Xna.Framework.Vector2.DistanceSquared(Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2)>) method:
61
+
To calculate the squared distance between two points, MonoGame provides the [**Vector2.DistanceSquared**](xref:Microsoft.Xna.Framework.Vector2.DistanceSquared(Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2)) method:
62
62
63
63
[!code-csharp[](./snippets/vector2_distance.cs)]
64
64
65
65
> [!TIP]
66
-
> MonoGame also provides a distance calculation method with [**Vector2.Distance**](<xref:Microsoft.Xna.Framework.Vector2.Distance(Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2)>) which returns the distance by providing the square root of the distance squared. So why not use this instead?
66
+
> MonoGame also provides a distance calculation method with [**Vector2.Distance**](xref:Microsoft.Xna.Framework.Vector2.Distance(Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2)) which returns the distance by providing the square root of the distance squared. So why not use this instead?
67
67
>
68
68
> Square root operations are more computationally complex for a CPU. So instead of getting the normal distance, which would require the square root operation, it is more efficient for the cpu to multiply the sum of the radii by itself to get the squared sum and use that for comparison instead.
69
69
@@ -93,7 +93,7 @@ To determine if two rectangles overlap using AABB collision detection, there are
93
93
94
94
If even a single one of these conditions is false, then the rectangles are not overlapping and thus not colliding.
95
95
96
-
MonoGame provides the [**Rectangle.Intersects**](<xref:Microsoft.Xna.Framework.Rectangle.Intersects(Microsoft.Xna.Framework.Rectangle)>) method which will perform an AABB collision check for us:
96
+
MonoGame provides the [**Rectangle.Intersects**](xref:Microsoft.Xna.Framework.Rectangle.Intersects(Microsoft.Xna.Framework.Rectangle)) method which will perform an AABB collision check for us:
Sometimes, instead of preventing an object from moving onto another object, we want to ensure an object remains contained within a certain bounding area. MonoGame also provides the [**Rectangle.Contains**](<xref:Microsoft.Xna.Framework.Rectangle.Contains(Microsoft.Xna.Framework.Rectangle)>) method that we can use to determine this. [**Rectangle.Contains**](<xref:Microsoft.Xna.Framework.Rectangle.Contains(Microsoft.Xna.Framework.Rectangle)>) can check if any of the following are completely contained within the bounds of the rectangle;
147
+
Sometimes, instead of preventing an object from moving onto another object, we want to ensure an object remains contained within a certain bounding area. MonoGame also provides the [**Rectangle.Contains**](xref:Microsoft.Xna.Framework.Rectangle.Contains(Microsoft.Xna.Framework.Rectangle)) method that we can use to determine this. [**Rectangle.Contains**](xref:Microsoft.Xna.Framework.Rectangle.Contains(Microsoft.Xna.Framework.Rectangle)) can check if any of the following are completely contained within the bounds of the rectangle;
For games that need objects to bounce off each other (like the ball in a Pong game), we need to calculate how their velocity should change after the collision. MonoGame provides the [**Vector2.Reflect**](<xref:Microsoft.Xna.Framework.Vector2.Reflect(Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2)>) method to handle this calculation for us. The method needs two pieces of information:
177
+
For games that need objects to bounce off each other (like the ball in a Pong game), we need to calculate how their velocity should change after the collision. MonoGame provides the [**Vector2.Reflect**](xref:Microsoft.Xna.Framework.Vector2.Reflect(Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2)) method to handle this calculation for us. The method needs two pieces of information:
178
178
179
179
1. The incoming vector (the direction the object is moving in before the collision).
180
180
2. The normal vector (the direction perpendicular to the surface).
@@ -318,15 +318,15 @@ The key changes made here are:
318
318
2. The field `_batVelocity` was added to track the velocity of the bat.
319
319
3. The `AssignRandomBatVelocity()` method was added which calculates a random x and y velocity for the bat to move at when called.
320
320
4. In [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize), the initial position of the bat is set and `AssignRandomVelocity` is called to assign the initial velocity for the bat.
321
-
5. In [**Update**](<xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)>), collision detection and response logic was added to perform the following in order:
321
+
5. In [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), collision detection and response logic was added to perform the following in order:
322
322
1. A [**Rectangle**](xref:Microsoft.Xna.Framework.Rectangle) bound is created to represent the bounds of the screen.
323
323
2. A `Circle` bound is created to represent the bounds of the slime.
324
324
3. Distance based checks are performed to ensure that the slime cannot move outside of the screen, the resolution of which is to perform a blocking response.
325
325
4. A new position for the bat is calculated based on the current velocity of the bat.
326
326
5. A `Circle` bound is created to represent the bounds of the bat.
327
327
6. Distance based checks are performed to ensure the bat cannot move outside of the screen, the resolution of which is to perform a bounce response.
328
328
7. A collision check is made to determine if the slime and bat are colliding (bat "eating" the slime). If so, the bat is assigned a new random position within the screen and assigned a new random velocity.
329
-
6. In [**Draw**](<xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)>), the bat is now drawn using the `_batPosition` value.
329
+
6. In [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)), the bat is now drawn using the `_batPosition` value.
330
330
331
331
Running the game now
332
332
@@ -389,10 +389,10 @@ In the next chapter, we will explore using tilesets and tilemaps to create tile
389
389
Two circles are colliding if the distance between their centers is less than the sum of their radii. If the distance is greater, they are separate. If the distance equals the sum of radii, they are just touching at one point.
390
390
:::
391
391
392
-
4. When implementing bounce collision response, what two pieces of information does [**Vector2.Reflect**](<xref:Microsoft.Xna.Framework.Vector2.Reflect(Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2)>) need?
392
+
4. When implementing bounce collision response, what two pieces of information does [**Vector2.Reflect**](xref:Microsoft.Xna.Framework.Vector2.Reflect(Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2)) need?
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/17_scenes/index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -115,7 +115,7 @@ The key changes here are:
115
115
116
116
## Updating the Game
117
117
118
-
With the scene architecture in place, we’re now ready to update the game by breaking it into separate scenes. We will create two scenes; a title scene and a gameplay scene. First, however, we need to add an additional SpriteFont Description that will be used during the title scene to display the title of the game. Open the *Content.mgcb* content project file in the MGCB Editor and perform the following:
118
+
With the scene architecture in place, we are now ready to update the game by breaking it into separate scenes. We will create two scenes; a title scene and a gameplay scene. First, however, we need to add an additional SpriteFont Description that will be used during the title scene to display the title of the game. Open the *Content.mgcb* content project file in the MGCB Editor and perform the following:
119
119
120
120
1. Right-click the `fonts` folder and choose `Add > New Item...`.
121
121
2. Select `SpriteFont Description (.spritefont)` from the options.
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/22_snake_game_mechanics/index.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -269,8 +269,8 @@ This update method:
269
269
2. Calls `HandleInput` to check for player input.
270
270
3. Increments the movement timer by the amount of time that has elapsed between the game's update cycles.
271
271
4. Performs a check to see if the movement timer has accumulated more time than the threshold to perform a movement cycle update. If it has then:
272
-
- The movement timer is reduced by the threshold time.
273
-
- The `Move` method is called to perform a movement cycle update.
272
+
1. The movement timer is reduced by the threshold time.
273
+
2. The `Move` method is called to perform a movement cycle update.
274
274
5. Finally, the movement progress amount is calculated by dividing the number of seconds accumulated for the movement timer by the number of seconds for the threshold. This gives us a normalized value between 0.0 and 1.0 that we can use for visual interpolation for fluid movement.
0 commit comments