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

2019/12/19

Turning to Mouse but with an Radius

Valstrax Valstrax

2019/12/19

#
I´m trying to make a game where you fly a jet in it, that is controlled via mouse. Right now its flying directly at the mouse. So it means that it is working, but right now i want to make it go via turn(<value>) to the mouse so that this doesn´t happen. this is my code right now:
public class Jet extends Actor
{
    private int mouseX;
    private int mouseY;
    public int timer = 0;

    /**
     * Act - tut, was auch immer Jet tun will. Diese Methode wird aufgerufen, 
     * sobald der 'Act' oder 'Run' Button in der Umgebung angeklickt werden. 
     */
    public void act() 
    {
        move(4);
        setRotation();
    } 

    private void setRotation()
    {
        if(Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            mouseX = mouse.getX();
            mouseY = mouse.getY();
        }

        if (timer > 0) {
            timer--;
        }
        if(timer == 0) {
            timer = 3;
            setRotation(pointTo(mouseX,mouseY));
        } 

    }

    public int pointTo(int x, int y)
    {
        int dX = x - getX();
        int dY = y  - getY();
        double rotation = Math.atan2(dY, dX); 
        rotation = Math.toDegrees(rotation); 
        return (int)rotation;
    }  
}
danpost danpost

2019/12/19

#
Valstrax wrote...
I´m trying to make a game where you fly a jet in it, that is controlled via mouse. Right now its flying directly at the mouse. So it means that it is working, but right now i want to make it go via turn(<value>) to the mouse so that this doesn´t happen. << Code Omitted >>
I think you are wanting the same behavior as the turret in my Tank Target Practice scenario.
Valstrax Valstrax

2019/12/20

#
Yes, thank you. But the controls for the code don´t seem to work for me.
danpost danpost

2019/12/20

#
Valstrax wrote...
Yes, thank you. But the controls for the code don´t seem to work for me.
Please show what you tried.
Valstrax Valstrax

2019/12/20

#
I can´t find the QVAL value from you game.
Valstrax Valstrax

2019/12/20

#
Right now i have this
if(Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            targetX = mouse.getX();
            targetY = mouse.getY();
        }
        
        int angleToTarget = (int)(Math.atan2(getY()-targetY,getX()-targetX)*180/Math.PI);
        int rotationToTarget = (this.getRotation()-angleToTarget+540)%360-180;
        int directionOfRotation = (int)Math.signum(rotationToTarget);
        this.turn(QVAL*3/2*directionOfRotation);
But i don´t what QVAL is.
danpost danpost

2019/12/20

#
Valstrax wrote...
]I can´t find the QVAL value from you game.
QVAL is defined in the QActor class, that from which the Tank class extends.
Right now i have this << Code Omitted >> But i don´t what QVAL is.
You can probably just change line 10 to:
this.turn(2*directionOfRotation); // or '3*'
Valstrax Valstrax

2019/12/20

#
Thank you.
You need to login to post a reply.