# Need help with motion direction

• March 3rd, 2012, 08:02 PM
Sayco
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.
• March 3rd, 2012, 08:45 PM
helloworld922
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.
• March 3rd, 2012, 09:46 PM
Sayco
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.
• March 3rd, 2012, 10:06 PM
helloworld922
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.

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).
• March 4th, 2012, 02:07 PM
Sayco
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
}
}
• March 4th, 2012, 03:23 PM
TheJohnMajor01
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.
• March 4th, 2012, 08:35 PM
helloworld922
Re: Need help with motion direction
Quote:

Originally Posted by Sayco
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
• March 4th, 2012, 08:40 PM
Sayco
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 :D. 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.
• March 4th, 2012, 11:25 PM
helloworld922
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)
• March 6th, 2012, 09:15 AM
Sayco
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 :D.