Hi,
I want to draw a sprite between two points (A, B). Both points are determined by pointer positions. I used this well known approach:
// Setup
lineSprite = createSprite(lineImage)
setSpriteSize(lineSprite, -1, LINE_WIDTH)
setSpriteOffset(lineSprite, 0, getSpriteHeight(lineSprite) * 0.5)
// Update
dX = bX - aX
dY = bY - aY
angle = aTanFull(dX, dY)
length = sqrt((dX * dX) + (dY * dY))
setSpritePositionByOffset(lineSprite, aX, aY)
setSpriteAngle(lineSprite, angle - 90)
setSpriteSize(lineSprite, length, LINE_WIDTH)
But I get some inaccurate results as you can see in this screenshot:
The circle sprite is centered at the A coordinates and the trianlge is at the B coordinates. As you can see the line angle is slightly off (but gets closer at angles that are a full multitude of 90 degrees). Also the length is not correct (at straight horizontal angles it is correct, and gets worse the more vertical it gets). I try to use floats where ever possible. All sprites are created from PNG images.
I feel like I'm doing some Math stuff wrong.
Can somebody help me out please?