Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 help - moving things on an angle
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Raknarg




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
whoareyou




PostPosted: 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
Raknarg




PostPosted: 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.
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
whoareyou




PostPosted: 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
whoareyou




PostPosted: 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 ();
        }
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 22 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: