When adopting any technology it is always a good idea to look into how its authors intended for it to be used. Since I skipped that step with Box2D and was having issues with physics, I thought I would start the process of evaluation over from the beginning. I want to go over the basic restrictions/recommendations for using Box2D and talk about how I am going to make a tuned ship experience based on these recommendations and my own tuning/design needs.
Box2D Units and restrictions
- Unit (Meter Kilograms Seconds)
- Recommended scale of moving object 0.1m to 10m
- 200m/s top speed (this one took some digging )
Scale – Does not fit my needs exactly
Given that most of the objects I am working on are roughly fighter sized, 10 meters is actually a bit on the small side. I may have to go with 0.1 = 1 meter to get the scale where I would like it. With this, my smallest moving unit would be a missile at 1m length and the largest would be a 100m boss ship. I’ll have to test if I can simply scale this. Metric calculations should scale with this even if they aren’t mathematically correct as long as I am consistent.
I have tested the top speed in my game and it is clamped to 200m/s. However, if you assume that the ship should cover 10-30% of the screen and lets assume it is 10 meters. This would mean a screen width of 33.3m – 100m. Giving it between 1 second to 1/3rd of a second to cross the width of the screen. I think that top speed should be fine but I will have to use ray intersections for things that travel faster and handle them as special cases. If I do my scale hack, I think that since 1m becomes 10m, 200m/s should become 2000m/s which should be more than enough for missiles (roughly mach six given that the speed of sound is about 340m/s).
Getting my ship moving
With the global restrictions out of the way, how do I move my ship?
- ApplyForce/ApplyImpulse – These will let me move my ship forward or backward using the API properly (SetVelocity bypasses this). The difference between a Force and an Impulse is that an Impulse is N*s (kg*m/s) whereas the Force is N. In my case I will be applying these at the center of mass so no torque will be applied.
- ApplyTorque – This will cause the ship to have an angular velocity. This can be overridden by setting the AngularVelocity directly.
- Damping – Both linear (velocity) and angular damping are supported and should make space…well less space like and have the ship slow down and be controllable by people that are used to friction.
In order to tune with the correct functions, I am going to be applying kg*m/s. For tuning what I am really interested in is things like the following:
- Time to top speed
- Max acceleration
- Ease-in/ease out
These aren’t the things that you tune with Box2D so I am going to need another layer.
What do I have to figure out this week?
- How do I set the mass of the ship?
- If I change the scale from 1 unit = 1m to 1 unit = 10m, what other variables do I need to change for mass?
- Convert from setting angular velocity to applying torque
- Determine how I want to tune the acceleration/force curve over time
- Use Box2D native dampening rather than my own hacked functions
I now have a better idea of how to use the technology correctly and I am looking forward to mapping it to a more friendly tuning mechanism. More on the practical issues I run into next time.