gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > side scroller and inclines

#5857 - sgeos - Sat May 10, 2003 11:25 pm

What is generally the best way to approach climbing inclines in a platform physics simulation? The down part is easy, as it comes as a side effect of gravity.

-Brendan

#5861 - tepples - Sun May 11, 2003 1:04 am

sgeos wrote:
What is generally the best way to approach climbing inclines in a platform physics simulation? The down part is easy, as it comes as a side effect of gravity.

Are you talking about a rolling object or a legged object? For rolling objects, you'll have to split gravity into components perpendicular to and parallel to the surface. The equation for this involves the slope. Walking objects tend to act much more nonlinearly, and you may have to hardcode movement characteristics for each slope you're planning on using.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#5864 - sgeos - Sun May 11, 2003 4:43 am

tepples wrote:

Are you talking about a rolling object or a legged object?


Thats a good question. I had not thought about that. I'll just "roll" everything, even legged objects for now.

tepples wrote:

For rolling objects, you'll have to split gravity into components perpendicular to and parallel to the surface.


Assuming that the slope is defined (our equation is not x = n), and gravity is apllied along the y-axis (probably down), does not gravity only have a component perpendicular to the surface?

tepples wrote:

The equation for this involves the slope.


How would you store the slope? Should each tile have a slope, or should an algorithm that 'reads the terrain' (by looking at the direction of movement and non-zero pixels) be written? I'd want to read the terrain to cope with curves. (Imagine terrain that looks like a sine wave, a few tiles high with a period four to eight tiles.)

tepples wrote:

Walking objects tend to act much more nonlinearly,


I imagine that this becomes more difficult to detect the further away they are viewed from.

tepples wrote:

and you may have to hardcode movement characteristics for each slope you're planning on using.


I'll think about this.

-Brendan

#5868 - tepples - Sun May 11, 2003 8:15 am

sgeos wrote:
tepples wrote:

For rolling objects, you'll have to split gravity into components perpendicular to and parallel to the surface.


Assuming that the slope is defined (our equation is not x = n), and gravity is apllied along the y-axis (probably down), does not gravity only have a component perpendicular to the surface?

Look at this ascii diagram:
Code:
          |
          |
          |
          | g
          |
          |         _,-'
          |     _,-'
          v _,-'
        _,-'
    _,-'
_,-'

This represents gravity (vector 'g') and a surface with slope of +1/2. (ASCII art cells are typically twice as high as wide.)

Code:
     a
      _,-':
    <'    :
     \    :
      \   :
     b \  :
        \ :         _,-'
         \:     _,-'
          v _,-'
        _,-'
    _,-'
_,-'

This represents the same gravity and the same surface, but the gravity vector has been decomposed into vectors 'a' parallel to the surface and 'b' normal to the surface. The acceleration of the ball is 'a' times a constant slightly less than 1 that accounts for rotational inertia. (A full Newtonian analysis, including angular momentum, is beyond the scope of my physics abilities at 2 AM.) Friction is proportional to 'b' and depends on the characteristics of the surfaces.

Quote:
How would you store the slope? Should each tile have a slope

Each tile number should have an associated slope. For example, if tile 34 is a piece with slope +1/2, then store 0x80 (fixed point for 128/256) in slope_table[34].

Quote:
I'd want to read the terrain to cope with curves. (Imagine terrain that looks like a sine wave, a few tiles high with a period four to eight tiles.)

For a Sonic clone, an approximation of slope every 8 pixels isn't going to hurt anybody, especially because grassy terrain is slightly irregular anyway.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#5926 - sgeos - Tue May 13, 2003 5:44 am

That is some awesome ascii art. I've worked something out, I'll use my inferior skills to attempt to explain it:

Code:

         x
          _,-':
        <>R  B:
         \    : G
          \   : |
         y \  : |
            \A: |
             \: V
            R<:
         _,-'B:
     _,-'    _:
 _,-'A      R| :
^^^^^^^^^^^^^^


G is the gravity vector.
x is the component of gravity parallel to the surface.
y is the component of gravity normal to the surface.
A is the angle of incline.
B is the other angle in the triangle.
R is a right angle.

x = G * sin(A)
y = G * cos(A)

Now... the bios only seems to have an atan routine. Does that mean that the only options are linking with the math library or a look up table? (What is atan good for?)

tepples wrote:

[SNIP diagram, see post above]

This represents the same gravity and the same surface, but the gravity vector has been decomposed into vectors 'a' parallel to the surface and 'b' normal to the surface. The acceleration of the ball is 'a' times a constant slightly less than 1 that accounts for rotational inertia. (A full Newtonian analysis, including angular momentum, is beyond the scope of my physics abilities at 2 AM.)


Is "less than 1 that accounts for rotational inertia" close enough to 1 to just use 1?

Quote:

Friction is proportional to 'b' and depends on the characteristics of the surfaces.


Either stored with the tile, or in a 'surface map'?

Quote:

Each tile number should have an associated slope. For example, if tile 34 is a piece with slope +1/2, then store 0x80 (fixed point for 128/256) in slope_table[34].


Why not use the angle instead of the slope?

Quote:

For a Sonic clone, an approximation of slope every 8 pixels isn't going to hurt anybody, especially because grassy terrain is slightly irregular anyway.


Excellent.

-Brendan

#5927 - sgeos - Tue May 13, 2003 7:22 am

Actually, I meant to ask: should not the net velocity for our critter be split into components relative to the surface? If so, than the above G vector should work for any vector parallel to the y-axis. Here is what I worked out for a vector parallel the x-axis:

Code:

   V----->
          _,\ y
    x _,-'R\/\
  _,-'A      B\
 ^^^^^^^^^^^^^^:
        A_,-'B:
     _,-'    _:
 _,-'A      R| :
^^^^^^^^^^^^^^


V is a vector parallel to the x-axis.
x is the component of the vector parallel to the surface.
y is the component of the vector normal to the surface.
A is the angle of incline.
B is the other angle in the triangle.
R is a right angle.

x = V * cos(A)
y = V * sin(A)

A + B = 90 degrees, so that funny A in the middle plus B (by y) indicates that y is normal to the surface.

The net velocity should be split into x and y compontent (relative to the axis) to begin with, so I don't think anything other than a lookup table is needed to split the vector relative to the surface.

Just in case anyone with weak trig is reading this, here are a few notes about the diagrams:

V cos(A) = V sin(B)
V sin(A) = V cos(B)
A = 90 - B
B = 90 - A

If V is replaced with G, these notes hold for the diagram in my previous post.

-Brendan

#5928 - Quirky - Tue May 13, 2003 7:41 am

You can use look up tables for sin and cos. Use fixed point maths - there's source for doing it on thepernproject, though you can improve it to use tables smaller tables (since you only need 90 degrees rather than a full 360, then it +-repeats).

atan is used to find an angle when you know the sides:

tan(A) = opposite/adjacent
A = atan(opposite/adjacent)

where the sides are as in this copy-pasted ascii art diagram:
Code:


        _,--:
    _,-'   _: opposite
_,-'A     | :
^^^^^^^^^^^^^
adjacent



So it might be useful if, for example, you had a baddy "aim" at something and it knows the x and y distance on screen away of its target, you can get it to rotate to the correct angle to fire.