Info_edupage/TuGUI/Objects/nr_objects/controllable_objects/RopeInteractiveDot.py

40 lines
1.5 KiB
Python
Raw Permalink Normal View History

2021-09-02 22:25:45 +02:00
import math
import pygame
class RopeInteractiveDot:
2021-09-06 18:32:02 +02:00
object_type = "nr_c"
2021-09-02 22:25:45 +02:00
def __init__(self, position, r, color, force, forcefield_range, scene):
self.position = position
self.r = r
self.color = color
self.force = force
self.forcefield_range = forcefield_range
self.scene = scene
def blit(self):
2021-09-03 16:44:18 +02:00
pygame.draw.circle(self.scene.s, self.color, self.scene.matrix @ self.position, self.r * self.scene.pd_)
2021-09-02 22:25:45 +02:00
def input(self, mouse_pos, clicked):
self.position = mouse_pos
if clicked:
2021-09-06 16:03:18 +02:00
for object_batch in self.scene.get_i_objects():
2021-09-07 07:19:23 +02:00
rmp = object_batch[1] # relative mouse position
2021-09-06 16:03:18 +02:00
for object_ in object_batch[0]:
if object_.__class__.__name__ == "Rope":
for node in object_.nodes:
if node.locked is False:
2021-09-07 07:19:23 +02:00
if math.sqrt(sum((node.position - rmp) ** 2)) < self.forcefield_range:
force_vector = node.position - rmp
2021-09-06 18:32:02 +02:00
full_force_vector = (force_vector / math.sqrt(sum(force_vector ** 2))) \
* self.forcefield_range
2021-09-06 16:03:18 +02:00
force_vector = full_force_vector - force_vector
force_vector /= self.forcefield_range / self.force
node.position += force_vector
2021-09-02 22:25:45 +02:00