Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Understanding Sprite coordinate systems
-
Very newbie question - apologies if there's obvious documentation on this; I haven't found it.
Ultimately, my code should draw a line segment starting at a specific coordinate on the screen (x, y) and ending at an offset (x + dx, y + dy). Importantly, dx and dy can be positive or negative. It will then change color over time, so I'd like to use a scene.ShapeNode that I can animate.
For starters, I'm testing by creating a line segment that starts at my touch point. However, getting the line to start at the touch point seems only to work with a somewhat unwieldy set of conditions to set the anchor_point (see below). I think this is because of the way the ShapeNode bounding rectangle, and therefore its coordinate system, changes as the size of the shape changes.
Is there some smarter, cleaner, more elegant way to get this done?
For bonus points, am I right in thinking that the +y direction for a Sprite is down, but the y coordinate of a touchPosition increases as you go up the screen? Thoughts on why?
def drawSeg(self, touchPosition): segPath = ui.Path() segPath.move_to(0, 0) dx = randint(-100, 100) dy = randint(-100, 100) segPath.line_to(dx, dy) seg = ShapeNode(path = segPath, position = touchPosition) seg.stroke_color = (255,255,255) seg.fill_color = "clear" if dx >= 0: if dy >= 0: seg.anchor_point = (0, 1) else: seg.anchor_point = (0, 0) else: if dy >= 0: seg.anchor_point = (1, 1) else: seg.anchor_point = (1, 0) self.add_child(seg)
-
segPath.line_to(dx,-dy) seg=ShapeNode(path=segPath, position=touchPosition) seg.anchor_point=((dx<=0),(dy<=0))
You are correct that scene coordinates has y positive = up. Images and path usually have the origin in the top left corner. The rationale... that is the way iOS does it... which is for obscure historical reasons.
-
If it's helpful, here is some code I was experimenting with to draw line segments and have a ball bounce off them using the scene module.
-
@JonB Thank you - that's perfect. And, as with all the best code, totally obvious in retrospect. Much appreciated!
-
@chriswilson Thank you, Chris - I will check it out. I appreciate your help.