Handmade Network»Forums
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
2d physics using rays
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.
Asaf Gartner
64 posts / 2 projects
Handmade Network Staff
2d physics using rays
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.
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
2d physics using rays
In platformers should movement be force based or impulse based and I am using velocity verlet integrator will this effect movement at this situation?
Stijn Brouwer
1 posts
2d physics using rays
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)