How to fix the position of a growing path properly?
rownn last edited by rownn
I have a ui.path in a shapeNode, which is growing somehow every second. Unfortunately, the path is jumping from state to state. I know why it is jumping and I also know for what this behavior is good for, but I‘d like to fix the path in center where it is meant to be. I guess playing around with the anchor_point would fix the problem only in some cases. Of course I could calculate around with the nodes bbox and the path bounds, but I cannot imagine that there is no better solution. I also thought about defining the size of path with a rect() before generating the actual path, but the resulting rectangle would be visible, of course. I hope you know what I’m talking about. Following a snippet which hopefully describes better what my long questions is about.
from scene import * from time import time class MyScene (Scene): def setup(self): self.points = (Point(-100, -100),Point(100, -100),Point(100, 100),Point(-200, 100)) self.startTime = time() self.prev_dt = -1 ground = Node(parent=self) ground.position = (self.size.w/2, self.size.h/2) self.node = ShapeNode(ui.Path.oval(0,0,10,10), fill_color='clear', stroke_color='red') self.node2 = ShapeNode(ui.Path.oval(0,0,20,20), fill_color='clear', stroke_color='yellow') ground.add_child(self.node) ground.add_child(self.node2) def update(self): dt = int((time()-self.startTime)%4) if dt != self.prev_dt: self.prev_dt = dt path = ui.Path.oval(0,0,10,10) path.move_to(self.points.x, self.points.y) path.line_to(self.points.x, self.points.y) for i in range(dt): path.line_to(self.points[(i+2)%4].x,self.points[(i+2)%4].y) self.node.path = path if __name__ == '__main__': run(MyScene(), show_fps=False)
Thx alot forfor every hint :)
stephen last edited by
@rownn hang in there ive been tying for like 4 hour lol
JonB last edited by
The anchor point of 0,0 will fix the path in the center of the path's bounding box. But I gather you want the local path drawing coordinate 0,0 to be fixed in a particular spot.
ShapeNodes are really just SpriteNodes -- the path is converted to an image, then acts just like a SpriteNode -- so it totally loses that internal coordinate system. You could perhaps draw to an ImageContext, where you maybe have slightly more control, but if your shape is allowed to grow to unlimited size, that wouldn't really work.
There might be an option using ObjCInstance on your path, then callbounds -- that gives the bounds of the path in local path coords, I think, which would let you then easily adjust the anchor point, or use 0,0 anchor and some math to offset the x,y.
mikael last edited by
JonB last edited by
@mikael I have tried that before, but iirc it doesn't work.