Computer Science Canada

help - moving things on an angle

Author:  whoareyou [ Mon May 16, 2011 6:34 pm ]
Post subject:  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 Neutral.

This is what I have so far, I guess its moving based on slope:

Java:

        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 <= c.maxy () - h)
                {
                    c.clear ();
                    x += 10;
                    y += 10;
                    c.fillOval (x, y, w, h);
                    Thread.sleep (10);
                }

                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 ? Confused

Author:  Tony [ Mon May 16, 2011 6:42 pm ]
Post subject:  RE:help - moving things on an angle

Trigonometry!

http://en.wikipedia.org/wiki/Trigonometric_functions#Sine.2C_cosine.2C_and_tangent

Author:  Raknarg [ Mon May 16, 2011 6:43 pm ]
Post subject:  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Θ
y = speed * sinΘ

In degrees btw, not radians. Idk how sin and cos work in Java.

Author:  whoareyou [ Mon May 16, 2011 6:47 pm ]
Post subject:  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Θ and y = speed * sinΘ, and then ... Neutral

Author:  Tony [ Mon May 16, 2011 6:51 pm ]
Post subject:  RE:help - moving things on an angle

so work with angles in radians... or just convert with Math#toRadians(double angdeg)

Author:  Raknarg [ Mon May 16, 2011 6:57 pm ]
Post subject:  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

Author:  whoareyou [ Mon May 16, 2011 6:57 pm ]
Post subject:  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 Smile

... so i can set a speed too right?

Author:  whoareyou [ Tue May 17, 2011 2:54 pm ]
Post subject:  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Θ and y = speed * sinΘ, 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 <= c.maxy ()), it still goes past the top of the screen.

The reason I'm asking these questions is because I'm trying to make a pong game, so if the ball was hit at an angle, then .... yah.

ps. if the paddle is rectangular, then the only way for the ball to be hit at an angle would be to hit it on it's edge right?

Author:  Tony [ Tue May 17, 2011 3:06 pm ]
Post subject:  RE:help - moving things on an angle

is it

x = speed * cosΘ

or

x += speed * cosΘ

?

There are typically two approaches to collision:
- check if collision will occur before taking a step
- taking a step first, then checking if you end up inside of a wall

If the ball is travelling fast enough, then there will be a substantial amount of space before/after the wall boundary.

Author:  whoareyou [ Tue May 17, 2011 3:30 pm ]
Post subject:  RE:help - moving things on an angle

x += speed * cosΘ (speed is 1, so just cosΘ and it needs to be added to the existing value of x)

Okay then, let's say I check if collision will occur before taking a step, and it does, what do I do with the ball? The ball will stay in its current position away from the boundary, and then when I have to reflect the ball off the boundary, then the animation of the moving ball will be messed up ...

I had this idea where instead of the ball moving up sinΘ, it would move up by 1. This way, the ball would stop exactly at the boundary. Then the x-value would change by cosΘ/sinΘ (divide x and y by sinΘ so y = 1 , and x = cosΘ / sinΘ) (kinda trying to do a slope thing) but when I run, it just moves the ball up the screen Neutral. I think its because the value of cosΘ/sinΘ isn't an integer so, it won't do anything. If I do the explicit typecast, then it would end up being 0. Is there anyway i can fix that ?

Author:  Tony [ Tue May 17, 2011 3:59 pm ]
Post subject:  RE:help - moving things on an angle

It's all approximations anyway. In some applications fudging +/- max_velocity/2 is "close enough". You can make it much better, but be prepared for much heavier Math -- http://compsci.ca/v3/viewtopic.php?t=14897

Author:  whoareyou [ Tue May 17, 2011 4:58 pm ]
Post subject:  Re: RE:help - moving things on an angle

Tony @ Tue May 17, 2011 3:59 pm wrote:
It's all approximations anyway. In some applications fudging +/- max_velocity/2 is "close enough".

I'm sorry, i don't understand Confused Embarassed

I'll just keep working on what I have so far. My main focus isn't the collision detection yet, it's actually getting the ball to move at the required angle. Then, I'll focus on bouncing it around the console screen which would require collisions.

If I can't do this, I'll probably try something simpler, like Tic Tac Toe, and focus on this in the summer Smile

Author:  Tony [ Tue May 17, 2011 5:03 pm ]
Post subject:  Re: RE:help - moving things on an angle

I mean that
whoareyou @ Tue May 17, 2011 3:30 pm wrote:
... the animation of the moving ball will be messed up ...

