Fire bullet problem

Hi,



I want to fire a bullet, but the bullet should not start from the middle of the camera (as in the HelloIntersection example).

It should start a bit from the left (cause my weapon is there)…

The problem is that if i only set the bullet a bit more left or right on the x - axis like this


bullet.setLocalTranslation(new Vector3f(camPos.x-0.5f,camPos.y,camPos.z));


it doesn't work and the bullet starts anywhere on a circle with the radius 0.5 around the center (crosshair)
depending on the camera movement
So I changed the FireBullet class to this: (to make the bullet rotate with my camera movements)


   /** Move bullet to the camera location */
   //bullet.setLocalTranslation(new Vector3f(cam.getLocation())); //<- old one

You can make use of the scenegraph here; if you have a player-node, you can attach a gun to it which will make it turn and twist with the player. Then you just shoot your bullets in the gun's direction!

Hi Per,



the shooting isn't the problem… i shoot it in the cameras direction. I use a CameraNode here where the gun is attached…

The Problem is where the bullet should start (the bullet position). this should not be in the middle of the screen but there where the gun is…

Right, but as Per said, you can make use of the gun's world location and start the bullet there.

So you add the gun to your CameraNode/player-node, and then tweak its (the gun) local translation a bit so that it gets to the place you want.

hm, why is this a memory leak?



           bullet.getLocalTranslation().set(gun.getWorldTranslation());
//            bullet.setLocalTranslation(new Vector3f(gun.getWorldTranslation()));



both methods work...

for the other things,
well I reused the example in helloIntersection, which creates a new bullet every time the perfomAction method is called...
yes it would be better to reuse the bullets...
guess i'll look at the furballz code then

Yes, both works.



But in the one you commented out, you were using the "new" keyword. That tells Java you want to allocate more memory for, in your case, a brand new Vector3f. It's usually better to have your update logic clean of "news" :slight_smile:

I think it's not so much a leak but producing garbage by allocating and disposing a Vector3f for each new bullet.



To see Bullets in action i'd recommend jme-physics, there's a BallThrower class in there which pools bullets.

( com.jmextest.physics.BallThrower.java )



hth

Martin

I think it's not so much a leak but producing garbage by allocating and disposing a Vector3f for each new bullet.


I thought that was what was reffered to as a "leak" in Java (?).
Per said:

I thought that was what was reffered to as a "leak" in Java (?).

Not exactly. A leak in Java would mean that some memory cannot be reclaimed (objects garbage collected). e.g. a static reference to all created spatials...

Creating many new vectors and throwing them away only causes the application to hang quite often (when the garbage collector kicks in) the actual memory usage does not climb up that badly.

well, i changed all my code now…



is this now better



class xxx
{

Not exactly. A leak in Java would mean that some memory cannot be reclaimed (objects garbage collected). e.g. a static reference to all created spatials...


You're right -- that's a better definition!

Fly: yes, I guess, that pseudo-code is a little hard to get :)

Thank you guys :smiley:



Finally I got it…it took me hours ;O but it was so simple:



            Node gun = (Node)camNode.getChild(0);
            Vector3f gunWorldPos = gun.getWorldTranslation();
           
            bullet.setLocalTranslation(new Vector3f(gunWorldPos));

            bullet.setRenderState(bulletMaterial);

Happy to help!



Though… I hope that's not pasted from your update logic, as "new Vector3f(gunWorldPos)" would prove a memory-leak. Better to use bullet.getLocalTranslation.set(gunWorldPos).



Also, why are you setting a render state there? Such things should only be done when initializing the bullets, and the code you pasted seemed to come from a fire event of some sort.



The proper way to handle bullets and such is to store sets of them in pools, so that you can just reuse them when needed. It would surprise me if Furballs isn't doing this, so a tip is to poke around some in that code (if you're not already doing this (!)).