• Create Account

# mwkenna

Member Since 21 Jun 2006
Offline Last Active Nov 07 2013 09:30 AM

### In Topic: Basic Swept SAT Collision Response

30 October 2013 - 04:45 AM

Thanks for that... but in my post I did mention that I don't want to implement rigid body dynamics. All I am trying to achieve is the simplest collision response where a polygon cannot penetrate other polygons.

Thanks,

Mark.

### In Topic: 2D Multiple Collision Response SAT

27 October 2013 - 04:12 AM

After debugging the code further I believe the problem is now a case of how to deal with "touching" collisions.

What's happening is that when I detect a future collision and update the players position to "hug" the colliding object, the collision code is continuously detecting the "touching state" and not applying the position offset.

I have attached my code:

```protected override void Update(GameTime gameTime)
{
// Allows the game to exit
this.Exit();

//player position update
float amount = 10;
Vector2 offset = Vector2.Zero;
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Keys.Up))
offset += new Vector2(0, -amount);
if (state.IsKeyDown(Keys.Down))
offset += new Vector2(0, amount);
if (state.IsKeyDown(Keys.Left))
offset += new Vector2(-amount, 0);
if (state.IsKeyDown(Keys.Right))
offset += new Vector2(amount, 0);

foreach (Polygon p in polygons)
{
float t = 1.0f;
Vector2 N = Vector2.Zero;

Vector2 relPos = redPoly.Position - p.Position;
Vector2 relDisplacement = offset;

if (Collide(redPoly, p, relPos, relDisplacement, ref N, ref t))
{
if (t < 0)
{
//process teh current overlap
redPoly.Position -= N * t;
offset = Vector2.Zero;
}
else
{
if (t == 0)
{
//the objects are just touching
}
else
{
//process an overlap forward in time
redPoly.Position += offset * t;
offset = Vector2.Zero;
}
}
}
}

redPoly.Position += offset;

base.Update(gameTime);
}
```

### In Topic: 2D Multiple Collision Response SAT

26 October 2013 - 03:31 AM

I am trying to accomplish the "Basic Arcade Collision Response" from the PolyColly tutorial, but without the physics. All I want is for it to be basic but correct). This is what I have modified the code to but I'm not getting the functionality that I would expect, the player is sticking to the polygons and sometimes penetrating the surface.

Again.. trying to keep it simple just using the arrow keys to modify the offset of the player (redPoly).

```protected override void Update(GameTime gameTime)
{
// Allows the game to exit
this.Exit();

//player position update
float amount = 40;
Vector2 offset = Vector2.Zero;
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Keys.Up))
offset += new Vector2(0, -amount);
if (state.IsKeyDown(Keys.Down))
offset += new Vector2(0, amount);
if (state.IsKeyDown(Keys.Left))
offset += new Vector2(-amount, 0);
if (state.IsKeyDown(Keys.Right))
offset += new Vector2(amount, 0);

//get the list of collisions
List<CollisionResult> collisions = new List<CollisionResult>();
foreach (Polygon p in polygons)
{
float t = 1.0f;
Vector2 N = Vector2.Zero;

Vector2 relPos = redPoly.Position - p.Position;
Vector2 relDisplacement = offset;

if (Collide(redPoly, p, relPos, relDisplacement, ref N, ref t))
{
if (t < 0)
{
ProcessOverlap(ref redPoly, N, t);
}
else
{
ProcessCollision(ref redPoly, N, t, ref offset);
}
}
}

redPoly.Position += offset;

base.Update(gameTime);
}
```

Does this look correct to you guys?

Thanks,

Mark.

### In Topic: 2D Multiple Collision Response SAT

25 October 2013 - 07:54 AM

Here is the collision code that I am using. Note that I am currently trying to understand it so I'm not worried about that fact that it's not very efficient yet. Also note that it's the current collisions which are not working correctly.

Thanks,

Mark.

```protected override void Update(GameTime gameTime)
{
// Allows the game to exit
this.Exit();

//player position update
float amount = 50;
Vector2 offset = Vector2.Zero;
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Keys.Up))
offset += new Vector2(0, -amount);
if (state.IsKeyDown(Keys.Down))
offset += new Vector2(0, amount);
if (state.IsKeyDown(Keys.Left))
offset += new Vector2(-amount, 0);
if (state.IsKeyDown(Keys.Right))
offset += new Vector2(amount, 0);
redPoly.Position += offset;

//player velocity update
Vector2 displacement = new Vector2(0, 0);
MouseState mouseState = Mouse.GetState();
Vector2 mousePos = Vector2.Zero;

if (mouseState.LeftButton == ButtonState.Pressed)
{
mousePos = new Vector2(mouseState.X - (boxWidth / 2), mouseState.Y - (boxHeight / 2));
redPoly.Displacement = mousePos - redPoly.Position;
}

//get the list of collisions
List<CollisionResult> collisions = new List<CollisionResult>();
foreach (Polygon p in polygons)
{
var collision = Collide(redPoly, p, redPoly.Position - p.Position, redPoly.Displacement - p.Displacement);
if (collision != null)
{
}
}

var futureTimes = (from a in collisions where a.CollisionType == CollisionResultType.Future select a.T).ToList();
if (futureTimes.Count > 0)
{
futureTimes.Sort();
collPos = redPoly.Position + redPoly.Displacement * futureTimes.First();
}
else
{
if (mouseState.LeftButton == ButtonState.Pressed)
collPos = mousePos;
}

Vector2 mtd = Vector2.Zero;
var currentCollisions = (from a in collisions where a.CollisionType == CollisionResultType.Current select a).ToList();
if (currentCollisions.Count > 0)
{
collPos = Vector2.Zero;

//find the largest  MTD required to push the player out
foreach (var currentCol in currentCollisions)
{
Vector2 a = currentCol.CollisionNormal * currentCol.T;

if (Math.Abs(a.X) > Math.Abs(mtd.X))
mtd.X = a.X;
if (Math.Abs(a.Y) > Math.Abs(mtd.Y))
mtd.Y = a.Y;
}
}

if (mtd != Vector2.Zero)
redPoly.Position -= mtd;

base.Update(gameTime);
}
```

### In Topic: SAT Collision Response Finding Edge

25 October 2013 - 02:48 AM

Could a moderator please close this post, I realise that there is a better forum suited to this.

PARTNERS