sometimes that is acceptable. It doesn't need to have LHC kind of precision, just something that is "good enough" to play the game Laughing

Author:  Raknarg [ Tue May 17, 2011 5:25 pm ]
Post subject:  Re: help - moving things on an angle

Tony wrote:

is it
x = speed * cosΘ
or
x += speed * cosΘ
?

Sorry, meant vx and vy.

Author:  whoareyou [ Tue May 17, 2011 6:52 pm ]
Post subject:  Re: help - moving things on an angle

Token @ Wed Mar 01, 2006 10:36 am wrote:
Okay, I got it figured out and completely forgot about this post, but it keeps freezing every once in a while, both on my home computer and school computer. my teacher said that its java's fault, but i think that if i were to change the screen mode somehow it would work better because i read some stuff on diferent screenmodes, does that make any sence at all? and if it does how would i go about doing that?

MysticVegeta: because I created my own class to store my commonly used methods so i wouldent have to recreate them or copy them over again.

heres my new code

code:
// The "Bounce" class.
import java.awt.*;
import java.applet.Applet;
import hsa.Console;


public class Bounce
{
    static Console c;           // The output console

    public static void main (String [] args)
    {

        c = new Console (25, 90);
        int ballx, bally;
        ballx = c.getWidth () / 2;
        bally = c.getHeight () / 2;
        int [] x = new int [11];
        int [] y = new int [11];
        int [] xdir = new int [11];
        int [] ydir = new int [11];

        for (int i = 1 ; i <= 10 ; i++)
        {
            x [i] = (int) Math.rint (Math.random () * c.getWidth ());
            y [i] = (int) Math.rint (Math.random () * c.getHeight ());
            xdir [i] = (int) Math.rint (Math.random () * 8);
            ydir [i] = (int) Math.rint (Math.random () * 8);
        }


        while (1 == 1)
        {

            for (int i = 1 ; i <= 10 ; i++)
            {

                c.setColor (Color.white);
                c.fillOval (x [i] - 5, y [i] - 5, 10, 10);

                x [i] += xdir [i];
                y [i] += ydir [i];

                if (x [i] >= c.getWidth () - 5 && xdir [i] > 0 || x [i] <= 5 && xdir [i] < 0)
                {
                    xdir [i] *= -1;
                }
                else if (y [i] >= c.getHeight () - 5 && ydir [i] > 0 || y [i] <= 0 + 5 && ydir [i] < 0)
                {
                    ydir [i] *= -1;
                }
                else if (dist (x [i], y [i], ballx, bally) < 55)
                {
                    xdir [i] *= -1;
                    ydir [i] *= -1;
                }

                if (i == 1)
                {
                    //c.clear ();
                }

                c.setColor (Color.black);
                c.fillOval (x [i] - 5, y [i] - 5, 10, 10);




            }
            c.fillOval (ballx - 50, bally - 50, 100, 100);
            delay (10);



        }





        // Place your program here.  'c' is the output console
    } // main method


    /////////////////////////////////--Delay--/////////////////////////////////

    static public void delay (int msec)
    {
        long enterMsec = System.currentTimeMillis ();
        long checkMsec = System.currentTimeMillis ();

        while ((checkMsec - enterMsec) < msec)
        {
            checkMsec = System.currentTimeMillis ();
        }
    }


    /////////////////////////////////--DISTANCE--/////////////////////////////////


    static public double dist (int x1, int y1, int x2, int y2)
    {
        return Math.sqrt ((Math.pow (dif (x1, x2), 2)) + (Math.pow (dif (y1, y2), 2)));
    }


    /////////////////////////////////--DIFERENCE--/////////////////////////////////

    static public int dif (int x1, int x2)
    {
        return Math.max (x1 - x2, x2 - x1);
    }


    /////////////////////////////////--DRAWBALL--/////////////////////////////////

    static public void drawball (int x, int y, int rad, Color color)
    {
        c.setColor (color);
        c.fillOval (x - rad, y - rad, rad * 2, rad * 2);
    }



    /////////////////////////////////--RANDCOLOR--/////////////////////////////////

    public static Color randColor ()
    {

        return new Color (
                (int) Math.rint (Math.random () * 255),
                (int) Math.rint (Math.random () * 255),
                (int) Math.rint (Math.random () * 255));
    }


    /////////////////////////////////--PAUSE--/////////////////////////////////

    static public void pause ()
    {
        char dummy;
        dummy = c.getChar ();
    }
} // Bounce class




