Computer Science Canada help - moving things on an angle |
Author: | whoareyou [ 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:
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 ? ![]() |
Author: | Tony [ 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 |
Author: | Raknarg [ 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. |
Author: | whoareyou [ 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 ... ![]() |
Author: | Tony [ 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) |
Author: | Raknarg [ 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 |
Author: | whoareyou [ 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? |
Author: | whoareyou [ 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? |
Author: | Tony [ 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. |
Author: | whoareyou [ 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 ![]() |
Author: | Tony [ 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 |
Author: | whoareyou [ 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 ![]() |
Author: | Tony [ 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 ![]() |
Author: | Raknarg [ 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. |
Author: | whoareyou [ 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
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? |
Author: | Raknarg [ Tue May 17, 2011 6:56 pm ] |
Post subject: | RE:help - moving things on an angle |
It's simple. He adds or subtracts one to the x and y values. If it hits a wall, the x direction or y direction is multiplied by -1. That way it switches direction every time it hits a wall. |
Author: | Tony [ Tue May 17, 2011 7:03 pm ] |
Post subject: | RE:help - moving things on an angle |
Because you can work with: - velocity_x, velocity_y or - velocity, angle Those are two different ways to represent the same information. |
Author: | whoareyou [ Tue May 17, 2011 7:03 pm ] |
Post subject: | RE:help - moving things on an angle |
So in pseudo-code: 1) draw the ball (0,0) 2) add "x" to the xposition of the ball 3) +/- "y" to the yposition of the ball 4) If the ball hits the boundary, multiply current x by -1 |
Author: | Raknarg [ Tue May 17, 2011 7:04 pm ] |
Post subject: | RE:help - moving things on an angle |
You can do it without trig, but there's some cases where you'd want to use it. For instance, if your just moving a ball around the screen linearly, there's probably no reason to use trig. |
Author: | Tony [ Tue May 17, 2011 7:13 pm ] |
Post subject: | RE:help - moving things on an angle |
Right. This assumes horizontal/vertical walls and incoming_angle = outgoing_angle (for collisions). Which works well enough for simple pong. Anything beyond those assumptions will require trig. |
Author: | whoareyou [ Tue May 17, 2011 7:36 pm ] | ||
Post subject: | RE:help - moving things on an angle | ||
![]()
Except sometimes it gets stuck at the bottom and I don't know why. I tried subtracting 10 from the x and y coordinates so they don't get stuck, but that doesn't seem to fox the problem. ![]() |
Author: | whoareyou [ Wed May 18, 2011 3:27 pm ] | ||
Post subject: | Re: help - moving things on an angle | ||
I've run into another issue. Now, when I run my program, the ball bounces around for sometime and then eventually gets stuck in the paddle. What I've done what assign the y-values of the front face of the paddle to an array and it checks it will touch the paddle - if so, then the ball will reflect. But it gets stuck in the paddle after a couple of bounces.
|