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

Thread: Need help with motion direction

1. 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. 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. 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. 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).

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

Sayco (March 4th, 2012)

6. 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
}
}

7. 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. Re: Need help with motion direction

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

9. 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. 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. 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 .