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

2017/11/21

Targetting bullets

NielsV5B NielsV5B

2017/11/21

#
I'm making a game in which the player-class 'Man' can shoot. When you click your mouse, a bullet will go to the position on the moment of clicking, There also is an Enemy-class which occasionally shoots towards the Man-class. But there's a problem: because of the inaccuracy of the direction (caused by the fact that the world exists out of pixels), you can't shoot very accurate. There are only a few different directions in which you can shoot, and if your mouse is between one of those, it just choses the most nearby one. So I want to make the bullets turn to their destination every act. But when they have reached their destination, they should go straight ahead. So I made it stop turning towards it's destination when it's been near it, but it isn't working:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Bullet extends Movers
{
    private int speed;
    private int destinationX;
    private int destinationY;
    private String myTarget;
    private String mySender;
    
    private boolean isTouchingBlock = false;
    private boolean hasToRemove = false;
    private int margin = 5;
    private boolean passedGoTo = false;
    
    public Bullet(int targetX, int targetY, String senderClass, String targetClass)
    {
        setImage("Bullet" + senderClass + ".png");
        getImage().scale(25,5);
        speed = 3;
        destinationX = targetX;
        destinationY = targetY;
        mySender = senderClass;
        myTarget = targetClass;
    }
    
    protected void addedToWorld(World MyWorld)
    {
        turnTowards(destinationX, destinationY);
    }
    
    public void act() 
    {
        move(10);
        keepTurningI();
        checkTouching();
    }
    
    private void checkTouching()
    {
        if (canGetWorldManEnemy())
        {
            if ( isTouching(Enemy.class) && myTarget == "Enemy" )
            {
                Enemy enemy = getWorld().getObjects(Enemy.class).get(0);
                enemy.hitEnemy();
                hasToRemove = true;
            }
            else if ( isTouching(Man.class) && myTarget == "Man" )
            {
                Man man = getWorld().getObjects(Man.class).get(0);
                man.hitMan();
                hasToRemove = true;
            }
            else if (getOneIntersectingObject(Block.class) != null || getOneIntersectingObject(Door.class) != null || isAtEdge() || isTouching(Bullet.class) || getOneIntersectingObject(Weapon.class) != null)
            {
                hasToRemove = true;
            }
            else if (isTouching(Bullet.class))
            {
                removeTouching(Bullet.class);
                getWorld().removeObject(this);
            }
            else if (getOneIntersectingObject(ForceField.class) != null && mySender != "Man")
            {
                hasToRemove = true;
            }
            else
            {
                hasToRemove = false;
            }
        }
        if (hasToRemove == true)
        {
            getWorld().removeObject(this);
        }
    }
    
    private boolean isNearbyGoToPoint()
    {
        if ( isNearbyX() && isNearbyY() )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    private void keepTurningI()
    {
        if (isNearbyX() && isNearbyY())
        {
            passedGoTo = true;
        }
        if (passedGoTo == false)
        {
            turnTowards(destinationX, destinationY);
        }
        if (passedGoTo == false)
        {
            turnTowards(destinationX, destinationY);
        }
    }
    
    public boolean isNearbyX()
    {
        if ( getX() < destinationX + margin || getX() > destinationX - margin )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public boolean isNearbyY()
    {
        if ( getY() < destinationY + margin || getY() > destinationY - margin )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
        
    public String getSender()
    {
        return mySender;
    }
}
It still isn't accurate.
danpost danpost

2017/11/21

#
There are a couple of ways (at least) to improve the direction of movement. Probably the best one is to track the location and rotation of the actor using finer units of measure (rather than units of whole pixels and whole degrees). The SmoothMover class provided with greenfoot provides a finer coordinate system, but does not provide finer rotation. My QActor support class, however, provides both. It can be found in my Asteroids w/Improved QActor class scenario.
You need to login to post a reply.