Matrix rotation problems

I think I must be doing something completely wrong. What I want to achieve is a triangle that rotates to point at a dot. I started out by Using the formula angle = math.deg(atan2(y2-y1,x2-x1)), then when failing, I tried creating vec2s and using angle = math.deg(vec2a:angleBetween(vec2b)). Still not pointing correctly. Maybe I am doing my rotation wrong. I would appreciate any help getting this working. Please see code below, my intent was that the triangle should always point at the dot, regardless of the value of the parameters

function setup()
    parameter("x1",100,600,100)
    parameter("y1",100,600,100)
    parameter("x2",100,600,400)
    parameter("y2",100,600,400)
end

function draw()
    vec2a = vec2(x1,y1)
    vec2b = vec2(x2,y2)
    myangle = vec2a:angleBetween(vec2b)
    background(40, 40, 50)
    strokeWidth(5)
    stroke(0, 0, 0, 255)
    --print the target to point at
    ellipse(x1,y1,5,5)
    pushMatrix()
    --now I translate to where I want to draw my triangle
    --then I rotate to the angle
    --and draw 
    translate(x2,y2)
    rotate(math.deg(myangle))
    drawtriangle()
    popMatrix()
end

function drawtriangle()
    strokeWidth(8)
    stroke(255, 255, 255, 255)
    line(0,0,20,-60)
    line(20,-60,-20,-60)
    line(-20,-60,0,0)
end

Any tips appreciated, thanks.

You had to measure the angle between the vector formed betwen the two objects and the y axis, and then negate it:

function setup()
    parameter("x1",100,600,100)
    parameter("y1",100,600,100)
    parameter("x2",100,600,400)
    parameter("y2",100,600,400)
end

function draw()
    vec = vec2(x1,y1) - vec2(x2,y2)

    myangle = math.deg(vec:angleBetween(vec2(0,1)))
    background(40, 40, 50)
    strokeWidth(5)
    fill(255, 240, 0, 255)
    --print the target to point at

    ellipse(x1,y1,10,10)
    pushMatrix()
    --now I translate to where I want to draw my triangle
    --then I rotate to the angle
    --and draw 
    translate(x2,y2)
    rotate(-myangle)
    
    drawtriangle()
    popMatrix()
end

function drawtriangle()
    strokeWidth(8)
    stroke(255, 255, 255, 255)
    line(0,0,20,-60)
    line(20,-60,-20,-60)
    line(-20,-60,0,0)
end

pepinganos’ solution is correct (I hope) but the explanation a bit sloppy and misses to explain the vectors of interest.

You want to know the angle between your arrow and the place that the arrow should point to (and not the angle of two vectors based in the coordinate system’s root). The arrow faces north, the vector is therefore v(0,1) or anything else in this direction. It shall point from its place to the target place (the distance is target minus source), means v(x1,y1) - v(x2,y2). And if you take the angle between the arrow and the target, you don’t even have to negate.

Summary:


vec_arrow  = vec2(0, 1)
vec_target = vec2(x1, y1) - vec2(x2, x2)
myangle = vec_arrow:angleBetween(vec_target)
...
rotate(math.deg(myangle))

Thank you both for the quick answers. I think what I am reading is that my code was calculating the angle against the origin 0,0 but I need to calculate against 0,1 because I am drawing initially pointing north. I will experiment and can figure it out now. Thanks.

Thank you @Codeslinger, you explained it a lot better than I did.