Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drawLine uses <RectangleRenderer> instead of <LineRenderer> #3117

Open
robknopf opened this issue Jul 3, 2024 · 6 comments
Open

drawLine uses <RectangleRenderer> instead of <LineRenderer> #3117

robknopf opened this issue Jul 3, 2024 · 6 comments
Labels
bug This issue describes undesirable, incorrect, or unexpected behavior docs Relating to documentation in any way stale This issue or PR has not had any activity recently

Comments

@robknopf
Copy link

robknopf commented Jul 3, 2024

Drawing a line from a starting position other than (0,0) renders an incorrect line. Looks like drawLine is using RectangleRenderer instead of LineRenderer:

public drawLine(start: Vector, end: Vector, color: Color, thickness = 1) {
this.draw<RectangleRenderer>('ex.rectangle', start, end, color, thickness);
}

@eonarheim
Copy link
Member

Thanks @robknopf this definitely looks wrong...

Can you send me an example screenshot of where it's going wrong for you and what you expected

@robknopf
Copy link
Author

robknopf commented Jul 4, 2024

I threw together a codepen to demonstrate it here. If this doesn't suffice, let me know!

@eonarheim
Copy link
Member

eonarheim commented Jul 4, 2024

@robknopf Okay I think I know what's up. I'm going to be adding an explainer and some diagrams to the docs to help prevent this issue in the future. I'm labelling this as a docs+bug until I resolve that.

The reason I think this is confusing is actors have a center anchor by default vec(.5, .5) which means they will center their graphics around their pos.

  1. The white square has position (0, 0), but the white box graphic is centered by default using the (.5, .5) anchor, so the top left corner is actually (-25, -25)

    image

  2. The line Actor has is position relative to the parent, with relative offset (0, 0) so effectively on the same spot in red above.

  3. The Line graphic is relative to that new child's position, not the top left of the white box. But additionally has an anchor of (.5, .5) by default.

  4. Confusingly line graphics are also subject to the anchoring calculation, so ex.Line({start: vec(25, 25), end: vec(50, 50)}) having bounds with width ~= 25.7 and height ~= 25.7 gets shifted to apply anchor by .5 * -width and .5 * -height. Resulting in the current output.

Setting the anchor to vec(0,0) on lines makes them make more sense generally (not sure if automatically doing this would make it better or extra magically unexpected). So doing that zero anchoring the line is relative to the center of the white box producing this, which is what I expect:

image

Here is the code, I used to tweak it to be what you wanted where the line goes from the center of the box to the edge. Line relative to the center so starting at 0, and out to the edge at 25.

image

const line = new ex.Actor({
  pos: vec(0, 0)
});
line.graphics.anchor = vec(0, 0);
line.graphics.use(
  new ex.Line({
    start: vec(0, 0),
    end: vec(25, 25),
    color: ex.Color.Red
  })
);

@eonarheim eonarheim added bug This issue describes undesirable, incorrect, or unexpected behavior docs Relating to documentation in any way labels Jul 4, 2024
@robknopf
Copy link
Author

robknopf commented Jul 4, 2024

Wow.. impressively detailed response. The line isn't a child (in this case), so placing that actor it in the same position as the square and not changing the anchor (for either of them) feels like it should behave like I expected: A line from (0,0) to (25, 25) draws from the anchor (center) to the corner. There separate coordinate spaces between the actor and the associated graphic is a little confusing, but perhaps some documentation around the relationship will help.

Regardless, now I know and knowing is half the battle (so I hear). Thank you! I'll leave this open if you want any additional followup discussions, but I consider it closed.

@eonarheim
Copy link
Member

Thanks for digging into this with me! It does feel confusing for lines 100% I'll be chewing on this for a while.

Totally missed that line wasn't a child 🤦 but luckily doesn't change the math whew!

Copy link

github-actions bot commented Sep 3, 2024

This issue hasn't had any recent activity lately and is being marked as stale automatically.

@github-actions github-actions bot added the stale This issue or PR has not had any activity recently label Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue describes undesirable, incorrect, or unexpected behavior docs Relating to documentation in any way stale This issue or PR has not had any activity recently
Projects
None yet
Development

No branches or pull requests

2 participants