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

2015/9/20

Locations problem

Vellyxenya Vellyxenya

2015/9/20

#
Hi, here's what i want to do: I create an image (32x2) with a given rotation at a given x and a given y. Then, I want to add another image (from the same class) next to it. I can do it when the rotation is 0, but when the rotation is "int != 0", the new x of the new image must be smaller, you know, so i've been trying this code and i don't know what's wrong with it...
public void dispersion()
    {
        if(getWorld() !=null)
        {
            if(isLightning == true)
            {
                Actor lightning = (Actor)getWorld().getObjects(Lightning.class).get(0); 
                double nextX = 16*Math.cos(Math.abs(lightning.getRotation()));
                double nextY = 0;
                Lightning lightningNext = new Lightning(lightning.getRotation()+60);
                getWorld().addObject(lightningNext, (int)(lightning.getX()+ nextX), lightning.getY()+ (int)nextY);
                isLightning = false;
            }
        }
    }
where 16 is the: image.getWidth()/2 This code compiles but doesn't put the middle of the second image next to the end of the first, it's not accurate at all...
danpost danpost

2015/9/20

#
I would presume you would want them to be end to end (not middle to end). You can place the new lightning at the same location as the first giving it the same rotation as the first and move the 32 pixels along the angle the first one takes.
Vellyxenya Vellyxenya

2015/9/20

#
hm okay i can try that, but it will be difficult afterwards, because i want that the lightnings spread in the world, like a real lightning... any idea why the code part with the Math.cos doesn't show the true value?^^
Vellyxenya Vellyxenya

2015/9/20

#
It's like there's a problem with the conversion from double to int
danpost danpost

2015/9/20

#
Vellyxenya wrote...
It's like there's a problem with the conversion from double to int
The Math class trig functions that require an angle needs them in terms of radians, not degrees.
int rotation = getRotation();
double radians = Math.cos((double)rotation*Math.PI/180);
Vellyxenya Vellyxenya

2015/9/20

#
Ah okay, thank you very much :) By the way, i tried your way and it works pretty well ^^
You need to login to post a reply.