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.
Help with intersetion between line and rect
-
So, I'm working on my new project which is a game that there are two balls. One red, and one blue. You must draw a line to prevent them from touching.
I have gotten the balls, and bouncing to work and I just got the lines to work.
I just need help or ideas on how to get the balls bounce off the line.
I can use a similar code to the one I used for the edges of the screen but I'm thinking there might be issues since the line wont be horizontal or vertical. They might be diagonal.
Any ideas are welcome!import random, scene from scene import * class Particle(object): def __init__(self, wh): self.w = wh.w self.h = wh.h self.xblue = random.randint(0, self.w) self.yblue = random.randint(0, self.h) self.vxblue = 10 self.vyblue = 10 self.xred = random.randint(0, self.w) self.yred = random.randint(0, self.h) self.vxred = 10 self.vyred = 10 self.blueball = Rect(self.xblue, self.yblue, self.w/6, self.w/6) self.redball = Rect(self.xred, self.yred, self.w/6, self.w/6) def update(self): self.xblue += self.vxblue self.yblue += self.vyblue if self.xblue > self.w-100: self.xblue = self.w-100 self.vxblue *= -1 if self.xblue < 0: self.xblue = 0 self.vxblue *= -1 if self.yblue > self.h-100: self.yblue = self.h-100 self.vyblue *= -1 if self.yblue < 0: self.yblue = 0 self.vyblue *= -1 self.xred += self.vxred self.yred += self.vyred if self.xred > self.w-100: self.xred = self.w-100 self.vxred *= -1 if self.xred < 0: self.xred = 0 self.vxred *= -1 if self.yred > self.h-100: self.yred = self.h-100 self.vyred *= -1 if self.yred < 0: self.yred = 0 self.vyred *= -1 self.blueball = Rect(self.xblue, self.yblue, self.w/6, self.w/6) self.redball = Rect(self.xred, self.yred, self.w/6, self.w/6) def draw(self): fill('#3547ff') ellipse(*self.blueball) fill('#ff0b0b') ellipse(*self.redball) if self.blueball.intersects(self.redball): print('yay') class MyScene(Scene): def setup(self): self.particles = [] self.particles.append(Particle(self.size)) self.line = [] def touch_began(self, touch): self.pposx = touch.location.x self.pposy = touch.location.y def touch_moved(self, touch): self.x = touch.location.x self.y = touch.location.y x = touch.location.x y = touch.location.y self.line = Rect(self.pposx, self.pposy, self.x, self.y) def draw(self): background(0, 0, 0) for p in self.particles: p.update() p.draw() stroke(1, 1, 1) stroke_weight(7) line(*self.line) run(MyScene(), PORTRAIT)
-
Could you not mention a bunch of people in your posts unless you need to? Thanks.
-
A quick google search found me this:
Detection between a circle and a lineShould be a good start.