This site requires JavaScript, please enable it in your browser!
Greenfoot back
AA1
AA1 wrote ...

2014/12/18

Firing bullets from the tip of a long image instead of the center

AA1 AA1

2014/12/18

#
I am trying to fire bullets from a turret. When I fire and rotate, the bullets come out of the center of the image to the east. How do I change it so that the bullets come out of the tip of the gun? This code is based of the asteroid code which the rocket image is a much smaller image than the one I am using. My image is also longer than the rocket image. This is the code I am using to rotate the turret:
private void checkKeys() 
    {
        if (Greenfoot.isKeyDown("space")) 
        {
            fire();
        }

        if (Greenfoot.isKeyDown("left")) 
        {
          setRotation(getRotation() - 5);
        }

        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(getRotation() + 5);
        }
    }
and this code to fire the bullets:
private void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) 
        {
            Bullets bullets = new Bullets (getMovement().copy(), getRotation());
            getWorld().addObject (bullets, getX(), getY()-55);
            bullets.move ();
            reloadDelayCount = 0;
        }
    }
danpost danpost

2014/12/18

#
There are two things you can do to improve upon what you have. One, line 7 calls for the Bullets object to move; but, you do not specify how far. Adding a value about equal to the distance from where it gets added to the world to the end of the turret gun should place it at the end of the gun. The other is using the 'setPaintOrder' method of the World class to have objects of the Turret class painted over other objects to hide the Bullets objects until the end of the gun is reached.
xFabi xFabi

2014/12/18

#
Try this: http://www.greenfoot.org/support_classes/Mover.java https://www.youtube.com/watch?v=69qZB7isAho
AA1 AA1

2014/12/18

#
I see how these can help me, but the only thing is that the direction of my gun is facing 270 degrees rather than 0 degrees. So how do I change the code so that it comes out at 270 degrees?
danpost danpost

2014/12/18

#
Is not the rotation of the gun given to the Bullets objects when created by way of its constructor call? Line 5 on your 'fire' method shows this:
Bullets bullets = new Bullets (getMovement().copy(), getRotation());
dramallama dramallama

2014/12/19

#
I had this same problem myself, I wrote a quite ugly, bulky function to fix it, if you'd like, try this, it will use your rotation and the length of your barell in pixels(hypLength) to determine where the end of your barrel is. simply create the bullet at getX()+adjacent and getY()+opposite
public void checkRotation(double hypLength){
    if((getRotation()<=90)&&(getRotation()>0)){
            angleRad = ((double)getRotation())*(3.1415926/180.0);
            opposite = (Math.sin(angleRad))*hypLength;
            adjacent = (hypLength*hypLength)-(opposite*opposite);
            adjacent = Math.sqrt(adjacent);
    }
    if((getRotation()<=180)&&(getRotation()>90)){
            angleRad = (180-(double)getRotation())*(3.1415926/180.0);
            opposite = (Math.sin(angleRad))*hypLength;
            adjacent = (hypLength*hypLength)-(opposite*opposite);
            adjacent = Math.sqrt(adjacent);
            adjacent = adjacent - (adjacent*2.0);
    }
    if((getRotation()<=270)&&(getRotation()>180)){
            angleRad = ((double)getRotation()-180)*(3.1415926/180.0);
            opposite = (Math.sin(angleRad))*hypLength;
            adjacent = (hypLength*hypLength)-(opposite*opposite);
            adjacent = Math.sqrt(adjacent);
            adjacent = adjacent - (adjacent*2.0);
            opposite = opposite - (opposite*2);
    }
    if((getRotation()<=360)&&(getRotation()>270)){
            angleRad = (360-(double)getRotation())*(3.1415926/180.0);
            opposite = (Math.sin(angleRad))*hypLength;
            adjacent = (hypLength*hypLength)-(opposite*opposite);
            adjacent = Math.sqrt(adjacent);
            opposite = opposite - (opposite*2);
    }
    }
You need to login to post a reply.