Skip to content

Commit

Permalink
Fix some quality issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rejurime committed Oct 1, 2024
1 parent 509f94f commit ea3a545
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 61 deletions.
24 changes: 15 additions & 9 deletions TGC.MonoGame.Samples/Samples/Collisions/CameraCollision.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TGC.MonoGame.Samples.Cameras;
using TGC.MonoGame.Samples.Collisions;
using TGC.MonoGame.Samples.Geometries.Textures;
Expand Down Expand Up @@ -120,7 +117,9 @@ protected override void LoadContent()

// Enable default lighting for the Robot
foreach (var mesh in _robot.Meshes)
{
((BasicEffect)mesh.Effects.FirstOrDefault())?.EnableDefaultLighting();
}

// Create the Quad
_quad = new QuadPrimitive(GraphicsDevice);
Expand Down Expand Up @@ -194,7 +193,9 @@ private void UpdateCamera()
}
// If the Camera didn't collide with the scene
else
{
_camera.Position = newCameraPosition;
}

// Set our Target as the Robot, the Camera needs to be always pointing to it
_camera.TargetPosition = _robotPosition;
Expand All @@ -219,14 +220,16 @@ private void UpdateCamera()
var cameraToPlayerRay = new Ray(cameraPosition, normalizedDifference);

// Test our ray against every wall Bounding Box
for (var index = 0; index < _wallBoxes.Length; index++)
foreach (var t in _wallBoxes)
{
// If there was an intersection
// And this intersection happened between the Robot and the Camera (and not further away)
// Return the distance of collision
var distance = cameraToPlayerRay.Intersects(_wallBoxes[index]);
var distance = cameraToPlayerRay.Intersects(t);
if (distance.HasValue && distance < distanceToRobot)
{
return distance;
}
}
// Return null if there was no valid collision
return null;
Expand Down Expand Up @@ -277,17 +280,21 @@ public override void Update(GameTime gameTime)
_robotCylinder.Center = newPosition;

// Test against every wall. If there was a collision, move our Cylinder back to its original position
for (int index = 0; index < _wallBoxes.Length; index++)
if (!_robotCylinder.Intersects(_wallBoxes[index]).Equals(BoxCylinderIntersection.None))
foreach (var t in _wallBoxes)
{
if (!_robotCylinder.Intersects(t).Equals(BoxCylinderIntersection.None))
{
moved = false;
_robotCylinder.Center = _robotPosition;
break;
}
}

// If there was no collision, update our Robot Position value
if (moved)
{
_robotPosition = newPosition;
}
}

// If we effectively moved (with no collision) or rotated
Expand Down Expand Up @@ -348,9 +355,8 @@ public override void Draw(GameTime gameTime)

// Draw Gizmos for Bounding Boxes and Robot Cylinder

for (int index = 0; index < _wallBoxes.Length; index++)
foreach (var box in _wallBoxes)
{
var box = _wallBoxes[index];
var center = BoundingVolumesExtensions.GetCenter(box);
var extents = BoundingVolumesExtensions.GetExtents(box);
Game.Gizmos.DrawCube(center, extents * 2f, Color.Red);
Expand Down
38 changes: 34 additions & 4 deletions TGC.MonoGame.Samples/Samples/Collisions/ThirdPersonPlatformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public override void Initialize()
_camera = new TargetCamera(GraphicsDevice.Viewport.AspectRatio, Vector3.One * 100f, Vector3.Zero);

// Create World matrices for our stairs
_stairsWorld = new Matrix[]
_stairsWorld = new[]
{
Matrix.CreateScale(70f, 6f, 15f) * Matrix.CreateTranslation(0f, 3f, 125f),
Matrix.CreateScale(70f, 6f, 15f) * Matrix.CreateTranslation(0f, 9f, 140f),
Expand All @@ -122,7 +122,9 @@ public override void Initialize()
// Instantiate Bounding Boxes for the stairs
int index = 0;
for (; index < _stairsWorld.Length; index++)
{
_colliders[index] = BoundingVolumesExtensions.FromMatrix(_stairsWorld[index]);
}

// Instantiate a BoundingBox for the Box
_colliders[index] = BoundingVolumesExtensions.FromMatrix(_boxWorld);
Expand All @@ -147,8 +149,10 @@ protected override void LoadContent()

// Enable default lighting for the Robot
foreach (var mesh in _robot.Meshes)
{
((BasicEffect)mesh.Effects.FirstOrDefault())?.EnableDefaultLighting();

}

// Create a BasicEffect to draw the Box
_boxesEffect = new BasicEffect(GraphicsDevice);
_boxesEffect.TextureEnabled = true;
Expand Down Expand Up @@ -240,13 +244,19 @@ public override void Update(GameTime gameTime)

// Check for the Jump key press, and add velocity in Y only if the Robot is on the ground
if (Game.CurrentKeyboardState.IsKeyDown(Keys.Space) && _onGround)
{
_robotVelocity += Vector3.Up * RobotJumpSpeed;
}

// Check for key presses and add a velocity in the Robot's Front Direction
if (Game.CurrentKeyboardState.IsKeyDown(Keys.Up))
{
_robotVelocity += _robotFrontDirection * RobotSideSpeed;
}
else if (Game.CurrentKeyboardState.IsKeyDown(Keys.Down))
{
_robotVelocity -= _robotFrontDirection * RobotSideSpeed;
}

// Add the Acceleration to our Velocity
// Multiply by the deltaTime to have the Position affected by deltaTime * deltaTime
Expand Down Expand Up @@ -291,7 +301,9 @@ private void SolveVerticalMovement(Vector3 scaledVelocity)
{
// If the Robot has vertical velocity
if (scaledVelocity.Y == 0f)
{
return;
}

// Start by moving the Cylinder
_robotCylinder.Center += Vector3.Up * scaledVelocity.Y;
Expand All @@ -304,8 +316,10 @@ private void SolveVerticalMovement(Vector3 scaledVelocity)
for (var index = 0; index < _colliders.Length; index++)
{
if (!_robotCylinder.Intersects(_colliders[index]).Equals(BoxCylinderIntersection.Intersecting))
{
continue;

}

// If we collided with something, set our velocity in Y to zero to reset acceleration
_robotVelocity = new Vector3(_robotVelocity.X, 0f, _robotVelocity.Z);

Expand Down Expand Up @@ -336,7 +350,9 @@ private void SolveVerticalMovement(Vector3 scaledVelocity)

// If we are on bottom of the collider, push down
else
{
penetration = -cylinderY - _robotCylinder.HalfHeight + colliderY - extents.Y;
}

// Move our Cylinder so we are not colliding anymore
_robotCylinder.Center += Vector3.Up * penetration;
Expand All @@ -346,7 +362,9 @@ private void SolveVerticalMovement(Vector3 scaledVelocity)
for (var index = 0; index < _colliders.Length; index++)
{
if (!_robotCylinder.Intersects(_colliders[index]).Equals(BoxCylinderIntersection.Intersecting))
{
continue;
}

// Iterate until we don't collide with anything anymore
collided = true;
Expand All @@ -364,16 +382,20 @@ private void SolveHorizontalMovementSliding(Vector3 scaledVelocity)
{
// Has horizontal movement?
if (Vector3.Dot(scaledVelocity, new Vector3(1f, 0f, 1f)) == 0f)
{
return;

}

// Start by moving the Cylinder horizontally
_robotCylinder.Center += new Vector3(scaledVelocity.X, 0f, scaledVelocity.Z);

// Check intersection for every collider
for (var index = 0; index < _colliders.Length; index++)
{
if (!_robotCylinder.Intersects(_colliders[index]).Equals(BoxCylinderIntersection.Intersecting))
{
continue;
}

// Get the intersected collider and its center
var collider = _colliders[index];
Expand All @@ -386,7 +408,9 @@ private void SolveHorizontalMovementSliding(Vector3 scaledVelocity)
// If the Robot collided with a step and climbed it, stop here
// Else go on
if (stepClimbed)
{
return;
}

// Get the cylinder center at the same Y-level as the box
var sameLevelCenter = _robotCylinder.Center;
Expand Down Expand Up @@ -426,27 +450,33 @@ private bool SolveStepCollision(BoundingBox collider, int colliderIndex)
// Is this collider a step?
// If not, exit
if (extents.Y >= 6f)
{
return false;
}

// Is the base of the cylinder close to the step top?
// If not, exit
var distanceToTop = MathF.Abs((_robotCylinder.Center.Y - _robotCylinder.HalfHeight) - (colliderCenter.Y + extents.Y));
if (distanceToTop >= 12f)
{
return false;
}

// We want to climb the step
// It is climbable if we can reposition our cylinder in a way that
// it doesn't collide with anything else
var pastPosition = _robotCylinder.Center;
_robotCylinder.Center += Vector3.Up * distanceToTop;
for (int index = 0; index < _colliders.Length; index++)
{
if (index != colliderIndex && _robotCylinder.Intersects(_colliders[index]).Equals(BoxCylinderIntersection.Intersecting))
{
// We found a case in which the cylinder
// intersects with other colliders, so the climb is not possible
_robotCylinder.Center = pastPosition;
return false;
}
}

// If we got here the climb was possible
// (And the Robot position was already updated)
Expand Down
2 changes: 1 addition & 1 deletion TGC.MonoGame.Samples/Samples/PBR/BasicPBR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace TGC.MonoGame.Samples.Samples.PBR
{
public class BasicPBR : TGCSample
public class BasicPBR : TGCSample
{
private Texture2D _albedo;
private Texture2D _ao;
Expand Down
19 changes: 15 additions & 4 deletions TGC.MonoGame.Samples/Samples/PostProcessing/EnvironmentMap.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TGC.MonoGame.Samples.Cameras;
using TGC.MonoGame.Samples.Geometries;
using TGC.MonoGame.Samples.Viewer;
using TGC.MonoGame.Samples.Viewer.GUI.Modifiers;

namespace TGC.MonoGame.Samples.Samples.PostProcessing
{
Expand Down Expand Up @@ -75,8 +73,12 @@ protected override void LoadContent()

// Assign the Environment map effect to our robot
foreach (var modelMesh in _robot.Meshes)
foreach (var part in modelMesh.MeshParts)
part.Effect = _effect;
{
foreach (var part in modelMesh.MeshParts)
{
part.Effect = _effect;
}
}

// Create a render target for the scene
_environmentMapRenderTarget = new RenderTargetCube(GraphicsDevice, EnvironmentMapSize, false,
Expand All @@ -96,8 +98,13 @@ private void OnEffectEnable(bool enabled)
{
var effectToAssign = enabled ? _effect : _basicEffect;
foreach (var modelMesh in _robot.Meshes)
{
foreach (var part in modelMesh.MeshParts)
{
part.Effect = effectToAssign;
}
}

_effectOn = enabled;
}

Expand All @@ -118,9 +125,13 @@ public override void Update(GameTime gameTime)
public override void Draw(GameTime gameTime)
{
if (_effectOn)
{
DrawEnvironmentMap();
}
else
{
DrawRegular();
}

GraphicsDevice.DepthStencilState = DepthStencilState.None;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
namespace TGC.MonoGame.Samples.Samples.RenderPipeline
{
/// <summary>
/// Shows why back-to-front sorting needs to be performed when drawing alpha blended geometry
/// Shows why back-to-front sorting needs to be performed when drawing alpha blended geometry.
/// </summary>
public class AlphaBlendingSorting : TGCSample
{
private const string WorldViewProjection = "WorldViewProjection";

/// <summary>
/// A camera to draw geometry
/// </summary>
Expand Down Expand Up @@ -85,7 +87,7 @@ public AlphaBlendingSorting(TGCViewer game) : base(game)
public override void Initialize()
{
var baseScaleRotation = Matrix.CreateScale(10f) * Matrix.CreateRotationX(MathF.PI / 2f);
_backToFrontQuadWorlds = new List<Matrix>()
_backToFrontQuadWorlds = new List<Matrix>
{
baseScaleRotation * Matrix.CreateTranslation(-30f, 5f, 0f),
baseScaleRotation * Matrix.CreateTranslation(-30f, 5f, 10f),
Expand Down Expand Up @@ -130,7 +132,7 @@ public override void Initialize()

_quad = new QuadPrimitive(GraphicsDevice);

_camera = new FreeCamera(GraphicsDevice.Viewport.AspectRatio, Vector3.One * 20f);
_camera = new FreeCamera(GraphicsDevice.Viewport.AspectRatio, new Vector3(0,20,110));

_texts = new List<string>()
{
Expand Down Expand Up @@ -184,7 +186,9 @@ public override void Update(GameTime gameTime)

// Update text positions in screen space to face the camera
for (var index = 0; index < _textScreenPositions.Count; index++)
{
UpdateTextPosition(index);
}

base.Update(gameTime);
}
Expand Down Expand Up @@ -214,7 +218,7 @@ public override void Draw(GameTime gameTime)
GraphicsDevice.BlendState = BlendState.Opaque;

// Draw the floor
_tilingFloorEffect.Parameters["WorldViewProjection"].SetValue(Matrix.CreateScale(100f) * viewProjection);
_tilingFloorEffect.Parameters[WorldViewProjection].SetValue(Matrix.CreateScale(100f) * viewProjection);
_quad.Draw(_tilingFloorEffect);

// Set the blend state as alpha blend,
Expand All @@ -230,7 +234,7 @@ public override void Draw(GameTime gameTime)
for (var index = 0; index < _backToFrontQuadWorlds.Count; index++)
{
_alphaBlendingEffect.Parameters["Tint"].SetValue(_quadColors[index].ToVector3());
_alphaBlendingEffect.Parameters["WorldViewProjection"].SetValue(_backToFrontQuadWorlds[index] * viewProjection);
_alphaBlendingEffect.Parameters[WorldViewProjection].SetValue(_backToFrontQuadWorlds[index] * viewProjection);
_quad.Draw(_alphaBlendingEffect);
}

Expand All @@ -239,7 +243,7 @@ public override void Draw(GameTime gameTime)
{
var colorIndex = _frontToBackQuadWorlds.Count - index - 1;
_alphaBlendingEffect.Parameters["Tint"].SetValue(_quadColors[colorIndex].ToVector3());
_alphaBlendingEffect.Parameters["WorldViewProjection"].SetValue(_frontToBackQuadWorlds[index] * viewProjection);
_alphaBlendingEffect.Parameters[WorldViewProjection].SetValue(_frontToBackQuadWorlds[index] * viewProjection);
_quad.Draw(_alphaBlendingEffect);
}

Expand All @@ -249,7 +253,7 @@ public override void Draw(GameTime gameTime)
for (var index = 0; index < _depthNoneQuadWorlds.Count; index++)
{
_alphaBlendingEffect.Parameters["Tint"].SetValue(_quadColors[index].ToVector3());
_alphaBlendingEffect.Parameters["WorldViewProjection"].SetValue(_depthNoneQuadWorlds[index] * viewProjection);
_alphaBlendingEffect.Parameters[WorldViewProjection].SetValue(_depthNoneQuadWorlds[index] * viewProjection);
_quad.Draw(_alphaBlendingEffect);
}

Expand All @@ -262,7 +266,9 @@ public override void Draw(GameTime gameTime)
RasterizerState.CullNone);

for (var index = 0; index < _textScreenPositions.Count; index++)
{
Game.SpriteBatch.DrawString(_spriteFont, _texts[index], _textScreenPositions[index], Color.White);
}

Game.SpriteBatch.End();

Expand Down
Loading

0 comments on commit ea3a545

Please sign in to comment.