Creating a Body at the Position of Another Fast Moving Body

Post about the Flash World Construction Kit and the Alchemy port of Box2D.
civilian
Posts: 3
Joined: Tue May 07, 2013 9:44 pm

Creating a Body at the Position of Another Fast Moving Body

Postby civilian » Tue May 07, 2013 10:09 pm

I'm trying to fire a bullet out of the barrel of a fast moving vehicle's gun barrel. The problem I'm finding is that the bullet's start position appears to lag behind the position of the gun barrel. As the vehicle increases velocity, the bullet's start position lags behind further.

I'm guessing that the problem is that I'm positioning the bullet prior to the timestep getting called, such that the vehicle has already moved forward one step by the time the bullet has appeared on stage (thus creating the lag I'm seeing). Does anyone have an idea on how to address this (or perhaps an alternative idea as to what the problem is)?

So far I did try positioning the bullet after the 'create' function has executed. However, this doesn't seem to work (as I believe manually setting the x,y pos of a dynamic body doesn't work after it's been created... unless it's type is set to 'animated').

civilian
Posts: 3
Joined: Tue May 07, 2013 9:44 pm

Re: Creating a Body at the Position of Another Fast Moving B

Postby civilian » Thu May 09, 2013 12:07 am

Ok I think I found a solution. The lag in terms of bullet placement was actually a result of two issues. The first was that I had a listenWhileVisible loop that applied a setLinearVelocity to move the bullet. I had added the listenWhileVisible in the create function of the bullet, but I had forgotten that it doesn't get applied until the next step... so the bullet had no velocity for the first timeStep, and lagged behind a bit.

To fix the second problem (which I'm still not sure about the underlying cause of, but which further increased the lag), I added the velocity of the vehicle to the start position of the bullet. This gave me the correct start position for the bullets. However, I'm still not sure why I had to do this, as I'm pulling the current position of the vehicle's gun to set the bullet. But I guess it works, so for now I'll move on. Hope that helps somebody :)

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Creating a Body at the Position of Another Fast Moving B

Postby irresistible force » Thu May 09, 2013 12:46 am

The bullets are already moving at the same speed as the vehicle even before you fire them. Consider what would happen if you held an object out the window of the vehicle and just let it go.... it does not suddenly have zero velocity, it has the same velocity as the vehicle. So if you're throwing or shooting it forwards, then the velocity from throwing or shooting must be additional to the velocity it already has.

civilian
Posts: 3
Joined: Tue May 07, 2013 9:44 pm

Re: Creating a Body at the Position of Another Fast Moving B

Postby civilian » Thu May 09, 2013 4:59 am

My issue was with the starting position of the bullet though, only for the first loop of the stepEvent. After creating the bullet, I was adding the additional velocity of the vehicle correctly. For the first step, the bullet appeared to spawn in the wrong location (behind the vehicle slightly, despite me setting them to the exact coordinates of the gun barrel).

Before I was doing:
var bullet = new Bullet();
bullet.x = gunBarrel.x;
bullet.y = gunBarrel.y;

Wheareas this implementation has appeared to fix it:
var bullet = new Bullet();
bullet.x = gunBarrel.x + vehicle.b2body.getLinearVelocity().x*world.scale*world.timeStep;
bullet.y = gunBarrel.y + vehicle.b2body.getLinearVelocity().y*world.scale*world.timeStep;

I'm wondering why it's necessary to add that velocity right at the start just to get the right location.


Return to “WCK”



Who is online

Users browsing this forum: No registered users and 1 guest