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

2013/6/11

What's wrong with my trigonometry?

Kartoffelbrot Kartoffelbrot

2013/6/11

#
I am working on my crosshair. Therefore I need to get the radius of it in dependency of the distance to the player. But it's value is always to high.
public int getRadius()
    {
        double dx = getX()-playerX;
        double dy = getY()-playerY;
        if(dx<0)dx=-dx;
        if(dy<0)dy=-dy;
        double distance = Math.sqrt((dx*dx)+(dy*dy));
        return (int)(Math.tan((double)Superclass.precision)*distance);
    }
The distance values are right. It has to be something with the last line, but I don't know what it is.
Could you explain a bit better what you want to do with the crosshair? You can get rid of lines 5 and 6, and change lines 3 and 4 to:
double dx  = Math.abs(getX()-playerX);
double dy = Math.abs(getY()-playerY);
Duta Duta

2013/6/11

#
First off, you don't need lines 5 and 6 where you ensure it is positive, as you're then squaring the values, which means it doesn't matter whether they started off negative or positive -- what I mean is that 3*3 = 9 and also (-3)*(-3) = 9. There's actually a function in the Math class which does the pythagorean formula for you, so you can change
double distance = Math.sqrt((dx*dx)+(dy*dy));
to:
double distance = Math.hypot(dx, dy);
I'm not sure what you're trying to do with line 8 - what is Superclass.precision? If all you want to do is have your crosshair be larger the greater the distance to the player, then why not something like:
double scaleFactor = 0.1; // Change this to whatever "feels right"
return (int)Math.round(distance * scaleFactor);
Kartoffelbrot Kartoffelbrot

2013/6/11

#
Thank you all, bur I solved the problem now.
You need to login to post a reply.