Added random rotation to pendel

This commit is contained in:
madmaurice 2016-03-17 11:54:41 +01:00
parent 80c974b291
commit e9d094b95f
1 changed files with 16 additions and 5 deletions

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python
from math import sin, cos, pi
from game import *
@ -30,6 +31,9 @@ class Vector(object):
def __sub__(self, other):
return self.__add__(other.__neg__())
def rotate(self,alpha):
return Vector( cos(alpha) * self.x - sin(alpha) * self.y, sin(alpha) * self.x + cos(alpha) * self.y)
def len(self):
return math.sqrt( self.x * self.x + self.y * self.y )
@ -41,7 +45,8 @@ class Vector(object):
class PendelSim(Game):
def __init__(self):
super(PendelSim,self).__init__(1000,600, 60)
super(PendelSim,self).__init__(1300,700, 60)
self.time = 0
self.init_data()
self.mousedown = False
self.mousepos = (0,0)
@ -54,16 +59,22 @@ class PendelSim(Game):
def init_data(self):
self.staff_lengths = staff_lengths = [ randint(150,200), randint(100,200), randint(100,150) ]
self.staff_lengths = staff_lengths = [ randint(200,300), randint(100,220), randint(30,120) ]
fixed_joint = Vector( self._width/2, self._height/5 )
joint_a = fixed_joint - Vector(staff_lengths[0],0)
joint_b = joint_a - Vector(0,-staff_lengths[1])
joint_c = joint_b - Vector(-staff_lengths[2],0)
joint_a = fixed_joint - Vector(staff_lengths[0],0).rotate( (randint(0,180) / 180 * pi) )
joint_b = joint_a - Vector(staff_lengths[1],0).rotate( (randint(0,180) / 180 * pi) )
joint_c = joint_b - Vector(staff_lengths[2],0).rotate( (randint(0,180) / 180 * pi) )
self.joints = [fixed_joint, joint_a, joint_b, joint_c ]
self.joint_speed = [ Vector(0,0), Vector(0,0), Vector(0,0), Vector(0,0) ]
self.old_joints = self.joints
def on_update(self, dtime):
self.time += dtime
if self.time > 10:
self.resetpath()
self.init_data()
self.time = 0
new_joints = self.joints[:]
for i in range(1,4):
new_joints[i] += (0.99 * self.joint_speed[i] + Vector(0,40)) * dtime