Basic platform movement and gravity

If that’s happening to you then your raycast is not written correctly, or you have very funkily shaped collision areas.

I’ve done my mentioned approach loads of time and it works great. Granted I usually use circle colliders so that makes it easy, but still.

Okey guys! I feel a little lost now! You talking about some funky raycasting and stuff :slight_smile:
Of what I understood from your replays was that it was difficult to calculate the collisions and when to stop which velocity due to my world implementation? Have I understood you right? :slight_smile:

What I want is a simple platformer with the basic movement and physics :slight_smile:

Someone correct me if I’m wrong, but…

In the Platformer I’m working on right now, instead of using a List style thing, I’m using a Block[][]. This means that I can customize my iterator so that I can control the direction of consideration. This allows me to find the point of first collision rather easily. From there, I can figure out where the collisions will occur.

The simplest way to raycast would just be to iterate over each pixel looking for collisions each time. Here is a more correct way to do it. As I see it that’s the best way to do collision - because it’s not discrete (therefore you can’t be moving so fast you pass over things).

But it may be beyond your skill level, sure. For now I’d just do pixel-by-pixel movement, i.e.


public void move()
{
    float magnitude = Math.sqrt(velocity.x*velocity.x + velocity.y*velocity.y);
    float normalizedVelocity = new Vector2(velocity.x / magnitude, velocity.y / magnitude);
    Vector2 newPosition = new Vector2(position.x, position.y);
    for (int i = 0; i < magnitude; i++)
    {
        if (!collidesWithStuff(position.x + normalizedVelocity.x * i, position.y + normalizedVelocity.y * i))
        {
            newPosition = new Vector2(position.x + normalizedVelocity.x * i, position.y + normalizedVelocity.y * i);
        }
        else
        {
            break;
        }
    }
    position = newPosition;
}

Thanks for the link! It seems like a valuable resaure. Also thans for the exemple! I’ll have to try to implement this into my game :slight_smile: