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

2015/9/28

Problems with trig functions

Vellyxenya Vellyxenya

2015/9/28

#
Hi, I'm trying to set the rotation of my "orbes" according to the position of an "aimer" The aimer is in the top right corner and my orbes are in the center of my world Here's my code:
public void shoot()
    {
        if(counter == 280)
        {
            Aimer aimer = new Aimer();
            getWorld().addObject(aimer, 600, 100);
        }
        if(counter == 290)
        {
            Actor aimer = (Actor)getWorld().getObjects(Aimer.class).get(0);
            int xAimer = aimer.getX();
            int yAimer = aimer.getY();
            List<Orbe> orbes = getWorld().getObjects(Orbe.class);
            int length = orbes.size();
            if(length>=1)
            {
                for(Orbe orbe : orbes)
                {
                    int xOrbe = orbe.getX();
                    int yOrbe = orbe.getY();
                    int dx = xAimer-xOrbe;
                    int dy = yAimer-yOrbe;
                    int angle = (int)Math.toDegrees(Math.atan(dy/dx));
                    orbe.setRotation(180-angle);
                    readyToGo = true;
                }
            }
        }
    }
I tried many times and found out that the angle is either 0 (or even -45° if dy is higher than dx) I already asked the same question some days ago and tried the answer you game me but it doesn't want to work in the current code... please help me^^
Vellyxenya Vellyxenya

2015/9/28

#
Maybe it's not clear... I want the orbes to fly far away from the aimer
danpost danpost

2015/9/28

#
The variables 'dx' and 'dy' are int values and dividing one by the other as int values will not give any fractional results, which are needed for 'atan' to produce the appropriate values. You can cast them to double values and divide as follows:
int angle = (int)Math.toDegrees(Math.atan((double)dy / (double)dx));
There is, however a much easier way to adjust their rotation. I have a concern, though, as to where the method given is located. In what Actor subclass is this method located?
Vellyxenya Vellyxenya

2015/9/28

#
Yea in the meantime, i've found another way of doing it: by using turnTowards and making them move backward... it works (it's in the Actor class) But thank you for your answer, I didn't know i had to cast the dx and dy to doubles to have appropriate values. Thank you very much :D
danpost danpost

2015/9/29

#
Vellyxenya wrote...
Yea in the meantime, i've found another way of doing it: by using turnTowards and making them move backward... it works (it's in the Actor class) But thank you for your answer, I didn't know i had to cast the dx and dy to doubles to have appropriate values. Thank you very much :D
Using 'turnTowards' was what I was thinking when I wrote this:
danpost wrote...
There is, however a much easier way to adjust their rotation.
davmac davmac

2015/9/29

#
Vellyxenya wrote...
Yea in the meantime, i've found another way of doing it: by using turnTowards and making them move backward...
You could also use turnTowards followed by turn(180), and then move forwards :)
You need to login to post a reply.