Game Programming

Need Some Physics Up In Here

by Cory on August 28, 2018 10:41 PM (Edited August 28, 2018 10:44 PM)

I added Velocity and Acceleration components to the MegamanScene. I also put in a PhysicsSystem to do collision detection, but everything's all wrong. This is way harder than I thought it was.

So what I have so far is pretty crap right now. The physics system operates on entities with Collision, Velocity, Acceleration, and Space components. I made a green slab to act as the floor and added the new components to the player character entity.

I also made a gravity component, which is functionally the same as the Acceleration component. Both of these components contain an sf::Vector2f that points in the direction of the acceleration. I added gravity to the player character entity, having it point straight down at 1 pixel per frame per frame.

To get the jump in, I set the velocity of the player character to 10 pixels per frame upwards and let the gravity acceleration handle bringing it back down.

Note how everything is all wrong. The player sinks down into the green platform arbitrarily, as the collision system allows tunneling right now. And if you look closely, you can see that the collision box doesn't even follow the sprite box either.

I've been reading about collision detection and there's so much stuff out there. I feel like I'm starting from square 1 all over again! I felt like I was almost done developing the engine when I finished the ECS, but now it just feels like I really didn't do much at all.

I've come across a lot of buzzwords that I should look into. Apparently something called "Swept AABB" (AABB is axis aligned bounding box, i.e. a rectangle that is not rotated which is used to calculate whether or not stuff inside the AABB is colliding with another thing, also encased in an AABB) sounds like a promising collision detection algorithm to start out with. Also, what functions and calculations to do once you've registered a collision has occurred is called "collision response". Typical things to do is to push the colliding entities backwards so that they don't overlap. (That would definitely help with my jumping code shown above). You might also want to do other things like transfer momentum (like in ping pong), or just have them lose all velocity. That's not really something that's easy to google, imo.

I might need to buy another book...

This Thought is part of Game Programming

Game programming general topic. I eventually hope to split this into separate ideas exclusively about specific games that I make.

back to the