slow bullets with physics simulation

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
rellig
Prole
Posts: 3
Joined: Mon Jan 13, 2014 12:28 pm

slow bullets with physics simulation

Post by rellig »

Hi everyone!

I tried using love.physics to shoot bullets on a top-down map with several obstacles.
My first thought was that using love.physics might come in handy at some point and I wanted to use it anyway, so I gave it a shot.
But now I think that a very fast, very small bullet might not be suitible for love.physics/box2d.
Creating, fireing and colliding the bullets really was not much of a problem and I was happy with my choice. But I can't get the bullets to any "bullet like" high speed at all.

bullet creation:

Code: Select all

		projectile = {};
		projectile.body = love.physics.newBody(World, player.pos.x, player.pos.y, "dynamic");
		projectile.shape = love.physics.newCircleShape(1);
		projectile.fixture = love.physics.newFixture(projectile.body, projectile.shape);
		projectile.body:setBullet(true);
		local speed = 10000;
		local x = speed * math.cos(player.rot);
		local y = speed * math.sin(player.rot);
		projectile.body:applyLinearImpulse(x, y);
I obviously have not too much of a clue about box2d best-practices. And I am not offended if you tell me that the whole idea was pretty stupid.
And I want to say that I have searched the internet/this forum without helping results before posting here, but I am sure you hear that a lot ...

I could supply a .love file, but the code looks horrible as it just grew over the past few hours from a small idea.

Any help in any direction would be much appreciated!

rellig
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: slow bullets with physics simulation

Post by Plu »

Physics engines aren't made for bullets (I'm not going to call you stupid because it's not a strange assumption to make, but it's the unfortunate truth)

So you'll have to make some changes. Bullets in general, if rendered like a pixel-bullet and then moved across the screen would work poorly; you'll never get the kind of fps where you can see them make a good path and they'd be close to invisible.

So you'll need something else. If you're going for physically accurate bullets, your best bet is probably drawing the bullet as a really fast moving line, and using some custom collision detection to see if the line hits something, or using Raycasting to project a ray along the path of the bullet and then detecting targets like that (it would make your bullets travel at infinite speed, but for a top-down view with a limited area around you, that probably doesn't matter anyway)

Anyway. Keep it up :) Physics are hard, and they're not suitable for everything.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: slow bullets with physics simulation

Post by ivan »

First of all "newCircleShape(1)" is a sphere with a radius of 1 meter.
Box2D has a maxVelocity treshold so your speed of 10000 is probably getting clamped.
Since maxVelocity is hard-coded, what you usually want to do is make the bullet smaller (0.01 m) then scale up your rendering code - this will make your bullet appear to move faster.
Secondly, applyLinearImpulse applies an impulse (N-seconds or kg-m/s) which is not a velocity.

But ya, the easiest solution could be to perform a raycast instead of simulating the bullet as a particle.

PS. BTW, if your game is "side view" and uses gravity you may want to set the bullet's gravityScale to 0
rellig
Prole
Posts: 3
Joined: Mon Jan 13, 2014 12:28 pm

Re: slow bullets with physics simulation

Post by rellig »

Thanks for your help!

Originally i started using love.physics for the ray casting (which made me happy at first, because I had no problems using it :ultraglee:), but then on the way I thought I could try using a full simulation instead, which got me here.

My bullets are ment to move at (nearly) infinite speed anyway, so raycasting is perfecly fine for the start (or always). Maybe I will use simulation for slower stuff like grenades or rockets and obstacles like barrels to colide with the player. If I ever get this far with this idea.

Again thank you two for your great help and giving insight into physics simulation (of course "1" means 1 meter, I only read this about a houndred times on the wiki ...)

rellig
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests