Posted: 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.
Sponsor Sponsor
Tony
Posted: 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.
Posted: 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
Raknarg
Posted: 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.
Tony
Posted: 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.
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.
whoareyou
Posted: 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.
Java:
int x, y, w, h, vx, vy, timer;
x = c.maxx () / 2;
y = c.maxy () / 2;
w = 50;
h = w;
vx = 5;
vy = 5;
int x2, y2, w2, h2;
x2 = 0;
y2 = 124;
w2 = 30;
h2 = 250;
timer = 5;
c.setColor (Color.black);
c.fillOval (x, y, w, h);
c.fillRect (x2, y2, w2, h2);
int[] horizontal = new int [x2 + w2 + 1];
int[] vertical = new int [h2 - y2 + 1];
for (int i = 0 ; i < horizontal.length ; i++)
{
horizontal [i] = i;
}
int start = y2;
for (int i = 0 ; i < vertical.length ; i++)
{
vertical [i] = start;
start++;
}
while (true)
{
x += vx;
y += vy;
for (int i = 0 ; i < vertical.length ; i++)
{
if (vertical [i] < y && x < w && y < h2)
{
vx *= -1;
break;
}
}
if (x < 0 || x >= c.maxx () - w)
{
vx *= -1;
}
else if (y < 0 || y >= c.maxy () - h)
{
vy *= -1;
}
c.fillOval (x, y, w, h);
c.fillRect (x2, y2, w2, h2);