Page 1 of 1

Physics - What's a good workaround for maximum velocity?

Posted: Tue May 26, 2015 10:11 am
by HumanCatfood
Hey people,

I'm making a top-down spaceship game (like Asteroids). But I'm struggling to understand the maximum velocity limitation of love.physics.

To move I apply a linear force, simulating the thrusters. I kind of learnt to live with the ship not accelerating beyond a certain velocity (after all, I don't want it to go infinitely fast anyway), but now the ship can't shoot properly anymore, because the bullets it fires won't go faster than the ship and just bumble along in front of it.

I can think of a few solutions but I don't like any of them and would like to know if there's a better or established way:
  1. Use kinematic sensors for the bullets (so they still report collisions) and move them manually, but this seems dirty and might lead to problems if there are a lot of bullets.
  2. Limit the speed of both ships and bullets to be lower than the maximum velocity, but this has its own problems:
    • How exactly do I limit the velocity? Do I iterate over each body manually, check it's velocity and manually set it to something smaller? Do I apply force in the opposite direction? Friction?
    • I can't predict the maximum velocity, because there are asteroids in the world and it seems to change wildly depending how many there are:
      ~500 Asteroids -> about 400 m/s
      ~2000 Asteroids -> about 200 m/s
    • The game's quite boring if things move too slow. I guess I need to adjust the scale of things to make them look faster, but then the velocity changes again :x
I'm sure this is not a new problem, so if anybody has any pointers, I'd greatly appreciate it!

FYI:
  • ships are 4 by 4.5 units and have a mass of 7
  • bullets are 0.4 by 1.4 units and have a mass of 0.001
  • asteroids are random size between 20 by 20 and 50 by 50 and have a mass between ~25 and ~250 when set to 'dynamic' body. I tried using kinematic bodies but that didn't help.
  • 'meter' is set to 6 for drawing and 1 the rest of the time

Re: Physics - What's a good workaround for maximum velocity?

Posted: Tue May 26, 2015 1:10 pm
by ivan
The maximum velocity constrain in Box2D is very high - in the hundreds
and the library uses meter-kilogram-units - which means hundreds of meters per second,
probably around the speed of a real bullet.
Some people want instantly fast bullets (or lasers) - in that case you can just use ray casing.
kind of learnt to live with the ship not accelerating beyond a certain velocity
Oh ya, check that your damping is set to 0 for the moving objects.
can't predict the maximum velocity
You rarely have to do this. The maxvelocity is determined by the "maxTranslation" constant which is dependent on the time step.
It's tricky to get this part right, so I would just recommend using everyday sized-objects and velocities.
it fires won't go faster than the ship and just bumble along in front of it
This could be caused by several things.
When you spawn the bullets, do they have the any initial velocity?
Are you spawning the bullets on top the ship's fixtures?
You could use collision filters to make sure the ship firing the bullets doesn't collide with them.

4 by 4.5 meters is probably about the size of two cars which seems reasonable, their mass should be ~4000-5000 kg.
The mass is mostly relevant when object collide,
for example, if your spaceships are very heavy and your bullets are light then
the spaceships won't be affected too much when a bullet hits them.
Making your bullets kinematic bodies would be strange because kinematic bodies do not respond to collisions at all.
But ya, assuming your units are 'meters', then it sounds fine, I would suspect that something is up with your rendering/'setMeter'.

PS. welcome to the forums btw

Re: Physics - What's a good workaround for maximum velocity?

Posted: Tue May 26, 2015 1:45 pm
by HumanCatfood
Hi Ivan, thanks for the quick reply!
The maximum velocity constrain in Box2D is very high - in the hundreds
That's what I read. I don't understand why it's so low for me, and why that seems to correlate with the number of asteroids, which are completely separate objects, especially when set to 'kinematic'.
Some people want instantly fast bullets (or lasers) - in that case you can just use ray casing.
I might do that, but it would be nice to be able to do slow moving rockets, etc. Basically anything that starts from the ship but can still accelerate.
Oh ya, check that your damping is set to 0 for the moving objects.
Yep, no change :(
When you spawn the bullets, do they have the any initial velocity?
Are you spawning the bullets on top the ship's fixtures?
You could use collision filters to make sure the ship firing the bullets doesn't collide with them.
I'm pretty sure I got that bit right. I spawn a bullet in front of the ship (I set the masks to make double sure), give it the ship's velocity and rotation, then apply a huge impulse to it in the direction it's facing. I also tried setting the bullets velocity to ship's velocity + desired bullet velocity right away. Both work fine as long as I don't fire in travelling direction when at maximum velocity.
The mass is mostly relevant when object collide
Thank you, I guess I can rule that out then.
Making your bullets kinematic bodies would be strange because kinematic bodies do not respond to collisions at all.
Oh, not even when they're sensors? I don't necessarily need them to react to collisions by bouncing off things or so, I just want the collision callbacks to fire.
But ya, assuming your units are 'meters', then it sounds fine, I would suspect that something is up with your rendering/'setMeter'.
I tried different values for setMeter, including removing it completely, but no change.

I tried experimenting with the asteroids, making them bigger and smaller and that has no effect. Removing them completely does raise the maximum velocity significantly, but that brings me back to option 2: even if I manage to up the ante there, I still need the spaceship to be slower than the bullets. How is that usually done? Force in the opposite direction? Manually setting the velocity?

Ivan wrote:PS. welcome to the forums btw
Thanks so much! :)

Sorry for starting off with such a monolithic mess ..

Re: Physics - What's a good workaround for maximum velocity?

Posted: Tue May 26, 2015 7:39 pm
by potatoman
HumanCatfood wrote: [*] I can't predict the maximum velocity, because there are asteroids in the world and it seems to change wildly depending how many there are:
~500 Asteroids -> about 400 m/s
~2000 Asteroids -> about 200 m/s
That makes it sound like your simulation is too expensive, check if you are having a high cpu load while playing your game.
Though I don't have much experience with box2d so please don't take my word for it

Re: Physics - What's a good workaround for maximum velocity?

Posted: Wed May 27, 2015 8:39 am
by HumanCatfood
potatoman wrote: That makes it sound like your simulation is too expensive, check if you are having a high cpu load while playing your game.
I checked. The Löve process was the only one I found and that one never even gets over 0% CPU.

It's not a complicated game, it's basically Asteroids, but with more asteroids.