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.


how much realism do you want to add? I mean, real formulas or just something that looks good and works fine for your purpose?

If you just want to slide the character a little distance you can perhaps use a constant slide value, and then use Sinus. Something like:

private final int slideValue = 5;

  private double calcSlide(int angle) {
    return Math.sin(Math.toRadians(ange)) * slideValue;

By using sinus on angle 0 to 90 you get a value ranging from 0 to 1, so the greater the angle is the greater the slide will be.

You might probably have to elaborate with the slide constant.

If you want to use more realistic physics, I think you need to google for some formulas. But beware of using to much realism, sometimes that takes the edge off the gameplay.

My main issue is moving up and down slopes. I really can't get it right.
The realism I want is pretty much only falling and sliding. The rest will probably be modified to better suit the gameplay.

More details are needed and a more precise question. What exactly are you having problems with? The math/physics formulas regarding acceleration/velocity at different ramp angles? The coding of the math/physics formulas regarding acceleration/velocity? Having a figure "walk" at a certain velocity on a line in a GUI (i.e. swing repaints, affine Transforms, and using timers)? "Walking" and "falling" could be quite simple or quite elaborate. Taking stick figures alone, do you need to emulate the several stages of walking (i.e. heel only on ramp, then full foot on ramp, then toe on ramp, etc.)? Is the figure vertical compared to the screen or vertical as compared to the ramp? Etcetera, etcetera? All this can be fairly basic or as complex as you like regarding walking, falling, gliding to a stop, etc.

The character I currently have is just a small circle. Right now I don't really want to get the animations of the character itself to look good, I want it to work gameplay-wise.
This is what I have trouble with:
- The coding of the math/physics formulas regarding acceleration/velocity
- The main structure of when to calculate velocity
- Where and how to check for line collision

I don't really have a standard knowledge for where in the code to check for line intersections to prevent the character from falling through lines because he was falling too fast and such.
I see two ways of doing it:
1. Predict where the player will be in the next frame and draw a line from the current position to it. Then check for collision. If there's a collision, move the character to the intersection point and set the x and y velocities based on the slope angle.
2. Store the current position in Point(xPrev,yPrev). Move the character to the next position based on the xVelocity and the y Velocity. Check for line collition along the current position and the previous position. If there's a collision, move the character to the intersection point and set the x and y velocities based on the slope angle.

I tried the first one, but the code got really messy and I only got it half-working :(

From a physics standpoint, you need to have some base units. Let one pixel be the equivalent of one meter. Let g = the freefall gravitational constant on planet Earth of an object in freefall in a vacuum, which we'll round off to 10 meters/second/second.

Velocity is directional speed. You can define it as either:

class Velocity
    double velocityX;
    double velocityY;


class Velocity
    double speed;
    double angle;

or use all four. You can derive some attributes from the other attributes. I'd recommend that. It gives more flexibility.

class Velocity
    double velocityX;
    double velocityY;
    double speed;
    double angle;

You can also have a Direction class:

class Direction
     double xDirection;
     double yDirection;
     double angle;

You can derive xDirection and yDirection from angle, or you can derive angle from xDirection and yDirection, in which case Velocity could now be this:

class Velocity
     Direction direction;
     double speed;

A character at a certain time would be at a certain Point and have a certain velocity:

class GameCharacter
     Point position;
     Velocity velocity;     

You can also have a variable called gravity, which could be a multiple of g, which is 10 m/s. So on a planet with gravity 4, an object at free-fall would accelerate downwards at 40 m/s.

So you need to figure out acceleration, which is based on gravity and the angle of the ramp. So let's assume the character at time 0 is at (100, 150) and it's at a standstill, and gravity acceleration is 10 m/s/s. One pixel is one meter. Let's say you're using a ramp that is 30 degrees downward and to the right. Your overall speed increase (acceleration) will be 10 m/s/s times sine(30 deg.) = 5 m/s.

Your x component of this will 5 m/s/s times cos (30 deg.) = 4.33 m/s/s. Your y-component will be 5 m/s times sin (30 deg) = 2.5 m/s/s.

Integrating, your position at time t will be:

positionX = 100 + 4.33 * (t^2 / 2)
positionY = 150 + 2.5 * (t^2 / 2)

So to have it move, decide your time slice (i.e. repaint every 0.05 seconds), and keep recalculation the formulas above and paint your circle at the resulting positions.

The physics formulas are fairly simple:
F = m*a (a = F/m)
vt = vt-1+a(dt)
pt = pt-1 +vt(dt)

F = force
m = mass
a = acceleration
vt = velocity at time T
dt = time between frames
pt = position at time T

So, given the force and mass of an object you can get the acceleration, which in turn can give you the velocity and position.

You can also calculate the speed:
sqrt(speed = vx2 + vy2 + vz2) (the '2' means power by two)
Set vz to zero

Getting it into code can be a little tricky, but I suggest adding all the calculation into a gameloop

I hope this can help you a little

Thanks alot for all the help!
I'll be sure to try this out when I have time.
I'll let you know how it went and if I run into any more trouble.

What exactly is xDirection and yDirection?

It's a conversion between the polar and cartesian coordinate system. Polar coordinates measure the TOTAL distance from the center, as well as an angle. The cartesian coordinate system doesn't use angles, but rather an x coordinate and a y-coordinate. You can convert back and forth from each other.

Is this the kind of angle values I need for the slope angles?

Yes, that is one way of doing it. There is more than one way to measure an angle. As long as you're consistent, it's fine.

This is a project that will involve a certain amount of math and physics. Here is a good link/tutorial that will explain things. Things to also google might be:

  1. Polar coordinate system
  2. Cartesian Coordinate system
  3. Vectors in Physics Motion Calculation


I have another question.
I am using netBeans. I've always run the file by clicking "Run file" when the Main class is selected. But now I accidentally clicked "Test Project". The applet didn't launch, so I pressed "Run file" like I usually do. And now my images won't show up :(
I tried restarting; didn't help.
Any ideas?

I don't know if you should mark it as solved yet; I haven't fully tried this out yet as I've been very busy lately.

I don't know if you should mark it as solved yet; I haven't fully tried this out yet as I've been very busy lately.

That's fine. FYI, since it's your thread, we wouldn't mark it solved. You would, since you are the thread starter.

This article has been dead for over six months. Start a new discussion instead.