•      Sign In
• Create Account

# Morphex

Member Since 02 Sep 2011
Offline Last Active Oct 16 2013 04:15 PM

### 2D Platformer, Advanced physics SAT. (Think Sonic)

02 January 2013 - 06:25 PM

Well, first of all I have done this in different ways but right now I am trying to implement it with SAT. Before I start let me set two points clear:

• Separating Axis Algorithm is working flawless, both Swept method (TOI etc)and Discrete(Normal MTV) (I have written a article on it, with a tutorial , soon to be posted for all to use/read)
• I can implement this in a hacky way with a crap ton of sensors around the player., by this I mean around 8.

So my question is, if I wanted to implement a movement like in the sonic Hedgehogs games for genesis using SAT how you go about it?

I have done this before with using sensors around the player to calculate slopes and all the other collisions, I could do it like this again, but I feel this way is hacky, I mean, using SAT I get somewhat a simplified 3D collision system, meaning I have access to normal of the polygons, their vertices, MTV, estimated time of impact, estimated time the polygon will leave (think bullets), etc.

I though of using the normal of the polygon the character is standing but it doesn't seem to want to work decently. I guess I want to find a aproach without using a sensor for each direction, bot, top, left,right angle, main.

So even if you don't know how to handle slopes like sonic does, SAT collision how would you do it in a simple movement way without using sensors, but still getting enough info to know where the collision is and stuff like that (This might trigger some ideas on my side, sinse I already know how to do this, your way might make see another perspective anyway).

Any insights would be greatly appreciated...

Sincerely Morphex.

### SAT, something is wrong I just don't know what....

24 December 2012 - 07:43 AM

I have implement this algorithm numerous times before, and it worked flawlessly everytime, right this is giving me trouble, and after rewritting this 3 times already I give up trying to find the error, maybe a set of new eyes can pinpoint it to me?

My problem is that it is detecting collisions where it shouldn't, and I can't seem to find out why.

```public static class SAT
{

public class MTV
{
public Vector2 Axis;
public float Magnitude;
public bool Collided = true;
}

public static Vector2[] GetAxes(Vertices vert)
{
Vector2[] axes = new Vector2[vert.Count];
// loop over the vertices
for (int i = 0; i < vert.Count; i++)
{
// get the current vertex
Vector2 p1 = vert[i];
// get the next vertex
Vector2 p2 = vert[vert.NextIndex(i)];
// subtract the two to get the edge vector
Vector2 edge = p1 - (p2);
// get either perpendicular vector
Vector2 normal = new Vector2(edge.Y, -edge.X);
// the perp method is just (x, y) => (-y, x) or (y, -x)
axes[i] = normal;
axes[i].Normalize();
}
return axes;
}

public static MTV CheckCollision(Vertices polygon1,Vertices polygon2)
{
float overlap = float.PositiveInfinity;// really large value;
MTV MTV = new SAT.MTV();
MTV.Collided = false;
Vector2 smallest = Vector2.Zero;
Vector2[] axes1 = GetAxes(polygon1);
Vector2[] axes2 = GetAxes(polygon2);
// loop over the axes1
for (int i = 0; i < axes1.Length; i++)
{
Vector2 axis = axes2[i];
float min, max, min2, max2;
// project both shapes onto the axis
polygon1.ProjectToAxis(ref axis, out min, out max);
polygon2.ProjectToAxis(ref axis, out min2, out max2);
// do the projections overlap?
float d1, d2;
d1 = min - max2;
d2 = min2 - max;
if (d1 > 0 || d2 > 0) //no intersection
{
// then we can guarantee that the shapes do not overlap
return MTV;
}
else
{
// get the overlap
float o = d1 - d2;
// check for minimum
if (o < overlap)
{
// then set this one as the smallest
overlap = o;
smallest = axis;
}
}
}
// loop over the axes2
for (int i = 0; i < axes2.Length; i++)
{
Vector2 axis = axes2[i];
float min, max, min2, max2;
// project both shapes onto the axis
polygon1.ProjectToAxis(ref axis,out min, out max );
polygon2.ProjectToAxis(ref axis, out min2, out max2);
// do the projections overlap?
float d1, d2;
d1 = min - max2;
d2 = min2 - max;
if ( d1 > 0 || d2 > 0 ) //no intersection
{
// then we can guarantee that the shapes do not overlap
return MTV;
}
else
{
// get the overlap
float o = d1 - d2;
// check for minimum
if (o < overlap)
{
// then set this one as the smallest
overlap = o;
smallest = axis;
}
}
}
MTV.Collided = true;
MTV.Axis = smallest;
MTV.Magnitude = overlap;

return MTV;

}
}
```

If anyone asked here is the Project to Axis from Vertices( its just List) :

`public void ProjectToAxis(ref Vector2 axis, out float min, out float max)        {            // To project a point on an axis use the dot product            float dotProduct = Vector2.Dot(axis, this[0]);            min = dotProduct;            max = dotProduct;             for (int i = 0; i < Count; i++)            {                dotProduct = Vector2.Dot(this[i], axis);                if (dotProduct < min)                {                    min = dotProduct;                }                else                {                    if (dotProduct > max)                    {                        max = dotProduct;                    }                }            }        }`

### Programming a Game...

23 December 2012 - 08:43 AM

Well, I am not even sure if this is the place to post this, but still here it goes.

Right now I am messing around with a simple game concept, a 2D platformer. But one of those never ending games, think Robot Unicorn. WIth a few twists on it (more on that later).

For the first time, I am trying the approach build the game not a engine, but I find myself troubled. When I writting code (that does what it is supposed to do), I always find myself trying to code the a generic approach so that I could use to other games, this mostly applys to refactoring the working code, separating class creating managers for everything.

Is this the way its supposed to be? What your experiences with this, do you find yourself always trying to make the "perfect" code, or do you hack and slash to just get something to work.

Any pointers on which kind of methodology I should ensure in my coding habits?

Just a little background, I have created games before, and a lot of decent engines for me and other folks, so I like to think of myself of decently knowledgeable on this subject (Just as a hobby of course, got much to learn). So I am not asking how to start, but more on how should I proceed to improve.

### Continuous Generating a 2D Level (Platform)

21 December 2012 - 12:27 PM

Well, I have been mocking up ideas about a 2D sidescroller that would continuously generate a level. Well I have a idea on how to do this, but I am not sure its the best one. So my question is mostly,how would you go about this?

The "requirements" are quite simple, it should be able to generate random landscapes from a basic set of pieces, slopes, platforms multiple paths are all to be accounted.

I am thinking in have some sort of map that keeps each piece connection points  the beggining and the end, and it would set the pieces accordingly one after the other. Ofcouse this would need alot of conditions, but I can't think of another way that lets me have control, but still generate a infinite level.

### C#,C++ Some insight (Not about performance)

17 December 2012 - 08:59 AM

Well, let me start by saying this is not asking about which language is better/faster.

I have been programming in the .Net platform for about 6/7 years, so I quite like to think of myself as a "decent" programmer, and no matter what C# is one, if not, my preferred language.

I have been thinking in writing a (another) game this days, the thing is I am feeling divided by what approach to take.
I am not sure if I should go for c++ or c#, I am proficient in both, but I am not sure which one to pick.

My doubts are mostly because I don't want to reinvent the wheel, and I feel that C# lacks a lot of good libraries that are available in C++. I talking mostly about physics and other components that are needed for game. I don't want to roll my own on everything, why should I since there are many great ones available already out there that simplifies the job.

So my question, I think, is more on what language would be "easier" and more future prof to write the game. I am not worried about portability, since c++ would probably win hands down, even with mono laying by C# side.

I was thinking in using OPENGL / DirectX with C#, no XNA since I have grown a dislike on the lacking features of DirectX and how hard it is to implement some of features.

I want to hear your insight on this subject, how viable is C# to make a decent game, or if I should scracth it an go with the market choice of C++. Once more I am not asking how performant they are, and lets be realistic I am not going to make a AAA game, so my question is not really which is the best language, but which one would make development easier/faster.

PARTNERS