Initial work on new collision engine.

This commit is contained in:
UnknownShadow200 2016-04-27 19:35:45 +10:00
parent 544f80a2f5
commit 6ee9f75e29
3 changed files with 83 additions and 8 deletions

View File

@ -153,6 +153,7 @@
<Compile Include="Entities\Components\InputComponent.cs" /> <Compile Include="Entities\Components\InputComponent.cs" />
<Compile Include="Entities\Components\InterpolatedComponent.cs" /> <Compile Include="Entities\Components\InterpolatedComponent.cs" />
<Compile Include="Entities\Components\CollisionsComponent.cs" /> <Compile Include="Entities\Components\CollisionsComponent.cs" />
<Compile Include="Entities\Components\NewCollisionsComponent.cs" />
<Compile Include="Entities\Components\PhysicsComponent.cs" /> <Compile Include="Entities\Components\PhysicsComponent.cs" />
<Compile Include="Entities\Components\ShadowComponent.cs" /> <Compile Include="Entities\Components\ShadowComponent.cs" />
<Compile Include="Entities\Components\SoundComponent.cs" /> <Compile Include="Entities\Components\SoundComponent.cs" />

View File

@ -53,18 +53,14 @@ namespace ClassicalSharp.Entities {
Vector3 size = entity.CollisionSize; Vector3 size = entity.CollisionSize;
BoundingBox entityBB, entityExtentBB; BoundingBox entityBB, entityExtentBB;
int count = 0; int count = 0;
FindReachableBlocks( ref count, ref size, out entityBB, out entityExtentBB ); FindReachableBlocks( ref count, out entityBB, out entityExtentBB );
CollideWithReachableBlocks( count, ref size, ref entityBB, ref entityExtentBB ); CollideWithReachableBlocks( count, ref size, ref entityBB, ref entityExtentBB );
} }
void FindReachableBlocks( ref int count, ref Vector3 size, void FindReachableBlocks( ref int count, out BoundingBox entityBB,
out BoundingBox entityBB, out BoundingBox entityExtentBB ) { out BoundingBox entityExtentBB ) {
Vector3 vel = entity.Velocity; Vector3 vel = entity.Velocity;
Vector3 pos = entity.Position; entityBB = entity.CollisionBounds;
entityBB = new BoundingBox(
pos.X - size.X / 2, pos.Y, pos.Z - size.Z / 2,
pos.X + size.X / 2, pos.Y + size.Y, pos.Z + size.Z / 2
);
// Exact maximum extent the entity can reach, and the equivalent map coordinates. // Exact maximum extent the entity can reach, and the equivalent map coordinates.
entityExtentBB = new BoundingBox( entityExtentBB = new BoundingBox(

View File

@ -0,0 +1,78 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using OpenTK;
#if false
namespace ClassicalSharp.Entities {
/// <summary> Entity component that performs collision detection. </summary>
public sealed class NewCollisionsComponent {
Game game;
Entity entity;
BlockInfo info;
static BoundingBox bb;
static RayTracer tracerP1Y1 = new RayTracer();
static RayTracer tracerP2Y1 = new RayTracer();
static RayTracer tracerP1Y2 = new RayTracer();
static RayTracer tracerP2Y2 = new RayTracer();
public NewCollisionsComponent( Game game, Entity entity ) {
this.game = game;
this.entity = entity;
info = game.BlockInfo;
}
internal unsafe void MoveAndWallSlide() {
if( entity.Velocity == Vector3.Zero ) return;
int* minY = stackalloc int[4096];
int* maxY = stackalloc int[4096];
int* minX = stackalloc int[4096];
int* maxX = stackalloc int[4096];
for( int i = 0; i < 4096; i++ ) {
minX[i] = int.MaxValue; minY[i] = int.MaxValue;
maxX[i] = int.MinValue; maxY[i] = int.MinValue;
}
bb = entity.CollisionBounds;
int count = 0;
// First raytrace out from the origin to find approximate blocks
CalcRayDirection();
while( true ) {
UpdateCoords( tracerP1Y1, minX, maxX, minY, maxY );
UpdateCoords( tracerP1Y2, minX, maxX, minY, maxY );
UpdateCoords( tracerP2Y1, minX, maxX, minY, maxY );
UpdateCoords( tracerP2Y2, minX, maxX, minY, maxY );
}
// Now precisely which blocks we really intersect with
// .. then perform collision
}
void UpdateCoords( RayTracer tracer, int* minX, int* maxX,
int* minY, int* maxY, ref int count ) {
tracer.Step();
minX[tracer.Z] = Math.Min( tracer.X, minX[tracer.Z] );
minY[tracer.Z] = Math.Min( tracer.Y, minY[tracer.Z] );
maxX[tracer.Z] = Math.Max( tracer.X, maxX[tracer.Z] );
maxY[tracer.Z] = Math.Max( tracer.Y, maxY[tracer.Z] );
count = Math.Max( count, tracer.Z );
}
void CalcRayDirection() {
Vector3 dir = entity.Velocity;
Vector3 P = new Vector3( bb.Min.X, bb.Min.Y, bb.Max.Z );
tracerP1Y1.SetRayData( P, dir );
P.Y = bb.Max.Y;
tracerP1Y1.SetRayData( P, dir );
P = new Vector3( bb.Max.X, bb.Min.Y, bb.Min.Z );
tracerP2Y1.SetRayData( P, dir );
P.Y = bb.Max.Y;
tracerP2Y2.SetRayData( P, dir );
}
}
}
#endif