class TimedWorld(soya.World): def __init__(self): soya.World.__init__(self) # self.speed = soya.Vector(self, 0.0, 0.0, -0.2) self.left_key_down = self.right_key_down = self.up_key_down = self.down_key_down = 0 self.j1f = self.dj1fp = self.j2f = 0 self.bp = 0 self.coordViewState = self.coordMpState = 0 # Like advance_time, begin_round is called by the main_loop. # But contrary to advance_time, begin_round is called regularly, at the beginning of each # round ; thus it receive no 'proportion' argument. # Decision process should occurs in begin_round. def bumpCoordViewState(self): self.coordViewState += 1 if self.coordViewState >= 4: self.coordViewState = 0 def bumpRenderMpCoords(self): self.coordMpState += 1 if self.coordMpState >= 2: self.coordMpState = 0 def bumpPauseState(self): global pauseState pauseState += 1 if pauseState >= 2: pauseState = 0 def next(self): #Returns the next action for event in soya.MAIN_LOOP.events: if event[0] == sdlconst.KEYDOWN: if (event[1] == sdlconst.K_q) or (event[1] == sdlconst.K_ESCAPE): sys.exit() # Quit the game elif event[1] == sdlconst.K_LEFT: self.left_key_down = 1 elif event[1] == sdlconst.K_RIGHT: self.right_key_down = 1 elif event[1] == sdlconst.K_UP: self.up_key_down = 1 elif event[1] == sdlconst.K_DOWN: self.down_key_down = 1 elif event[1] == sdlconst.K_a: self.bumpCoordViewState() elif event[1] == sdlconst.K_z: self.bumpRenderMpCoords() elif event[1] == sdlconst.K_p: self.bumpPauseState() elif event[0] == sdlconst.KEYUP: if event[1] == sdlconst.K_LEFT: self.left_key_down = 0 elif event[1] == sdlconst.K_RIGHT: self.right_key_down = 0 elif event[1] == sdlconst.K_UP: self.up_key_down = 0 elif event[1] == sdlconst.K_DOWN: self.down_key_down = 0 def vecProjectionAontoB(self, A, B): #~ print(A,B) normB = B.copy() normB.normalize() # unit vector, IN PLACE (must separately) AdotnormB = A.dot_product(normB) C = normB.__mul__(AdotnormB) return C def begin_round(self): global j1, j2, pend, scene, pole, ball, grabScreens, frNum global pauseState damping = 0.1 if (pauseState == 1): self.next() return # Calls the super implementation. soya.World.begin_round(self) #~ print(j1.getFeedback(), "#", j2.getFeedback()) # (force1, torque1, force2, torque2) : ((0.0, 332.9216613769531, -257.0126647949219), (180.77870178222656, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)) ..... (((0.0, 426.10772705078125, -1055.581298828125), (-5704.71435546875, 0.0, 0.0), (1.1854985008187952e-42, 2.312142466135948e-43, 9.164491956684304e-43), (1.4461400151832112e-42, 7.777206477002735e-43, 7.903323338791968e-43)), '#', ((0.0, 173.71861267089844, -6280.154296875), (-96.6642074584961, 0.0, 0.0), (0.0, -173.71861267089844, 6280.154296875), (-504.1296081542969, 0.0, 0.0))) self.j1f = j1.getFeedback() self.j2f = j2.getFeedback() frc1 = self.j2f[0] #[0] # note - vector should be referenced to scene, NOT pend, # for the damping to work! dampvect = Vector(scene, -damping * frc1[0], -damping * frc1[1], -damping * frc1[2]) pend.add_force(dampvect) #~ j1fp = j1.getFeedback() # same as j1f #~ print(frc1, "#", j1fp[0]) self.dj1fp = Vector(scene, frc1[0], frc1[1], frc1[2]) + dampvect #~ print(frc1, "#", self.dj1fp) #~ print(bp) #~ frep=bp+0.1*self.dj1fp # float required; no parse #~ frep=bp.add_mul_vector(0.1, self.dj1fp) # awful #~ frep=bp+self.dj1fp.__mul__(0.000001) # ?? #~ frep=Vector(ball,0,0,2) # ref to ball don't seem to matter! # AH - for these kind of transform with convert_to, should have Point, not Vector! # frcpball coord syst: 2 units in each direction, moved 1 unit along x so as not to be hidden by ball movx = 0 if (self.coordViewState == 0): self.bp = ball.position().add_vector(soya.Vector(ball, movx, 0, 0)) self.bp1 = self.bp self.bp2 = self.bp1 if (self.coordViewState == 1): movx = 1 self.bp = ball.position().add_vector(soya.Vector(ball, movx, 0, 0)) self.bp1 = self.bp self.bp2 = self.bp1 elif (self.coordViewState == 2): movx = 1 self.bp = ball.position().add_vector(soya.Vector(ball, movx, 0, 0)) self.bp1 = ball.position().add_vector(soya.Vector(ball, 2, 0, 1)) self.bp2 = self.bp1 elif (self.coordViewState == 3): movx = 1 self.bp = ball.position().add_vector(soya.Vector(ball, movx, 0, 0)) self.bp1 = ball.position().add_vector(soya.Vector(ball, 2, 0, 1)) self.bp2 = ball.position().add_vector(soya.Vector(ball, 3, 0, 2)) self.bp1b = self.bp1.copy().add_vector(soya.Vector(ball, -0.5, 0, 0)) self.frepx = Point(ball, 2 + movx, 0, 0) self.frepy = Point(ball, movx, 2, 0) self.frepz = Point(ball, movx, 0, 2) self.frepx.convert_to(scene) self.frepy.convert_to(scene) self.frepz.convert_to(scene) # scaled so they're approx the same scale_grav = scene.gravity.__mul__(0.5) scale_dfor = self.dj1fp.__mul__(0.03) self.epg = self.bp1 + scale_grav self.epfor = self.bp2 + scale_dfor tp = Vector(ball, 2, 0, 0) # convert_to must go separate! can be vector tp.convert_to(scene) # IN PLACE grav_lx = self.vecProjectionAontoB(scale_grav, tp) tp = Vector(ball, 0, 2, 0) tp.convert_to(scene) grav_ly = self.vecProjectionAontoB(scale_grav, tp) tp = Vector(ball, 0, 0, 2) tp.convert_to(scene) grav_lz = self.vecProjectionAontoB(scale_grav, tp) self.epgx = self.bp1 + grav_lx self.epgy = self.bp1 + grav_ly self.epgz = self.bp1 + grav_lz tp = Vector(ball, 2, 0, 0) tp.convert_to(scene) dfor_lx = self.vecProjectionAontoB(scale_dfor, tp) tp = Vector(ball, 0, 2, 0) tp.convert_to(scene) dfor_ly = self.vecProjectionAontoB(scale_dfor, tp) tp = Vector(ball, 0, 0, 2) tp.convert_to(scene) dfor_lz = self.vecProjectionAontoB(scale_dfor, tp) self.epforx = self.bp2 + dfor_lx self.epfory = self.bp2 + dfor_ly self.epforz = self.bp2 + dfor_lz tp = Vector(scene, 2, 0, 0) grav_lz_mx = self.vecProjectionAontoB(grav_lz, tp) tp = Vector(scene, 0, 2, 0) grav_lz_my = self.vecProjectionAontoB(grav_lz, tp) tp = Vector(scene, 0, 0, 2) grav_lz_mz = self.vecProjectionAontoB(grav_lz, tp) self.epglzx = self.bp1b + grav_lz_mx self.epglzy = self.bp1b + grav_lz_my self.epglzz = self.bp1b + grav_lz_mz if (grabScreens): frNum += 1 tfname = "/dev/shm/soya-pend%05d.png" % (frNum) soya.screenshot(filename=tfname, use_back_buffer=False) #~ print(ball.position()) # Computes the new rotation speed: a random angle between -25.0 and 25.0 degrees. #~ self.rotation_speed = random.uniform(-25.0, 25.0) # The speed vector doesn't need to be recomputed, since it is expressed in the Head # CoordSyst. def advance_time(self, proportion): global z_ax_glob, camera, v_orig, scene, pole, ball global frcpball, vecgrav, vecfor, vecgravlp, vecforlp, vecgravlzMp global pauseState yrotaxVect = soya.Vector(scene, 0, 1, 0) if (pauseState == 1): self.next() return # Calls the super implementation of advance_time. soya.World.advance_time(self, proportion) frcpball[0].set_xyz(self.bp.x, self.bp.y, self.bp.z) frcpball[0].endp = (self.frepx.x, self.frepx.y, self.frepx.z) frcpball[1].set_xyz(self.bp.x, self.bp.y, self.bp.z) frcpball[1].endp = (self.frepy.x, self.frepy.y, self.frepy.z) frcpball[2].set_xyz(self.bp.x, self.bp.y, self.bp.z) frcpball[2].endp = (self.frepz.x, self.frepz.y, self.frepz.z) vecgrav.set_xyz(self.bp1.x, self.bp1.y, self.bp1.z) vecgrav.endp = (self.epg.x, self.epg.y, self.epg.z) vecfor.set_xyz(self.bp2.x, self.bp2.y, self.bp2.z) vecfor.endp = (self.epfor.x, self.epfor.y, self.epfor.z) vecgravlp[0].set_xyz(self.bp1.x, self.bp1.y, self.bp1.z) vecgravlp[0].endp = (self.epgx.x, self.epgx.y, self.epgx.z) vecgravlp[1].set_xyz(self.bp1.x, self.bp1.y, self.bp1.z) vecgravlp[1].endp = (self.epgy.x, self.epgy.y, self.epgy.z) vecgravlp[2].set_xyz(self.bp1.x, self.bp1.y, self.bp1.z) vecgravlp[2].endp = (self.epgz.x, self.epgz.y, self.epgz.z) vecforlp[0].set_xyz(self.bp2.x, self.bp2.y, self.bp2.z) vecforlp[0].endp = (self.epforx.x, self.epforx.y, self.epforx.z) vecforlp[1].set_xyz(self.bp2.x, self.bp2.y, self.bp2.z) vecforlp[1].endp = (self.epfory.x, self.epfory.y, self.epfory.z) vecforlp[2].set_xyz(self.bp2.x, self.bp2.y, self.bp2.z) vecforlp[2].endp = (self.epforz.x, self.epforz.y, self.epforz.z) if (self.coordMpState == 1): vecgravlzMp[0].visible = 1 vecgravlzMp[1].visible = 1 vecgravlzMp[2].visible = 1 vecgravlzMp[0].set_xyz(self.bp1b.x, self.bp1b.y, self.bp1b.z) vecgravlzMp[0].endp = (self.epglzx.x, self.epglzx.y, self.epglzx.z) vecgravlzMp[1].set_xyz(self.bp1b.x, self.bp1b.y, self.bp1b.z) vecgravlzMp[1].endp = (self.epglzy.x, self.epglzy.y, self.epglzy.z) vecgravlzMp[2].set_xyz(self.bp1b.x, self.bp1b.y, self.bp1b.z) vecgravlzMp[2].endp = (self.epglzz.x, self.epglzz.y, self.epglzz.z) elif (self.coordMpState == 0): vecgravlzMp[0].visible = 0 vecgravlzMp[1].visible = 0 vecgravlzMp[2].visible = 0 # Rotates the object around Y axis. #~ self.rotate_y(proportion * 2.0) #~ z_ax_glob.x += proportion*0.1; #~ camera.rotate_z(proportion) # rotate_axis - vector via 0,0,0; but local # rotate is ok - around a point; not even look_at needed! self.next() if (self.right_key_down): camera.rotate(proportion, Point(scene, 0, 0, 0), yrotaxVect) camera.look_at(pole) #(v_orig) elif (self.left_key_down): camera.rotate(-proportion, Point(scene, 0, 0, 0), yrotaxVect) camera.look_at(pole) #(v_orig) elif (self.up_key_down): # translate camera.add_mul_vector(0.5 * proportion, Vector(camera, 0, 0, -1)) camera.look_at(pole) #(v_orig) elif (self.down_key_down): # translate camera.add_mul_vector(0.5 * proportion, Vector(camera, 0, 0, 1)) camera.look_at(pole) #(v_orig)