Posted: 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 .
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 ?
Sponsor Sponsor
Tony
Posted: Mon May 16, 2011 6:42 pm Post subject: RE:help - moving things on an angle
Posted: 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
Posted: 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 ...
Tony
Posted: 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)
Posted: 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
Posted: 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
... so i can set a speed too right?
whoareyou
Posted: 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
Tony
Posted: 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.
Posted: 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 . 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
Posted: 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
Posted: 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
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
Tony
Posted: 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
Posted: 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
Posted: 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];
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?