Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //create game object
- game = {dragging:false, clip:_root.game_mc};
- //create object
- //point p0 is its starting point in the coordinates x/y
- game.myOb = {clip:game.clip.ob_mc, r:40};
- game.myOb.p1 = {x:150, y:80};
- //create first vector
- //point p0 is its starting point in the coordinates x/y
- //point p1 is its end point in the coordinates x/y
- game.v1 = {p0:{x:50, y:120}, p1:{x:250, y:120}};
- game.v2 = {};
- //function to draw the points, lines and show text
- function drawAll(v)
- {
- //place ob mc
- v.clip._x = v.p1.x;
- v.clip._y = v.p1.y;
- //drawing the walls
- game.clip.p1._x = v.p1.x;
- game.clip.p1._y = v.p1.y;
- game.clip.p2._x = game.v1.p0.x;
- game.clip.p2._y = game.v1.p0.y;
- game.clip.p3._x = game.v1.p1.x;
- game.clip.p3._y = game.v1.p1.y;
- game.clip.dragger1._x = v.p1.x;
- game.clip.dragger1._y = v.p1.y;
- game.clip.dragger2._x = game.v1.p0.x;
- game.clip.dragger2._y = game.v1.p0.y;
- game.clip.dragger3._x = game.v1.p1.x;
- game.clip.dragger3._y = game.v1.p1.y;
- game.clip.v1._x = game.v1.p0.x;
- game.clip.v1._y = game.v1.p0.y;
- game.clip.v1._xscale = game.v1.vx;
- game.clip.v1._yscale = game.v1.vy;
- game.clip.v2._x = v.p1.x;
- game.clip.v2._y = v.p1.y;
- game.clip.v2._xscale = game.v2.vx;
- game.clip.v2._yscale = game.v2.vy;
- game.clip.v3._x = game.v3.p0.x;
- game.clip.v3._y = game.v3.p0.y;
- game.clip.v3._xscale = game.v3.vx;
- game.clip.v3._yscale = game.v3.vy;
- }
- //main function
- function runMe()
- {
- //check if point is dragged
- if (game.dragging)
- {
- var v = game.myOb;
- var x = Math.round(game.clip.dragger1._x);
- var y = Math.round(game.clip.dragger1._y);
- v.p1.x = x;
- v.p1.y = y;
- var v = game.v1;
- var x = Math.round(game.clip.dragger2._x);
- var y = Math.round(game.clip.dragger2._y);
- v.p0.x = x;
- v.p0.y = y;
- var x = Math.round(game.clip.dragger3._x);
- var y = Math.round(game.clip.dragger3._y);
- v.p1.x = x;
- v.p1.y = y;
- updateVector(v, true);
- }
- //start to calculate movement
- var ob = game.myOb;
- //check the wall for collisions
- var w = game.v1;
- var v = findIntersection(ob, w);
- v = updateVector(v, false);
- var pen = ob.r-v.len;
- //if we have hit the wall
- if (pen>=0)
- {
- //move object away from collision point
- game.v2.vx = v.dx*pen;
- game.v2.vy = v.dy*pen;
- }
- else
- {
- //hide this vector so the example look nicer
- game.v2.vx = 0;
- game.v2.vy = 0;
- }
- //drawing the line which shows closest distance to the wall
- game.v3 = v;
- game.v3.p0 = {};
- game.v3.p0.x = ob.p1.x-game.v3.vx;
- game.v3.p0.y = ob.p1.y-game.v3.vy;
- //draw it
- drawAll(ob);
- }
- //function to find all parameters for the vector
- function updateVector(v, frompoints)
- {
- //x and y components
- if (frompoints)
- {
- v.vx = v.p1.x-v.p0.x;
- v.vy = v.p1.y-v.p0.y;
- }
- else
- {
- v.p1.x = v.p0.x+v.vx;
- v.p1.y = v.p0.y+v.vy;
- }
- //length of vector
- v.len = Math.sqrt(v.vx*v.vx+v.vy*v.vy);
- //normalized unti-sized components
- if (v.len>0)
- {
- v.dx = v.vx/v.len;
- v.dy = v.vy/v.len;
- }
- else
- {
- v.dx = 0;
- v.dy = 0;
- }
- //right hand normal
- v.rx = -v.dy;
- v.ry = v.dx;
- //left hand normal
- v.lx = v.dy;
- v.ly = -v.dx;
- return v;
- }
- //find intersection point of 2 vectors
- function findIntersection(v1, v2)
- {
- //vector between center of ball and starting point of wall
- var v3 = {};
- v3.vx = v1.p1.x-v2.p0.x;
- v3.vy = v1.p1.y-v2.p0.y;
- //check if we have hit starting point
- var dp = v3.vx*v2.dx+v3.vy*v2.dy;
- if (dp<0)
- {
- //hits starting point
- var v = v3;
- }
- else
- {
- var v4 = {};
- v4.vx = v1.p1.x-v2.p1.x;
- v4.vy = v1.p1.y-v2.p1.y;
- //check if we have hit side or endpoint
- var dp = v4.vx*v2.dx+v4.vy*v2.dy;
- if (dp>0)
- {
- //hits ending point
- var v = v4;
- }
- else
- {
- //it hits the wall
- //project this vector on the normal of the wall
- var v = projectVector(v3, v2.lx, v2.ly);
- }
- }
- return v;
- }
- //project vector v1 on unit-sized vector dx/dy
- function projectVector(v1, dx, dy)
- {
- //find dot product
- var dp = v1.vx*dx+v1.vy*dy;
- var proj = {};
- //projection components
- proj.vx = dp*dx;
- proj.vy = dp*dy;
- return proj;
- }
- //calculate all parameters for the wall vectors
- updateVector(game.v1, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement