handmade.network » Forums » 2d physics using rays
msmshazan
Shazan Shums
166 posts

Some day I will make quality software.
Programming FTW.

#11780 2d physics using rays
6 months, 4 weeks ago

Hi I'm trying to implement sonic like engine . currently using aabb and was thinking how to implement ledge animations using rays.
But I'm stuck in implementation.

Just being curious on programming....
AsafG
Asaf Gartner
29 posts
#11781 2d physics using rays
6 months, 4 weeks ago

You mean Sonic the Hedgehog?
This is a pretty interesting guide: http://info.sonicretro.org/Sonic_Physics_Guide
The solid tiles section talks about slopes and ledges.
msmshazan
Shazan Shums
166 posts

Some day I will make quality software.
Programming FTW.

#11783 2d physics using rays
6 months, 3 weeks ago

In platformers should movement be force based or impulse based and I am using velocity verlet integrator will this effect movement at this situation?

Just being curious on programming....
sbrouwer
Stijn Brouwer
1 posts
#11784 2d physics using rays
6 months, 3 weeks ago

I would just do it by only keeping track of velocity (and of course the previous frame's velocity as is required for velocity-verlet integration). Force or impulse based simulation is overkill for most platformers IMO, but it depends on what you want to do of course. For example, Sonic for example does it in the same kind of way as I will describe here, but something radically different like Grow Home uses a full force and impulse based physics simulation for it's movement.
Anyway, here's how I'd do it:
Every frame you add the gravity to the velocity like this:
1
velocity.y += gravity * deltaTime
.
The horizontal movement can be done by simply lerping the X component of the velocity vector to the input like this:
1
velocity.x = lerp(velocity.x, movementSpeed * inputX, movementAcceleration * deltaTime)

(This is just one way of doing horizontal movement, you could also do it with acceleration and drag instead of lerp, but I find that harder to tweak.)
When you want to jump, just set the Y component of the velocity like this:
1
velocity.y = jumpVelocity

At the end of the frame you'd just integrate the velocity (via velocity-verlet) and add it to the position. (Of course you'd do raycasting before applying the velocity first though, so you know how much you can move without colliding)