Add collision detection with octree
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -21,12 +21,6 @@ namespace EvilSatellites.Objects
|
||||
set => _Dimensions = value;
|
||||
}
|
||||
public Vector3 Movement;
|
||||
// public Vector3 Movement
|
||||
// {
|
||||
// get => _Movement;
|
||||
// set => _Movement = value;
|
||||
// }
|
||||
|
||||
private Vector3 _Orientation;
|
||||
public Vector3 Orientation
|
||||
{
|
||||
@@ -42,7 +36,7 @@ namespace EvilSatellites.Objects
|
||||
}
|
||||
public IScreen Screen;
|
||||
public ObjectType Type { get; set; }
|
||||
|
||||
public bool IsDynamic { get => true; }
|
||||
public AbstractGameObject(IScreen screen)
|
||||
{
|
||||
Screen = screen;
|
||||
@@ -90,5 +84,9 @@ namespace EvilSatellites.Objects
|
||||
public abstract void Activate();
|
||||
|
||||
public abstract void Destroy();
|
||||
public virtual void OnCollision(IGameObject obj)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,20 +5,20 @@ using Microsoft.Xna.Framework;
|
||||
|
||||
namespace EvilSatellites.Objects
|
||||
{
|
||||
public interface IGameObject
|
||||
public interface IGameObject : EvilSatellites.Quadtree.Positional
|
||||
{
|
||||
void Activate();
|
||||
void Destroy();
|
||||
Vector3 Position { get; set; }
|
||||
Vector3 Dimensions { get; set; }
|
||||
Vector3 Orientation { get; set; }
|
||||
// Vector3 Movement { get; set; }
|
||||
bool IsDynamic { get; }
|
||||
float Theta { get; set; }
|
||||
Matrix Rotation { get; set; }
|
||||
void Update(GameTime gameTime);
|
||||
ObjectType Type { get; set; }
|
||||
void Move(Vector3 pose);
|
||||
|
||||
void OnCollision(IGameObject obj);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,12 +75,11 @@ namespace EvilSatellites.Objects
|
||||
}
|
||||
}
|
||||
|
||||
public void Accelerate()
|
||||
public void Accelerate(Vector3 direction)
|
||||
{
|
||||
Vector3 acc = new Vector3(0f, 0f, -0.035f);
|
||||
if (Math.Abs(Movement.X) < MaxSpeed.X && Math.Abs(Movement.Y) < MaxSpeed.Y && Math.Abs(Movement.Z) < MaxSpeed.Z)
|
||||
{
|
||||
Movement += acc;
|
||||
Movement += direction;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace EvilSatellites.Objects
|
||||
Sound = Screen.Manager.Sound.Load3D(@"sfx/ui/chat.wav");
|
||||
Sound.Loop = true;
|
||||
Position = pos;
|
||||
Dimensions = new Vector3(5, 5, 5);
|
||||
Sound.Position = new System.Numerics.Vector3(pos.X, pos.Y, pos.Z);
|
||||
// Sound.ConnectToEffect(Screen.Manager.EnvironmentEffect);
|
||||
Activate();
|
||||
@@ -32,5 +33,10 @@ namespace EvilSatellites.Objects
|
||||
{
|
||||
Sound.Position = new System.Numerics.Vector3(Position.X, Position.Y, Position.Z);
|
||||
}
|
||||
|
||||
public override void OnCollision(IGameObject obj)
|
||||
{
|
||||
Sound.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
using System.Numerics;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace EvilSatellites.Quadtree
|
||||
{
|
||||
public interface Positional
|
||||
{
|
||||
Vector3 Position { get; }
|
||||
Vector3 Dimensions { get; }
|
||||
Vector3 Position { get; set; }
|
||||
Vector3 Dimensions { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace EvilSatellites.Quadtree
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace EvilSatellites.Quadtree
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@ using SynthySound;
|
||||
using System.Linq;
|
||||
using EvilSatellites.Objects;
|
||||
using DavyKager;
|
||||
using EvilSatellites.Quadtree;
|
||||
|
||||
namespace EvilSatellites.Screens
|
||||
{
|
||||
@@ -15,6 +16,7 @@ namespace EvilSatellites.Screens
|
||||
public ScreenManager Manager { get; set; }
|
||||
public ScreenState State { get; set; }
|
||||
public List<IGameObject> Objects = new List<IGameObject>();
|
||||
private Quadtree<IGameObject> objtree = new Quadtree<IGameObject>(new Vector3(10000, 10000, 10000), 10, 20);
|
||||
private float Theta = 0;
|
||||
private Player _player;
|
||||
private ISoundEffect radarNoise;
|
||||
@@ -40,8 +42,10 @@ namespace EvilSatellites.Screens
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
objtree.Clear();
|
||||
foreach (var obj in Objects)
|
||||
{
|
||||
objtree.Add(obj);
|
||||
obj.Update(gameTime);
|
||||
if (obj.Type == ObjectType.Player)
|
||||
{
|
||||
@@ -56,14 +60,28 @@ namespace EvilSatellites.Screens
|
||||
Vector3 radarTarget = (Objects[1].Position - _player.Position);
|
||||
radarTarget.Normalize();
|
||||
radarNoise.Pitch = MathHelper.Clamp(0.1f / Vector3.Distance(_player.Rotation.Forward, radarTarget)*3f, 0, 1);
|
||||
CheckCollisions();
|
||||
return;
|
||||
}
|
||||
|
||||
public void HandleInput(GameTime gameTime, KeyboardState inputs)
|
||||
{
|
||||
Vector3 move = new Vector3(0, 0, 0);
|
||||
if (inputs.IsKeyDown(Keys.W))
|
||||
{
|
||||
_player.Accelerate();
|
||||
move.Z = -0.035f;
|
||||
}
|
||||
if (inputs.IsKeyDown(Keys.S))
|
||||
{
|
||||
move.Z = 0.035f;
|
||||
}
|
||||
if (inputs.IsKeyDown(Keys.A))
|
||||
{
|
||||
move.X = -0.035f;
|
||||
}
|
||||
if (inputs.IsKeyDown(Keys.D))
|
||||
{
|
||||
move.X = 0.035f;
|
||||
}
|
||||
if (inputs.IsKeyDown(Keys.Left))
|
||||
{
|
||||
@@ -81,6 +99,7 @@ namespace EvilSatellites.Screens
|
||||
{
|
||||
_player.RotateX(-0.05f);
|
||||
}
|
||||
_player.Accelerate(move);
|
||||
if (inputs.IsKeyDown(Keys.V))
|
||||
{
|
||||
Tolk.Speak(_player.Movement.Length().ToString(), true);
|
||||
@@ -95,5 +114,26 @@ namespace EvilSatellites.Screens
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckCollisions()
|
||||
{
|
||||
foreach (var obj in Objects)
|
||||
{
|
||||
var candidates = objtree.Find(obj.Position, obj.Dimensions);
|
||||
foreach (var target in candidates)
|
||||
{
|
||||
if (obj == target) continue;
|
||||
if (Intersects(obj, target))
|
||||
{
|
||||
obj.OnCollision(target);
|
||||
target.OnCollision(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool Intersects(IGameObject obj1, IGameObject obj2)
|
||||
{
|
||||
return (obj1.Position.X <= (obj2.Position.X + obj2.Dimensions.X) && (obj1.Position.X + obj1.Dimensions.X) >= obj2.Position.X) && (obj1.Position.Y <= (obj2.Position.Y + obj2.Dimensions.Y) && (obj1.Position.Y + obj1.Dimensions.Y) >= obj2.Position.Y) && (obj1.Position.Z <= (obj2.Position.Z + obj2.Dimensions.Z) && (obj1.Position.Z + obj1.Dimensions.Z) >= obj2.Position.Z);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:79fc765256e9b99f1b34de78fcbc38b6685caecf1b3b948896c04a8285eebe86
|
||||
size 2876306
|
||||
oid sha256:182dff19352c93c533e164ab8d1aca1579022ac9ae26be41e2ffa1033c8d73ec
|
||||
size 1429960
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:79fc765256e9b99f1b34de78fcbc38b6685caecf1b3b948896c04a8285eebe86
|
||||
size 2876306
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:0adb40d387297f6ecf47972d1409eaa1947200a14cff30bd8639d5ac7fdc4bd8
|
||||
size 1289018
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:7417dd254621e65db44f18be439dd5353e726accbc429b95d4d858f23420bc3c
|
||||
size 1313078
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:5189949bd3c368a8e5db29d69fc2f6ed0f71403c24e1a5bb4c851a02cccb6c15
|
||||
size 1536272
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:baf4ad57a88674a27eedd1d03e46c158673f2046a0f2fbd33a2b3f9c63012af2
|
||||
size 715046
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:2cea116eda9180d3b354317d926a03f89889fbec72f3a00790401c9d0ba1a700
|
||||
size 17684
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:c21046932ad1dc328d2cbaead959a92e6520b6eb2b7cfe9e58a6bb4dff6a36fa
|
||||
size 15920
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:5a0e062260906fb32689c692848f9c176f9da5ce0868fdbe82b81baab0eaac86
|
||||
size 15920
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:bb94beafa9649350dacba9f15d70ec346e7ebe8a48136a9ecd203541c4a4a9c1
|
||||
size 18260
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:ddb2c37d0e6e3b4253afcf1d45a07e22fbf32d80ec62510b6c0f36eb54a7e5b9
|
||||
size 332210
|
||||
Reference in New Issue
Block a user