Ok, scince my last post me and a friend have neen working hard at getting this to work. And it does.
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.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 }




Reply With Quote