• Create Account

TheVastBernie

Member Since 06 Sep 2012
Offline Last Active Apr 09 2013 06:08 PM

Collision detection - Reduce speed at steep hills

09 January 2013 - 06:03 PM

Hi, I got triangle based collision detection implemetation working in my game, I now want to add realistic movement behavior. For that I first added gravity (9.81 * gravityNormal each second to my players velocity) and movement (I move him by setting the XZ values of his velocity according to his movement direction).
Gravity works as planned, he accelerates, falls, and hits the ground (though it's a bit slow). The movent works as well, he climbs over smaller bumps and hills.
The problem I have, is that he's able to climb really steep hills and buildings, provided they're at an slight angle. I know this is due to the fact that I reset the velocity at an collision. When the player hits the ground there can't be any Y movement anymore, so it gets set to 0. Then, with e.g. an X velocity of 3 the player is easily able to climb a hill (his sliding plane makes him slide up) beacuse the gravity didn't have time to build up enough vertical movement.

I thought I'm correct with the math behind my 'accurate' physics approach, but the hillclimbing thing got me thinking I might be wrong.

I tried to overcome this by ignoring approaching walls with too steep angles, but this meant that ground debris (usually visualized as simple boxes) was to steep (since player collides with side and the side of a box has a steep angle).

My collision code (without triangle test):

private void ComputeMovement(ref Vector3 position, ref Vector3 velocity, Vector3 radius, int recursive)
{
if (recursive > 8 || velocity == Vector3.Zero)
return;

// Ellipsioid space conversion

float t = 1;
Vector3 collisionPoint = Vector3.Zero;
Vector3 planeNormal = Vector3.Zero;

// Compute trianle collision detection (t = factor at wich collision occurs)
CalculateCollision(position, velocity, radius, out t, out collisionPoint, out planeNormal);

Vector3 newPosition = position + t * velocity;

if (t < 1)
{
// Create sliding plane
Vector3 collisionNormal = Vector3.Normalize(position - collisionPoint);
Vector3 destinationPosition = position + velocity;
Plane slidingPlane = GeometryMath.PlaneFromPointNormal(collisionPoint, collisionNormal);
Vector3 newVelocity = (destinationPosition - slidingPlane.SignedDistance(destinationPosition) * slidingPlane.Normal) - collisionPoint;

// Check for steep hill
float normalDotVelocity = Vector3.Dot(collisionNormal, velocity);
if (Math.Abs(planeNormal.Y) < 0.707f && normalDotVelocity < 0)
newVelocity.Y = 0;

// Compute recursive function
ComputeMovement(ref position, ref velocity, radius, recursive + 1);
return;
}

velocity = velocity * t * radius;
}

There really must be something wrong, can't be that I can only have one or the other.
Would really appreciate any help on this.

DirectX10 setup and weird alpha artefacts

06 September 2012 - 05:59 PM

I have a weird problem. I use DirectX10 with SlimDX and I try to get the general setup right, but everytime there is something wrong. This time I encountered a weird error with alpha blending. I simply draw two intersecting planes with a opacity of 0.75f. When I draw them using 2x multisampling I get this result:
It looks like a tiny grid on top of my model. Strangely enough when I set multisampling to 1 everything seems fine, until I draw the model with 0.5f opacity, which creates the same artefacts. And that goes through each multisample count.
Here is my device setup:
// Create swap chain description
DXGI.SwapChainDescription swapChainDesc = new DXGI.SwapChainDescription()
{
BufferCount = 1,
Usage = DXGI.Usage.RenderTargetOutput,
OutputHandle = Window.Handle,
IsWindowed = true,
ModeDescription = new DXGI.ModeDescription(0, 0, new Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm),
SampleDescription = new DXGI.SampleDescription(1, 0),
Flags = DXGI.SwapChainFlags.AllowModeSwitch,
};

// Get device and swap chain
DX10.Device.CreateWithSwapChain(null, DX10.DriverType.Hardware, DX10.DeviceCreationFlags.None, swapChainDesc, out Device, out SwapChain);

// Get back buffer view
using (DX10.Texture2D texture = DX10.Resource.FromSwapChain<DX10.Texture2D>(SwapChain, 0))
BackBufferView = new DX10.RenderTargetView(Device, texture);

// Get depth stencil view
DX10.Texture2DDescription descTexture2D = new DX10.Texture2DDescription()
{
Width = 1280,
Height = 720,
MipLevels = 1,
ArraySize = 1,
Format = DXGI.Format.D24_UNorm_S8_UInt,
SampleDescription = BackBufferView.Resource.AsSurface().Description.SampleDescription,
Usage = DX10.ResourceUsage.Default,
BindFlags = DX10.BindFlags.DepthStencil,
CpuAccessFlags = DX10.CpuAccessFlags.None,
OptionFlags = DX10.ResourceOptionFlags.None
};
DX10.DepthStencilViewDescription descDepthStencilView = new DX10.DepthStencilViewDescription()
{
ArraySize = 1,
Dimension = DX10.DepthStencilViewDimension.Texture2DMultisampled,
FirstArraySlice = 1,
MipSlice = 1,
Format = descTexture2D.Format,
};
using (DX10.Texture2D texture = new DX10.Texture2D(Device, descTexture2D))
DepthStencilView = new DX10.DepthStencilView(Device, texture, descDepthStencilView);

// Set rasterizer
DX10.RasterizerStateDescription rasteriserDesc = new DX10.RasterizerStateDescription()
{
CullMode = DX10.CullMode.Front,
DepthBias = 0,
DepthBiasClamp = 0,
FillMode = DX10.FillMode.Solid,
IsAntialiasedLineEnabled = false,
IsDepthClipEnabled = true,
IsFrontCounterclockwise = false,
IsMultisampleEnabled = false,
IsScissorEnabled = false,
SlopeScaledDepthBias = 0
};
Device.Rasterizer.State = DX10.RasterizerState.FromDescription(Device, rasteriserDesc);

// Set viewport
var viewport = new DX10.Viewport(0, 0, Window.ClientSize.Width, Window.ClientSize.Height);
Device.Rasterizer.SetViewports(viewport);

// Set blend state
DX10.BlendStateDescription descBlendState = new DX10.BlendStateDescription()
{
IsAlphaToCoverageEnabled = true,
SourceBlend = DX10.BlendOption.SourceAlpha,
DestinationBlend = DX10.BlendOption.InverseSourceAlpha,
SourceAlphaBlend = DX10.BlendOption.Zero,
DestinationAlphaBlend = DX10.BlendOption.Zero,
};
descBlendState.SetBlendEnable(0, true);
Device.OutputMerger.BlendState = DX10.BlendState.FromDescription(Device, descBlendState);

// Set depth stencil state
DX10.DepthStencilStateDescription descDepthStencilState = new DX10.DepthStencilStateDescription()
{
IsDepthEnabled = true,
DepthComparison = DX10.Comparison.LessEqual,
IsStencilEnabled = true,
FrontFace = new DX10.DepthStencilOperationDescription()
{
FailOperation = DX10.StencilOperation.Keep,
DepthFailOperation = DX10.StencilOperation.Increment,
PassOperation = DX10.StencilOperation.Keep,
Comparison = DX10.Comparison.Always,
},
BackFace = new DX10.DepthStencilOperationDescription()
{
FailOperation = DX10.StencilOperation.Keep,
DepthFailOperation = DX10.StencilOperation.Decrement,
PassOperation = DX10.StencilOperation.Keep,
Comparison = DX10.Comparison.Always,
},
};
Device.OutputMerger.DepthStencilState = DX10.DepthStencilState.FromDescription(Device, descDepthStencilState);
Device.OutputMerger.DepthStencilReference = 1;

// Set render targets
Device.OutputMerger.SetTargets(DepthStencilView, BackBufferView);

Strange enough even though I don't use multisampling I must declare the DepthStencilViewDescription.Dimension to be Texture2DMultisampled or this line:
DepthStencilView = new DX10.DepthStencilView(Device, texture, descDepthStencilView);
will throw an error.

If someone wants to reproduce the error, here's the project: https://www.dropbox....yz3daq/Test.rar

So I wonder how to fix this problem since I've never encountered this kind of problem. Also I've been messing around with the device initialization code a lot, and my best bet is that the error lies somewhere within, but maybe you can tell me how to set the device up so that all basic functions work properly (eg multisampling, depth/stencling, alpha blending)

I really hope you can help me with this.

PARTNERS