Here the person (Token) isn't using any trig, but the balls are bouncing at the angles that they hit the wall at. But I don't understand how he's doing it. Can someone explain please?

Author:  Raknarg [ Tue May 17, 2011 6:56 pm ]
Post subject:  RE:help - moving things on an angle

It's simple. He adds or subtracts one to the x and y values. If it hits a wall, the x direction or y direction is multiplied by -1. That way it switches direction every time it hits a wall.

Author:  Tony [ Tue May 17, 2011 7:03 pm ]
Post subject:  RE:help - moving things on an angle

Because you can work with:
- velocity_x, velocity_y
or
- velocity, angle
Those are two different ways to represent the same information.

Author:  whoareyou [ Tue May 17, 2011 7:03 pm ]
Post subject:  RE:help - moving things on an angle

So in pseudo-code:

1) draw the ball (0,0)

2) add "x" to the xposition of the ball

3) +/- "y" to the yposition of the ball

4) If the ball hits the boundary, multiply current x by -1

Author:  Raknarg [ Tue May 17, 2011 7:04 pm ]
Post subject:  RE:help - moving things on an angle

You can do it without trig, but there's some cases where you'd want to use it. For instance, if your just moving a ball around the screen linearly, there's probably no reason to use trig.

Author:  Tony [ Tue May 17, 2011 7:13 pm ]
Post subject:  RE:help - moving things on an angle

Right. This assumes horizontal/vertical walls and incoming_angle = outgoing_angle (for collisions). Which works well enough for simple pong. Anything beyond those assumptions will require trig.

Author:  whoareyou [ Tue May 17, 2011 7:36 pm ]
Post subject:  RE:help - moving things on an angle

Very Happy

Java:

        int x, y, w, h, vx, vy, timer;

        x = (int) (Math.random () * c.maxx () - 10);
        y = (int) (Math.random () * c.maxy () - 10);
        w = (int) (Math.random () * c.maxx () / 2);
        h = w;
        vx = (int) (Math.ceil (Math.random () * 20));
        vy = (int) (Math.ceil (Math.random () * 20));

        timer = 5;

        c.setColor (Color.black);

        c.fillOval (x, y, w, h);

        while (true)
        {

            x += vx;
            y += vy;

            if (x < 0 || x >= c.maxx () - w)
            {
                vx *= -1;
            }

            if (y < 0 || y >= c.maxy () - h)
            {

                vy *= -1;
            }

            c.fillOval (x, y, w, h);

            Thread.sleep (timer);

            c.clear ();
        }


Except sometimes it gets stuck at the bottom and I don't know why. I tried subtracting 10 from the x and y coordinates so they don't get stuck, but that doesn't seem to fox the problem. Sad

Author:  whoareyou [ Wed May 18, 2011 3:27 pm ]
Post subject:  Re: help - moving things on an angle

I've run into another issue. Now, when I run my program, the ball bounces around for sometime and then eventually gets stuck in the paddle. What I've done what assign the y-values of the front face of the paddle to an array and it checks it will touch the paddle - if so, then the ball will reflect. But it gets stuck in the paddle after a couple of bounces.

Java:

        int x, y, w, h, vx, vy, timer;

        x = c.maxx () / 2;
        y = c.maxy () / 2;
        w = 50;
        h = w;
        vx = 5;
        vy = 5;

        int x2, y2, w2, h2;

        x2 = 0;
        y2 = 124;
        w2 = 30;
        h2 = 250;

        timer = 5;

        c.setColor (Color.black);

        c.fillOval (x, y, w, h);
        c.fillRect (x2, y2, w2, h2);

        int[] horizontal = new int [x2 + w2 + 1];
        int[] vertical = new int [h2 - y2 + 1];

        for (int i = 0 ; i < horizontal.length ; i++)
        {
            horizontal [i] = i;

        }

        int start = y2;

        for (int i = 0 ; i < vertical.length ; i++)
        {
            vertical [i] = start;
            start++;
        }

        while (true)
        {

            x += vx;
            y += vy;

            for (int i = 0 ; i < vertical.length ; i++)
            {
                if (vertical [i] < y && x < w && y < h2)
                {
                    vx *= -1;
                    break;
                }
            }

            if (x < 0 || x >= c.maxx () - w)
            {
                vx *= -1;
            }

            else if (y < 0 || y >= c.maxy () - h)
            {

                vy *= -1;
            }

            c.fillOval (x, y, w, h);
            c.fillRect (x2, y2, w2, h2);

            Thread.sleep (timer);

            c.clear ();
        }


: