• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sultanofswing.90

Isometric Collision Detection

1 post in this topic

Hello,

I'm developing a collision system for my little isometric engine. I have a PlayerEntity Class with a property

[source lang="csharp"]/// <summary>
/// Get or set the position of this entity
/// </summary>
public Vector2 Location
{
get { return _location; }
set
{
_location = value;
Rectangle = new Rectangle( (int)value.X, (int)value.Y, Rectangle.Width, Rectangle.Height );

if( Fixture != null )
Fixture.Location = value;
}

}
private Vector2 _location;[/source]

The rectangle field doesn't need any explanation. Well, what is a fixture? the Fixture is a Class needed for collision detection. Every fixture has his shape, for example

[source lang="csharp"]public enum FixtureShape
{
Rectangle,
Circle,
Rhomb,
MouseMap,
None
}[/source]
For example, [img]http://i46.tinypic.com/rqvxnd.png[/img] here the violet circle is the fixture for the player entity. For other shapes is similiar. If mouse map is choosen, the texture is thresholded and for every pixel with an alpha value different from zero, that pixel is set to black. Well, for collision detection i use per pixel collision algorithm, for now i use that from microsoft sample. Here's the function

[source lang="csharp"]public bool CheckCollision( Fixture f )
{
Rectangle f2Rect = f.Rectangle;
Color[] f2Data = f.FixtureData;

// Find the bounds of the rectangle intersection
int top = Math.Max( Rectangle.Top, f2Rect.Top );
int bottom = Math.Min( Rectangle.Bottom, f2Rect.Bottom );
int left = Math.Max( Rectangle.Left, f2Rect.Left );
int right = Math.Min( Rectangle.Right, f2Rect.Right );

// Check every point within the intersection bounds
for( int y = top; y < bottom; y++ )
{
for( int x = left; x < right; x++ )
{
// Get the color of both pixels at this point
Color colorA = FixtureData[ ( x - Rectangle.Left ) +
( y - Rectangle.Top ) * Rectangle.Width ];
Color colorB = f2Data[ ( x - f2Rect.Left ) +
( y - f2Rect.Top ) * f2Rect.Width ];

// If both pixels are not completely transparent,
if( colorA.A != 0 && colorB.A != 0 )
{
// then an intersection has been found
return true;
}
}
}

// No intersection found
return false;
}[/source]

And here's how i use it. I have a list of fixtures that are currently in the visible part of the screen, viewedFixtures. Here's the function where i check collision between objects and player

[source lang="csharp"]public bool IsPlayerColliding( List<Fixture> viewedFixtures, Vector2 MoveDirection )
{
if( PlayerFixture == null )
return false;

//take a clone of player fixture
Fixture t = (Fixture)PlayerFixture.Clone();
t.Location += MoveDirection;

foreach( Fixture f in viewedFixtures )
{
if( t.CheckCollision( f ) )
//there's a collision
return true;
}

return false;
}[/source]

what i do here is checking for collision between the player fixture, [u]moved along the direction the user choosed[/u], and the near fixtures, and this is the function that let the user to move the player in the map:

[source lang="csharp"]public void MovePlayer( Vector2 moveDirection )
{
if( Player == null )
return;

if( !CollisionSystem.IsPlayerColliding( Map.ViewedFixtures, moveDirection ) )
{
Player.Location += moveDirection;
}
}[/source]
here, if the fixture in the new location is not colliding, finally move the player and the player fixture along the choosed direction. Finally, here's the update method of the game source:

[source lang="csharp"]/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update( GameTime gameTime )
{
// Allows the game to exit
if( GamePad.GetState( PlayerIndex.One ).Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed )
this.Exit();

GamePadState padState = GamePad.GetState( PlayerIndex.One );

Vector2 MoveDirection = Vector2.Zero;

#region Movement section

#region North-west

if( padState.DPad.Left == Microsoft.Xna.Framework.Input.ButtonState.Pressed && padState.DPad.Up == Microsoft.Xna.Framework.Input.ButtonState.Pressed )
{
MoveDirection = new Vector2( -2, -1 );
}
else
#endregion North-west

#region North-east

if( padState.DPad.Right == Microsoft.Xna.Framework.Input.ButtonState.Pressed && padState.DPad.Up == Microsoft.Xna.Framework.Input.ButtonState.Pressed )
{
MoveDirection = new Vector2( 2, -1 );
}

#endregion North-east

#region South-west
else
if( padState.DPad.Left == Microsoft.Xna.Framework.Input.ButtonState.Pressed && padState.DPad.Down == Microsoft.Xna.Framework.Input.ButtonState.Pressed )
{
MoveDirection = new Vector2( -2, 1 );

}

#endregion South-west

#region South-east
else
if( padState.DPad.Right == Microsoft.Xna.Framework.Input.ButtonState.Pressed && padState.DPad.Down == Microsoft.Xna.Framework.Input.ButtonState.Pressed )
{
MoveDirection = new Vector2( 2, 1 );
}

#endregion South-east

#region North
else
if( padState.DPad.Up == Microsoft.Xna.Framework.Input.ButtonState.Pressed )
{
MoveDirection = new Vector2( 0, -1 );

}

#endregion North

#region South
else
if( padState.DPad.Down == Microsoft.Xna.Framework.Input.ButtonState.Pressed )
{
MoveDirection = new Vector2( 0, 1 );
}

#endregion South

#region West
else
if( padState.DPad.Left == Microsoft.Xna.Framework.Input.ButtonState.Pressed )
{
MoveDirection = new Vector2( -2, 0 );
}

#endregion West

#region East
else
if( padState.DPad.Right == Microsoft.Xna.Framework.Input.ButtonState.Pressed )
{
MoveDirection = new Vector2( 2, 0 );
}

#endregion East

#endregion Movement section

if( MoveDirection != Vector2.Zero )
world.MovePlayer( MoveDirection );


base.Update( gameTime );
}[/source]
Ok. There's a problem but i can't find it. Here's some screenshot:

[img]http://i48.tinypic.com/fkxge8.png[/img]

[img]http://i50.tinypic.com/11reltw.png[/img]

The problem is that the fixture go 'inside' the other fixture...anyone has a suggestion? (sorry for my bad english)

Thanks,
bye
0

Share this post


Link to post
Share on other sites
Sultan,

I'm wondering if that is even a problem for your code. You have both bounding shape collision and pixel collision. Although the bounding shape collision in both those images shows true, the pixel collision shows false. So these images don't necessarily show an invalid state. The bounding shape collision detection should just be a rough grade detection phase that won't always result in a collision once the finer collision detect (pixel-wise) process is complete.

I'm not 100% up on your code, so I'm just having a stab after first reading. What do you reckon ? Edited by Gavin Williams
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0