
-----------------------------------
whoareyou
Mon May 16, 2011 6:34 pm

help - moving things on an angle
-----------------------------------
I'm just playing around with Java right now in preparation for the final summative. What I've been working on is trying to move things at an angle. For example, I made a ball (oval with equal width and height) and I'm tying to move it at an angle ... and I have no idea how to do that :|.

This is what I have so far, I guess its moving based on slope:


        int x, y, w, h;

        x = 0;
        y = 0;
        w = 100;
        h = 100;

        c.setColor (Color.black);

        c.fillOval (x, y, w, h);

        while (true)

            {

                while (x = 0)
                {
                    c.clear ();
                    x -= 10;
                    y -= 10;
                    c.fillOval (x, y, w, h);
                    Thread.sleep (10);
                }

            }


So basically, my question is, let's say the ball is located at (0, c.maxy()) and I wanted to move it towards the top of the console screen at an angle of 30. How would I go about doing that?

Edit: I did a search on this site, and I think you'd need to find the speed of the ball ?  :?

-----------------------------------
Tony
Mon May 16, 2011 6:42 pm

RE:help - moving things on an angle
-----------------------------------
Trigonometry!

http://en.wikipedia.org/wiki/Trigonometric_functions#Sine.2C_cosine.2C_and_tangent

-----------------------------------
Raknarg
Mon May 16, 2011 6:43 pm

RE:help - moving things on an angle
-----------------------------------
Well, if you know the angle, the formula for the x and y speeds are:

x = speed * cos&#920;
y = speed * sin&#920;

In degrees btw, not radians. Idk how sin and cos work in Java.

-----------------------------------
whoareyou
Mon May 16, 2011 6:47 pm

RE:help - moving things on an angle
-----------------------------------
I looked at my methods sheet, and Math.sin(a), Math.cos(a) and Math.tan(a) are in radians.

So what I have to do is calculate the speed or first set a speed to the ball, then give it an angle, and then do x = speed * cos&#920; and y = speed * sin&#920;, and then ... :|

-----------------------------------
Tony
Mon May 16, 2011 6:51 pm

RE:help - moving things on an angle
-----------------------------------
so work with angles in radians... or just convert with Math#toRadians(double angdeg)

-----------------------------------
Raknarg
Mon May 16, 2011 6:57 pm

RE:help - moving things on an angle
-----------------------------------
You need to know what the speed is first. The formulas should give you the vx and vy based on what angle you gave it.

Note: You have to add the results of those formulas to your original x and y

-----------------------------------
whoareyou
Mon May 16, 2011 6:57 pm

RE:help - moving things on an angle
-----------------------------------
Why would radians be better? I just looked up radians and you can convert back to it by doing: (rad* 180/pi) ... or I typed what you typed in RTP except I changed it to degrees (Math.toDegrees) and it turns black, so I guess it works :)

... so i can set a speed too right?

-----------------------------------
whoareyou
Tue May 17, 2011 2:54 pm

RE:help - moving things on an angle
-----------------------------------
Converting from radians to degrees is painful, even if you use Math.toDegrees ... for example, it returns -57.something for sin30, which is 1/2.

Anyways, when I do the x = speed * cos&#920; and y = speed * sin&#920;, the ball goes off the screen a little then stops and I've been trying to stop it from going off the screen. Nothing I've tried seems to work (ie. while (y 