3
Answers

Several Questions(collission and storage)

Matthew Cash

Matthew Cash

15y
3.7k
1
So i'm working on a game, which i stopped working on for a couple months. I'm going over my code and i'm revising how i'm doing some stuff.
The first one is Collision Detection. When i(or someone else) maps areas, we place down 2 points to form a line. The char can then not cross over this line(its actually xx pixels from the chars center) Currently i do this by taking the slope of the line between those 2 points, inverting it, then finding a Y intercept that the new line falls on that includes the players center position. I then check if this point falls between the 2 points.

Does anyone know if theres an easier way/better way to do this?

Also can anyone suggest some form storage for a variety of file types, so that it will keep snoopers out, but not like lock down all the files(it should be optimised for Random mode reading only).
I was thinking of 2 files, one that contains a starting point and a lenght, then use something else to write that part of another file to a memmory stream.

Anyways, here is the current function i use for the collision
private bool CheckBlockLines(RenderInfo MapData, Point NewCharPos)
{
Point CalculatedPoint = new Point();
float CalculatedSlope;
float CalculatedInvertSlope;
int CalculatedIntercept;
int CalculatedInvertIntercept;

int Distance;
int Dist1;
int Dist2;
int Dist3;

Rendering.ILine TmpLine;
Rendering.ILabel TmpLable;

foreach (MapData.MapAttrabutes.TwoPointAttrabute PointPair in MapData.MapInfo.Attrabutes.BlockLines)
{

try
{
if (PointPair.Pos1.X - PointPair.Pos2.X == 0)
{
CalculatedPoint.X = PointPair.Pos1.X;
CalculatedPoint.Y = NewCharPos.Y;
}
else
{
CalculatedSlope = CalSlop(PointPair.Pos1, PointPair.Pos2);
if (CalculatedSlope == 0)
{
CalculatedPoint.X = NewCharPos.X;
CalculatedPoint.Y = PointPair.Pos1.Y;
}
else
{
CalculatedInvertSlope = (-1 / CalculatedSlope);
CalculatedIntercept = CalYInc(CalculatedSlope, PointPair.Pos1);
CalculatedInvertIntercept = CalYInc(CalculatedInvertSlope, NewCharPos);
CalculatedPoint = GetCross(CalculatedSlope, CalculatedInvertSlope, CalculatedIntercept, CalculatedInvertIntercept, PointPair.Pos1.Y, PointPair.Pos2.Y);

}
}

//check that the calculated point is betweem the 2 points
if (Between(PointPair.Pos1.X, PointPair.Pos2.X, CalculatedPoint.X, 0) &&
Between(PointPair.Pos1.Y, PointPair.Pos2.Y, CalculatedPoint.Y, 0))
{
Distance = CalDist(CalculatedPoint, NewCharPos);

if (Distance <= BarriorDist)
{
return true;
}
}
}
catch (Exception e) { e.ToString(); }

}

return false;

}


heres some more info:
MapData.MapAttrabutes.TwoPointAttrabute = Contains 2 Point vars(Int X, Int Y)
MapData.MapInfo.Attrabutes.BlockLines = a collection(Forget the type exactly)

private bool Between(int A, int B, int C, int Offset)
{
//Checks if C is between A and B
return ((A - Offset <= C && C <= B + Offset) || (A + Offset >= C && C >= B - Offset));
}

private float CalSlop(Point a, Point b)
{
return (float)(a.Y - b.Y) / (float)(a.X - b.X);
}
private int CalDist(Point a, Point b)
{
return (int)Math.Sqrt(Math.Pow((b.Y - a.Y), 2) + Math.Pow((b.X - a.X), 2));
}
private int CalYInc(float Slope, Point a)
{
return (int)(a.Y - (Slope * a.X));
}

private Point GetCross(float Slope1, float Slope2, float YInt1, float YInt2, int LY1, int LY2)
{

Point Rturn = new Point();
float x;
float y;

if (Slope1 == 0) // 'Line one is horizontal
{
Rturn.Y = LY1;
Rturn.X = (int)((YInt2 - LY1) / Slope2);
}
else if (Slope2 == 0) // Then 'Line two is horizontal
{
Rturn.Y = LY2;
Rturn.X = (int)((YInt2 - LY2) / Slope1);
}
else
{

y = ((YInt1 * (Slope2 / Slope1) - YInt2) / ((Slope2 / Slope1) - 1));
x = ((y - YInt1) / Slope1);


Rturn.X = (int)Math.Round(x);
Rturn.Y = (int)Math.Round(y);
}

return Rturn;
}

Answers (3)