Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Need help with motion direction

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Need help with motion direction

    So I am currently working on a very simple project, a bubbleshooter type of game where you have a launcher in the bottom center of the screen with a balloon floating atop. The launcher shoots a ball at the balloon, balloon pops and a new balloon is randomly placed on screen. I am fairly new to programming, currently in my 2nd term of college, so a program like this is not standard for me. The biggest problem I am having is the motion of the ball coming from the launcher, the launcher can rotate 180 degrees, from 1 end of the screen to the next. If my lauchner is 90 degrees, facing straight up, i know that all I have to do is decrease y-axis in order for it to shoot straight, but what if my angle is more complex than that? How can I insure that movement will be precise? This is the most difficult part for me, if anyone can help I would really appreciate it. Also when my launcher moves, it will be an increment of 5 degrees per keyPressed (0, 5, 10, 15... 180).

    Thank you.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with motion direction

    There is a simple trigonometric relationship between the angle and the x/y velocity of the projectile. I would suggest reading Projectile motion - Wikipedia, the free encyclopedia.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with motion direction

    Thanks for the suggestion, I will be looking into this and hopefully I will be able to solve my problem. Although after reading the article you just linked I am a bit more timid towards this little project but I will keep trying.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with motion direction

    It really boils down to a few equations:

    The ball's initial x and y velocity is this:

    Vx_0 = V * cos(theta)
    Vy_0 = V * sin(theta)

    where theta is the angle in radians and V is the magnitude of the velocity (a.k.a. it's speed).

    To convert between degrees to radians, multiply degrees by pi/180.

    If you haven't already, I would strongly recommend taking algebra and geometry at the very least. These classes will help you understand the basic math concepts such as vectors and angles.

    If you take physics I, it will help you to add gravity and collision detection/rebound to your game.

    Gravity is quite simple:
    Basically, as time goes on, Vy decreases.

    Vy = -g * t + Vy_0

    Collisions/rebound is harder, but doable provided you have the math/physics background (basic classes listed above).

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    Sayco (March 4th, 2012)

  6. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with motion direction

    I understand the equations, the only problem is I don't understand how I would implement it. I will be using a Timer class to determine the speed at which the ball moves, how would I implement this in my code? what would be incremented.

    Example,
    int angle = 45;
    int vel = 10; // milliseconds
    int velX = vel * cos(angle * (Math.PI / 180));
    int velY = vel * sin(angle * (Math.PI / 180));

    Timer time = new Timer(vel, new TimerListener());
    timer.start();

    class TimerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    // What do I enter here?
    // This is to shoot the ball
    }
    }
    Last edited by Sayco; March 4th, 2012 at 03:36 PM.

  7. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with motion direction

    Personally for rebouding I simply inverted the appropriate velocity value for example when the ball(I was working on a pong like game at the time) collided with the top of the screen the y velocity would switch from negative to positive it looked pretty realistic to me.

  8. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with motion direction

    Quote Originally Posted by Sayco View Post
    I understand the equations, the only problem is I don't understand how I would implement it. I will be using a Timer class to determine the speed at which the ball moves, how would I implement this in my code? what would be incremented.

    Example,
    int angle = 45;
    int vel = 10; // milliseconds
    int velX = vel * cos(angle * (Math.PI / 180));
    int velY = vel * sin(angle * (Math.PI / 180));

    Timer time = new Timer(vel, new TimerListener());
    timer.start();

    class TimerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    // What do I enter here?
    // This is to shoot the ball
    }
    }
    You're keeping track of the projectile's position, correct? At a given time step, simply keep updating it's position (this is a numerical integration).

    position.x = position.x + velX * time_step
    position.y = position.y + velY * time_step

  9. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with motion direction

    Well I figured it out, I got my ball moving in the proper directions using the calculations helloworld922 provided . Also for rebounding all I'm doing is drawing the ball in its original coords when it goes out of bounds (reloading the gun). I have added two buttons at the top of my frame, 1 for increasing the balls velocity and the other to decrease, the problem is when i click the button I lose focus of the main section which causes my keyPressed not to function. Any ideas how to fix this? I added setFocusable(true); into the ActionEvent but still does not work.

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with motion direction

    setFocusable() should only allow you to focus a component, not transfer the focus to that component.

    I'm not positive on the focus system in Java, but I believe calling requestFocusInWindow() will transfer the input focus to the component you want.

    If it does not, I would suggest reading How to Use the Focus Subsystem (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

  11. #10
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with motion direction

    Thank you for the suggestion again helloworld922, I've decided to remove these buttons from my game completely. My next chapter I'm studying has to do with the focusing system in java, so I will wait until then to dive into the topic. My game does not require this function, it was more of a perk, and thanks to you I have my game functioning properly .

Similar Threads

  1. need a bump in the right direction
    By mszyndlar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 15th, 2011, 01:38 AM
  2. [SOLVED] Someone point me to the right direction..
    By ineedhelp in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 30th, 2011, 10:03 PM
  3. JPanel motion
    By batyuska87 in forum AWT / Java Swing
    Replies: 2
    Last Post: March 8th, 2011, 01:00 PM
  4. Motion Tracking Questions
    By Brt93yoda in forum Java Theory & Questions
    Replies: 6
    Last Post: August 25th, 2010, 10:19 PM
  5. Direction,
    By Time in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 21st, 2010, 05:21 PM