Skip to main content
Known Participant
October 16, 2023
Question

line to movieclip collision

  • October 16, 2023
  • 1 reply
  • 2289 views

Dear All Thank you for your time

I have aproblem that cant seem to work out looked on line and read some books but cant find

a solution. I have one movie clip that is tweened on a straight path that is movable with the mouuse

as wel as rotatable with buttons, the imbedde tweened object cabn also be moved with buttons . The collision object is a line that rotates 360deg with a button . That all works, what I cant figure out to do is display the distance to the line as the tweened object approaches the like a countdown to a colision.

added a photo to help explain.

Thank you all

    This topic has been closed for replies.

    1 reply

    kglad
    Community Expert
    Community Expert
    October 16, 2023

    you can get an exact answer for a disc/circle and an approximation for a square using:

     

    first, calculate the line segment's point of collision.

    then, calculate distance between your object's center of that point of collision

     

    if you need a more exact answer, you would need both collision points, the segment and the object.

    Known Participant
    October 17, 2023

    Dear Kglad

    Thank you for your reply

    I am not sure how acomplish to that, as both the object and the ray are on different time lines how and are both capable of moving to diffent places on the screen. I can work it out if static but not each a time a new ray position and a new path is chosen.

    Thank you peter

     

     

    Known Participant
    November 2, 2023

    copy and paste a code snippet or relink an updated fla


    Dear Kglad

    Thank you for your help.

    Here is the code the result is the line starts at top lh corner and only one dot is in the correct position .

    Best Regards

    Peter

    Result with stage code included

     

    Normal way it shows

     

     

     

     

     

    var root = this
    var line1;
    var line2
    //let myset = new Set();
    var dot1;
    var dot2;
    var dot3;
    var shape;
    var container = new createjs.Container();
    var s;
    var w;
    var z;
    var zx;
    var zy;
    var By;
    var x2;
    //let myArray = ["line","dot","shape"]
    function redraw() {
    createjs.Ticker.addEventListener("tick", tick);
    //createjs.MotionGuidePlugin.install();
    root.yellowmc.dot1.x = x2/stage.scaleX
    //var p = root.yellowmc.dot1.localToGlobal(0, 0); //dot and other end of the line
    var q = root.greenmc.dot1.localToGlobal(0, 0); // dot at other end of the line
    var z = root.yellowmc.myplane.myplane2. localToGlobal(0,0)
     
    x2 =  p.x
    var y2 = p.y
    var x4 = q.x
    var y4 = q.y
     
    var Ax = root.yellowmc.x //yellow line on the stage
    var Ay = root.yellowmc.y //green line on the stage
    var Bx = p.x
    By = p.y
     
    var Cx = root.greenmc.x
    var Cy = root.greenmc.y
    var Dx = q.x
    var Dy = q.y
     
    var top = (Dx - Cx) * (Ay - Cy) - (Dy - Cy) * (Ax - Cx)
    var bottom = (Dy - Cy) * (Bx - Ax) - (Dx - Cx) * (By - Ay)
    //console.log(top + ("Top"))
     
    var t = top / bottom
    //console.log(t, "T")
    w = Ax + (Bx - Ax) * t
    //console.log(w +"myw")
     
    s = Ay + (By - Ay) * t
    //console.log (s + ("mys"))
     
    //console.log(s)
    function newdot1(A, B) {
     
    dot1 = new createjs.Shape();
    stage.addChild(dot1);
    dot1.graphics.beginFill("blue").drawCircle(A, B, 20);
    //myset.add("dot")
    //myArray
    //dot1.alpha =1
     
    }
    function newdot2(A, B) {
     
    dot2 = new createjs.Shape();
    stage.addChild(dot2);
    dot2.graphics.beginFill("gren").drawCircle(A, B, 20);
    //myset.add("dot")
    //myArray
    }
     
    function newdot3(A, B) {
     
    dot3 = new createjs.Shape();
    stage.addChild(dot3);
    dot3.graphics.beginFill("red").drawCircle(A, B, 20);
    //myset.add("dot")
    //myArray
    }
     
    newdot1(w, s)
    newdot2(Ax, Ay)
    newdot3(Bx, By)
    var text = new createjs.Text("A", "20px Arial", "black");
    text.x = Bx
    text.y = By
    root.addChild(text);
     
    function createline1(a, b, c, d) {
    line1 = new createjs.Shape();
     
     
    stage.addChild(line1);
    line1.graphics.setStrokeStyle(1).beginStroke("#000000");
    line1.graphics.moveTo(a, b);
    line1.graphics.lineTo(c, d);
    line1.graphics.endStroke();
     
    stage.update();
     
     
     
    }
     
    function createline2(a, b, c, d) {
    line2 = new createjs.Shape();
     
     
    stage.addChild(line1);
    line1.graphics.setStrokeStyle(1).beginStroke("#000000");
    line1.graphics.moveTo(a, b);
    line1.graphics.lineTo(c, d);
    line1.graphics.endStroke();
     
    stage.update();
     
    //console.log(s + "barleft", w)
     
     
    }
     
    createline1(Ax, Ay, Bx, By)
    createline2(Cx, Cy, Dx, Dy)
     
     
    //s/*hape = new createjs.Shape();
    //shape.graphics.beginFill("#ff00ff").drawCircle(Ax,Ay,50);
    // stage.addChild(shape);*/
    ////myset.add("shape")
    // //window.shape = shape1
    ////createjs.Tween.get(shape).to({guide:{ path:[0,0,w,s,Bx,By] }},7000);
     
    ////Cant get tween to work
    function tick(event) {
    stage.update();
    }
    //console.log (myArray)
    var bar = dot1.x - 30
     
    c = dot1.localToGlobal(0,0)
     
     
     
     
     
     
    console.log(x2)
     
     
    //console.log(root.yellowmc.myplane.myplane2.x+("planestate"))
     
     
    }
     
     
    function test() {
    c = dot1.localToGlobal(0,0)
    z = root.yellowmc.myplane.myplane2.localToGlobal(0, 0);
    zx = z.x
    zy = z.y
    //console.log(zx+("planex"))
         console.log(zy+("planey"))
    console.log (s +("mys"))
    console.log(By+(" myby"))
    //console.log (w +("myw"))
     
    if (zy<= s){
     
    if(zy>=s){
    console .log ("cross")
    root.yellowmc.myplane.stop()
    }
     
     
    }
    }
    function startmc (){
    root.ind1.gotoAndPlay(1)
    //console.log("is playing")
     
     
    //console.log(z.x+"mp")
    }
    //nsole.log(w)
     
     
     
     
    //redraw()
    this.downbtn.addEventListener("click", planedown.bind(this));
    this.updatebtn.on("mousedown", planestart)
    function planedown() {
    root.yellowmc.gotoAndStop(0)
    root.yellowmc.y += 10
    //redraw()
    stage.removeChild(dot1)
    stage.removeChild(dot2)
    stage.removeChild(dot3)
    stage.removeChild(line1)
    stage.removeChild(line2)
    stage.update();
     
     
    }
     
    function planestart() {
    //root.yellowmc.myplane.play()
    redraw()
    }
     
     
    root.yellowmc.on("pressmove", moveplane)
    this.start_btn.addEventListener("mousedown",begin.bind(this))
    this.stop_btn.addEventListener("mousedown",mystop.bind(this))
    function begin(){
    root.yellowmc.myplane.play()
    myInterval = setInterval(test, 200)
     
     
    }
    function mystop(){
    root.yellowmc.myplane.stop()
    clearInterval(myInterval)
    }
     
     
     
     
     
     
    function moveplane(e) {
    stage.removeChild(dot1)
    stage.removeChild(dot2)
    stage.removeChild(dot3)
    stage.removeChild(line1)
    stage.removeChild(line2)
    e.currentTarget.x = e.stageX;
    e.currentTarget.y = e.stageY;
    root.yellowmc.gotoAndStop(0)
     
    }
     
    function calculate(){
     
     
     
    }
     
     
     
    /*
    this.yellowmc.on("pressup",stopplane)
    function stopplane(){
    redraw ()
     
     
     
     
    }*/