|
-
Senior Member
[F8] Vectors help
I can get this vector stuff to work, going from moving a vector to making a vector bounce is a huge gap and I dont know how to get across.
Code:
// Declair Objects
Ball = {};
Wall = {};
// Balls First Point
Ball.p0 = {x:100, y:100};
// Wall First and Second Points
Wall.p0 = {x:380, y:120};
Wall.p1 = {x:380, y:200};
// Direction Components
Ball.vx = 1;
Ball.vy = 0;
Wall.vx = Wall.p1.x-Wall.p0.x;
Wall.vy = Wall.p1.y-Wall.p0.y;
// Main Function
onEnterFrame = function () {
// Move the MC MainBall to the current Ball vector
MainBall._x = Ball.p0.x;
MainBall._y = Ball.p0.y;
// Calculate the end point using the Direction Components
Ball.p1 = {};
Ball.p1.x = Ball.p0.x+Ball.vx;
Ball.p1.y = Ball.p0.y+Ball.vy;
// Make the first vector equal to the second vector
Ball.p0 = Ball.p1;
// Left Hand Norma of Wall
Wall.lx = Wall.vy;
Wall.ly = -Wall.vx;
// Dot Producted Between ball and wall
dp1 = Ball.vx*Wall.vx+Ball.vy*Wall.vy;
// projection of movement vector on the v2:
proj1.vx = dp1*Wall.dx;
proj1.vy = dp1*Wall.dy;
//reversing projection on the normal:
proj2.vx *= -1;
proj2.vy *= -1;
//finding new movement vector by adding up projections:
Ball.vx = proj1.vx+proj2.vx;
Ball.vy = proj1.vy+proj2.vy;
// The rest of the code just changes the direction componeants
// and resets the vector to the oposite side if it goes out of the stage.
}
That is what i have, i have been trying to follow tony's tute's, the first bit is heeps easy to follow wen well writting, but the whole bonce thing i dont get, it explains everything and how it works but I dont understand how the code does what it does. I downloaded the souce and its nothing like the stuff on his site so i found no use to it at all, is the code on his site just to show how stuff works and not actualy making the ball bounce?
Can some one have a look over my code and tell me what I have done and what needs to be done.
The italic code is the most recent stuff i added, once i added it my vector no longer moves.
Cheers
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
-
Senior Member
Ok, scince my last post me and a friend have neen working hard at getting this to work. And it does.
Code:
// Declair Objects
Ball = {};
Wall = {};
// Balls First Point
Ball.p0 = {x:100, y:100};
// Wall First and Second Points
Wall.p0 = {x:380, y:120};
Wall.p1 = {x:300, y:200};
// Direction Components
Ball.vx = 4;
Ball.vy = 1;
Wall.vy = -(Wall.p1.x-Wall.p0.x);
Wall.vx = Wall.p1.y-Wall.p0.y;
Wall.d = Math.sqrt(Wall.vx*Wall.vx+Wall.vy*Wall.vy);
Wall.midx = (Wall.p1.x+Wall.p0.x)/2;
Wall.midy = (Wall.p1.y+Wall.p0.y)/2;
Wall.vx /= Wall.d;
Wall.vy /= Wall.d;
// Main Function
onEnterFrame = function () {
// Move the MC MainBall to the current Ball vector
MainBall._x = Ball.p0.x;
MainBall._y = Ball.p0.y;
// Calculate the end point using the Direction Components
Ball.p1 = {};
Ball.p1.x = Ball.p0.x+Ball.vx;
Ball.p1.y = Ball.p0.y+Ball.vy;
// Make the first vector equal to the second vector
Ball.p0 = Ball.p1;
dp1 = Ball.vx*Wall.vx+Ball.vy*Wall.vy;
dist = MainBall._x*Wall.vx+MainBall._y*Wall.vy-(Wall.p1.x*Wall.vx+Wall.p1.y*Wall.vy);
if (Math.abs(dist)<MainBall._width/2) {
if ((dist<0 && dp1>0) || (dist>0 && dp1<0)) {
distfromcenter = MainBall._x*-Wall.vy+MainBall._y*Wall.vx-(Wall.midx*-Wall.vy+Wall.midy*Wall.vx);
if (Math.abs(distfromcenter)<Wall.d/2+MainBall._width/2) {
Ball.vx -= 2*Wall.vx*dp1;
Ball.vy -= 2*Wall.vy*dp1;
}
}
}
// Change the Direction Components
// Other unimportant code
}
So this is my method of conducting a bounce affect of of straight lines. I was wondering if one of you super math guys could offer some advice about it, like about if this is ok and if there are any flaws. So far it all works quite nicely.
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|