Saturday, September 28, 2019

xna - How do I create a player movement system in Farseer?


My group of students is helping produce a a 2D platformer. We tried making a pixel-based collision system which fell through. Now I am trying this physics engine on for size and see new ways to create and control my player. Like the obvious circle body player:


Image


I was using a different type of player movement system like this XNA video example.


What system would work for a player that needs to be able to jump and move in mid-air (as in the above link), and can run up slopes like the circular body example?


I guess that the circular body can be made to do anything, but would like community input.




Answer



Please take my answer as "the right way to do this", not necessarily the "good for a student project" way.


Rigid body physics are entirely inappropriate for modeling "walking" behaviour.


Rigid body physics are good for handling things like bouncing and friction. Which are things you want to avoid in a typical platformer. There is no "right shape" that will make a satisfying platformer character on its own.




Basically you need to not be afraid to take matters into your own hands and "override" the physics engine. Rather than working with impulses and forces, like you might for typical physics bodies, you should manually control your player's position and velocity. (Sounds scary, but it's perfectly ok to do.)


I recommend: Before the physics step, set the velocity to an appropriate value using typical platformer movement mechanics. After the physics step, modify the position to correct any unplatformerly behaviours (I find it best to "snap" the player object onto the ground, rather than relying on physics - which can cause the player to "skip" off the ground).


The actual shapes you use are then fairly irrelevant.


Obviously this is a fair bit more work, and you have to be able to deal with the 2D geometry calculations. But the results are markedly better.





Here's how I do it:


My initial attempts used a diamond shape, which is advantageous because it only has one point of contact with the ground to worry about (similar benefits to using a circle, only better). I ignore/cancel collisions with this point in the physics engine (if they're with a ground surface) - because I handle them myself by snapping it to the ground in my "fix-up" step.


(The "weight" effect of the player in that video is done manually too. Without it, this works better with static level geometry.)


More recently I have taken to using a "hovering rectangle". Rather than snapping to the ground, I snap some distance above it. The rectangle body provides collision detection with the walls and ceiling, while the hover behaviour handles the floor.


I'm actually exploring doing away with the rigid body entirely, because I'm working on movements like wall jumping, etc. And, again, rigid bodies aren't well suited for simulating these kind of human movements.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...