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 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
whoareyou




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




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




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




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




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




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




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




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




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




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




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




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




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




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




PostPosted: 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?
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 1 of 2  [ 22 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: