I'm making a Java applet that's a 2D platform game.
Each map contains (So far) only collision lines, a background and a foreground image. The collision lines are used for collision detection with pretty much everything that collides with the terrain.
For you guys to be able you help, I believe you need to know how the level system is set up. I'll try to explain how it works as simple as possible. It might get a bit confusing, but please bear with me. :)
I've created three classes for this line terrain system:
- Point
- Line
- LevelContainer
Point (int x, int y):
This class has got two integers; x and y. It's used to handle coordinates easier.
Line (Point p1, Point p2):
This class has got two Point objects to express a line
LevelContainer (double gravity, List<Line> lines):
This class is used to store levels. It contains a value for the gravity and a list of all the lines in the map.
Now to the issue; How do I make a character walk properly on these lines?
I have tried multiple times to get things working, but it always fails in one way or another. What I want is a character that can walk on all slopes <= 45°. When you stop walking you shouldn't stop instantly, I want a small glide. The glide's distance should vary depending on the slope angle of the surface the character is standing on.
I've got three fully working methods which can be useful to solve this problem:
public boolean lineIntersect(Line line1, Line line2);
Returns true if the two lines are intersecting
public Point lineIntersectPoint(Line line1, Line line2);
Returns the Point at which the two lines are intersecting. If they don't intersect it returns null.
public double pointDistance(Point p1, Point p2);
Returns the distance between two Points.
The current map you're playing on is loaded into a LevelContainer class, which is named currentLevel. If you want the level gravity you just type currentLevel.gravity;
This is alot of stuff that's hard to solve, but I'd be really happy if someone could help me on this.