def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): if platform.system() != 'Darwin': dy = -dy if keys[key.SPACE] or modifiers & pyglet.window.key.MOD_ALT: if buttons & mouse.LEFT: s = 0.0075 m = self.camera.matrix mi = self.camera.matrixinv globaly = Vector3(*mi[4:7]) xaxis = Vector3(1, 0, 0) d = abs(Vector3(*m[12:15]) - self.camera.center) offs = Vector3(0, 0, d) m *= Matrix4.new_translate(*(-offs)).rotate_axis( s * -dx, globaly).rotate_axis(s * -dy, xaxis).translate(*(offs)) if buttons & mouse.RIGHT: s = -0.05 m = self.camera.matrix dist = (Point3(*m[12:15]) - self.camera.center) * 0.1 # dist = abs(dist) zaxis = Vector3(0, 0, 1) tx = zaxis * s * dx + zaxis * s * dx * dist m.translate(*tx) #self.camera.center -= zaxis*s*dx if buttons & mouse.MIDDLE: s = -0.01 m = self.camera.matrix dist = (Point3(*m[12:15]) - self.camera.center) * 0.3 dist = abs(dist) xaxis = Vector3(1, 0, 0) yaxis = Vector3(0, 1, 0) trans = Matrix4.new_translate(*(xaxis * s * dx * dist)).translate(*(yaxis * s * -dy * dist)) m *= trans self.camera.center = trans * self.camera.center self.camera.update()
def do_transformation_matrix(self): if self.view.up.y == 1.: # Obj's are exported with z axis up (0,0,1), so we need to # rotate x for -90 degrees to align with view's y up axis (0,1,0) self.orient.rotatex(pi / -2.) ''' Apply transformations in following order (translation -> -pivot -> orientation -> pivot) in order to rotate around arbitrary pivot point and properly position the model. TODO: Optimize matrix concatenation (rewrite in compact form) ''' self.mtransform = Matrix4.new_translate(*self.position) * Matrix4.new_translate(*(self.pivot * -1.)) * \ self.morient * Matrix4.new_translate(*self.pivot) # Create ctype array of the matrix self.m = (GLfloat * 16)(*self.mtransform)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): if platform.system() != 'Darwin': dy = -dy if keys[key.SPACE] or modifiers & pyglet.window.key.MOD_ALT: if buttons & mouse.LEFT: s = 0.0075 m = self.camera.matrix mi = self.camera.matrixinv globaly = Vector3(*mi[4:7]) xaxis = Vector3(1,0,0) d = abs(Vector3(*m[12:15]) - self.camera.center) offs = Vector3(0,0,d) m *= Matrix4.new_translate(*(-offs)).rotate_axis(s*-dx, globaly).rotate_axis(s*-dy, xaxis).translate(*(offs)) if buttons & mouse.RIGHT: s = -0.05 m = self.camera.matrix dist = (Point3(*m[12:15]) - self.camera.center) * 0.1 # dist = abs(dist) zaxis = Vector3(0,0,1) tx = zaxis*s*dx + zaxis*s*dx*dist m.translate(*tx) #self.camera.center -= zaxis*s*dx if buttons & mouse.MIDDLE: s = -0.01 m = self.camera.matrix dist = (Point3(*m[12:15]) - self.camera.center) * 0.3 dist = abs(dist) xaxis = Vector3(1,0,0) yaxis = Vector3(0,1,0) trans = Matrix4.new_translate(*(xaxis*s*dx*dist)).translate(*(yaxis*s*-dy*dist)) m *= trans self.camera.center = trans * self.camera.center self.camera.update()
def add(self, shape, position=(0, 0, 0), orientation=None): matrix = Matrix4.new_translate(*position) if orientation is not None: matrix *= orientation child_offset = len(self.vertices) self.vertices.extend(self.child_vertices(shape, matrix)) self.faces.extend(self.child_faces(shape, child_offset))
def internal_tick_callback(self, timeStep): if self._cue_ball_rest(): disp = self.world.getDispatcher() n = disp.getNumManifolds() for i in xrange(n): cm = disp.getManifoldByIndexInternal(i) contacts = cm.getNumContacts() for c in xrange(contacts): obA = cm.getBody0().getUserPointer() obB = cm.getBody1().getUserPointer() if obA == 'Cue' and obB == 'Cue Ball' \ or obA == 'Cue Ball' and obB == 'Cue': p = cm.getContactPoint(c) if p.getDistance() < 0.0: # test for penetration # The cue's strike force is calculated from actual # linear velocity applied to the cues orientation vector b = self.cue.body.getLinearVelocity() v = eVector3(b.x, b.y, b.z) d = self.cue.direction if hasattr(self.cue, 'direction') \ else self.view.dir # get force impuls = v.magnitude() * SCALE_FACTOR * 25. force = bVector3(d.x * impuls, d.y * impuls, 0.0) # span offset to [0..1] offset = eVector3(self.cue.dx * 4., 0., self.cue.dy * 4.) # calculate offset from cue's position [convert from eye into object space] # TODO: (rewrite this in compact form of vector vs. matrix multiplication) moffset = self.view.orient.inverse() * Matrix4.new_translate(*offset) offset = eVector3(moffset.d, moffset.h, moffset.l) cue = self.ball['cue'] cue.body.clearForces() cue.body.applyGravity() cue.body.applyForce(force, bVector3(offset.x, offset.y, offset.z)) # Restore cue self._reset_cue() def ball_cant_fly(ball): # check for flying pos = ball.motion.getWorldTransform().getOrigin() if pos.z > ball.radius: ball.body.applyCentralForce(bVector3(0, 0, -15 * SCALE_FACTOR)) # check for ball speed v = ball.body.getLinearVelocity() vel = eVector3(v.x, v.y, v.z) if vel.magnitude_squared() > PoolWindow.BALL_MAX_SPEED: ball.body.setLinearVelocity(v * 0.9) for ball in self.ball.values(): ball_cant_fly(ball)
def on_draw(): a = 1.6 b = a * sqrt(3) / 3 r = sqrt(1 - b*b) R = Vector3(a, a, a).normalized() * b cs = Matrix4.new_translate(R[0], R[1], R[2]) * Matrix4.new_look_at(Vector3(0, 0, 0), R, Vector3(0, 1, 0)) glDisable(GL_DEPTH_TEST) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() draw_coords(Matrix4.new_identity(), 2) draw_line((0, 0, 0), R, (1, 1, 0)) draw_coords(cs, r, 3) glEnable(GL_DEPTH_TEST) draw_triangle((a, 0, 0), (0, a, 0), (0, 0, a), (.5, .5, 1), .2) draw_sphere(1, (1, .5, .5), .2)
def get_view_matrix(self): """Get the view matrix as a euclid.Matrix4.""" f = (v3(self.look_at) - v3(self.pos)).normalized() #print "f=", f up = Vector3(0, 1, 0) s = f.cross(up).normalize() #print abs(f), abs(up), abs(s) #print "s=", s u = s.cross(f) m = Matrix4.new( *itertools.chain(s, [0], u, [0], -f, [0], [0, 0, 0, 1])) #print m xlate = Matrix4.new_translate(*(-self.pos)) #print xlate mat = m.inverse() * xlate #print mat return mat
def on_draw(): a = 1.6 b = a * sqrt(3) / 3 r = sqrt(1 - b * b) R = Vector3(a, a, a).normalized() * b cs = Matrix4.new_translate(R[0], R[1], R[2]) * Matrix4.new_look_at( Vector3(0, 0, 0), R, Vector3(0, 1, 0)) glDisable(GL_DEPTH_TEST) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() draw_coords(Matrix4.new_identity(), 2) draw_line((0, 0, 0), R, (1, 1, 0)) draw_coords(cs, r, 3) glEnable(GL_DEPTH_TEST) draw_triangle((a, 0, 0), (0, a, 0), (0, 0, a), (.5, .5, 1), .2) draw_sphere(1, (1, .5, .5), .2)
def test_sphere_transform(): """We can transform a sphere.""" m = Matrix4.new_translate(0, 0, 5) assert Sphere(radius=2).transformed(m) == Sphere(Point3(0, 0, 5), 2)
def matrix(self): # stupid rotate_euler taking coords out of order! return Matrix4.new_translate(*self.translate.value).rotate_euler(*self.rotate.value.yzx).scale(*self.scale.value)
def get_matrix(self): r = self.rot.get_matrix() t = Matrix4.new_translate(*self.pos) return t * r