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

2012/12/9

Help with shooting

1
2
mncomic mncomic

2012/12/9

#
Trying to code some guns that fire at moving targets when they come with range. How do I code gun to follow target and fire at it? Thanks
vonmeth vonmeth

2012/12/9

#
Take a look at this. It explains how to get the angle between one object and another, and how to turn towards that object. It also has an example Greenfoot scenerio provided at the bottom.
tylers tylers

2012/12/9

#
Also, have a look at this sernario here
mncomic mncomic

2012/12/11

#
Keep in mind I'm new...can someone explain the code? Thx
vonmeth vonmeth

2012/12/11

#
Could you post which bits you are confused on?
mncomic mncomic

2012/12/11

#
Here is what I am doing... I have a gun...it can rotate...there is a tank moving towards gun...I want the gun to fire at tank when it comes into range...I have gun firing and rotating, but want it to see Tank and fire round at it...
vonmeth vonmeth

2012/12/11

#
Right. The link I posted has a very simple implementation of this idea (sans the firing bit). The cat gets the objects in a range then rotates towards that object and moves towards it. Instead of moving, have it fire.
        List objects = getObjectsInRange(1000, Pointer.class);
 
        if (!objects.isEmpty())
        {
            Pointer pointer = (Pointer)objects.get(0);
            
            int distX = pointer.getX() - getX();
            int distY = pointer.getY() - getY();
            
            double angleRadians = Math.atan2(distY, distX);
            int angleDegrees = (int)Math.toDegrees(angleRadians);
                        
            setRotation(angleDegrees);
            
            move(3);
        }
 
mncomic mncomic

2012/12/11

#
Thx for the help...one more thing, this code is placed in the gun source code? will I need to place the Tank class in the code..if so, where? I really appreciate it
vonmeth vonmeth

2012/12/11

#
Yes, it is placed in the gun class. Lets say the name of your Tank class, is, well, Tank. It would look something like this.
import java.util.List; // you'll want this as well

List objects = getObjectsInRange(1000, Tank.class); // 1000 is the distance in a radius it searches  
  
if (!objects.isEmpty())  
{  
    Tank target = (Tank)objects.get(0);  
      
    int distX = target.getX() - getX();
    int distY = target.getY() - getY();
      
    double angleRadians = Math.atan2(distY, distX);  
    int angleDegrees = (int)Math.toDegrees(angleRadians);  
                  
    setRotation(angleDegrees);  
      
    fire();  
}  
danpost danpost

2012/12/11

#
Or, simply (in the gun class; without importing List):
if (!getObjectsInRange(1000, Tank.class).isEmpty())
{
    Tank tank = (Tank) getObjectsInRange(1000 Tank.class).get(0);
    turnTowards(tank.getX(), tank.getY());
    fire();
}
mncomic mncomic

2012/12/11

#
Thanks...its working...now how do I get the gun to fire just one round at a time?
vonmeth vonmeth

2012/12/12

#
Add a condition to fire that delays how often it can go off. A simple counter would work fine.
mncomic mncomic

2012/12/12

#
How's that done?
danpost danpost

2012/12/12

#
I think we should be asking you how you want it to be done (not with the programming, but with the action)? (1) each key press and release sends off one shot (2) holding down the key sends off one shot at a time with a delay between each one (3) each key press sends off one shot (key needs released and pressed again for the next shot) The difference between (1) and (3) is when the shot is sent off: (1) sends the shot when the key is relased and (3) sends the shot when the key is pressed. Or, is there some other way you would like this to happen?
vonmeth vonmeth

2012/12/12

#
private int rateOfFire = 20; // how often it can fire
private int counter;

public void act() {
	if(counter!=rateOfFire) counter++; // increment counter by 1

	if(counter == rateOfFire)
	{
       shoot();
	   counter = 0;
	}
}
There are more replies on the next page.
1
2