def update_neurons(self): while True: scale=0.0003 m1=controlU.getOrigin('u1').getValues().getValues()[0]*scale m2=controlU.getOrigin('u2').getValues().getValues()[0]*scale v1=Vector3f(0,0,0) v2=Vector3f(0,0,0) #java.lang.System.out.println("m1: %f m2:%f"%(m1,m2)) self.upperarm.physics.applyTorqueImpulse(Vector3f(0,0,m1)) self.lowerarm.physics.applyTorqueImpulse(Vector3f(0,0,m2)) self.hinge1=-(self.shoulder.hingeAngle-pi/2) self.hinge2=-self.elbow.hingeAngle #java.lang.System.out.println("angle1: %f #angle2:%f"%(self.hinge1,self.hinge2)) self.upperarm.physics.getAngularVelocity(v1) self.lowerarm.physics.getAngularVelocity(v2) # put bounds on the velocity possible if v1.z > 2: self.upperarm.physics.setAngularVelocity(Vector3f(0,0,2)) if v1.z < -2: self.upperarm.physics.setAngularVelocity(Vector3f(0,0,-2)) if v2.z > 2: self.lowerarm.physics.setAngularVelocity(Vector3f(0,0,2)) if v2.z < -2: self.lowerarm.physics.setAngularVelocity(Vector3f(0,0,-2)) self.upperarm.physics.getAngularVelocity(v1) self.lowerarm.physics.getAngularVelocity(v2) wt=Transform() #self.target.physics.motionState.getWorldTransform(wt) wt.setIdentity() tx=controlV.getTermination('inputRefX').input if tx is not None: wt.origin.x=tx.values[0]+0.7 else: wt.origin.x=0.7 ty=controlV.getTermination('inputRefY').input if ty is not None: wt.origin.y=ty.values[0]+0.1 else: wt.origin.y=0.1 wt.origin.z=2 ms=self.target.physics.motionState ms.worldTransform=wt self.target.physics.motionState=ms self.vel1=v1.z self.vel2=v2.z yield 0.0001
def __init__(self, filename, texture, scale=1.0, mass=1, overdraw_scale=1.0): if not TextureManager.getInstance().containsTexture(texture): TextureManager.getInstance().addTexture(texture, Texture(texture)) if (filename, scale) not in MD2._shapes: shape = Loader.loadMD2(filename, scale) shape.rotateAxis(SimpleVector(1, 0, 0), math.pi/2) shape.rotateAxis(SimpleVector(0, 0, 1), -math.pi/2) shape.rotateMesh() shape.rotationMatrix = Matrix() MD2._shapes[(filename, scale)] = shape # self.shape = MD2._shapes[(filename, scale)].cloneObject() self.shape = Object3D(MD2._shapes[(filename, scale)], False) self.shape.build() self.shape.setTexture(texture) minx, maxx, miny, maxy, minz, maxz = self.shape.mesh.boundingBox shape = BoxShape(Vector3f((maxx - minx) / overdraw_scale / 2.0, (maxy - miny) / overdraw_scale / 2.0, (maxz - minz) / 2.0)) t = Transform() t.setIdentity() t.origin.x = (maxx + minx) / 2 t.origin.y = (maxy + miny) / 2 t.origin.z = (maxz + minz) / 2 ms = DefaultMotionState(t) inertia = Vector3f(0, 0, 0) shape.calculateLocalInertia(mass, inertia) rb = RigidBodyConstructionInfo(mass, ms, shape, inertia) self.physics = RigidBody(rb) self.shape.setOrigin(SimpleVector(-(maxx + minx) / 2, -(maxy + miny) / 2, -(maxz + minz) / 2))
def update_neurons(self): while True: scale = 0.0003 m1 = controlU.getOrigin('u1').getValues().getValues()[0] * scale m2 = controlU.getOrigin('u2').getValues().getValues()[0] * scale v1 = Vector3f(0, 0, 0) v2 = Vector3f(0, 0, 0) #java.lang.System.out.println("m1: %f m2:%f"%(m1,m2)) self.upperarm.physics.applyTorqueImpulse(Vector3f(0, 0, m1)) self.lowerarm.physics.applyTorqueImpulse(Vector3f(0, 0, m2)) self.hinge1 = -(self.shoulder.hingeAngle - pi / 2) self.hinge2 = -self.elbow.hingeAngle #java.lang.System.out.println("angle1: %f #angle2:%f"%(self.hinge1,self.hinge2)) self.upperarm.physics.getAngularVelocity(v1) self.lowerarm.physics.getAngularVelocity(v2) # put bounds on the velocity possible if v1.z > 2: self.upperarm.physics.setAngularVelocity(Vector3f(0, 0, 2)) if v1.z < -2: self.upperarm.physics.setAngularVelocity(Vector3f(0, 0, -2)) if v2.z > 2: self.lowerarm.physics.setAngularVelocity(Vector3f(0, 0, 2)) if v2.z < -2: self.lowerarm.physics.setAngularVelocity(Vector3f(0, 0, -2)) self.upperarm.physics.getAngularVelocity(v1) self.lowerarm.physics.getAngularVelocity(v2) wt = Transform() #self.target.physics.motionState.getWorldTransform(wt) wt.setIdentity() tx = controlV.getTermination('inputRefX').input if tx is not None: wt.origin.x = tx.values[0] + 0.7 else: wt.origin.x = 0.7 ty = controlV.getTermination('inputRefY').input if ty is not None: wt.origin.y = ty.values[0] + 0.1 else: wt.origin.y = 0.1 wt.origin.z = 2 ms = self.target.physics.motionState ms.worldTransform = wt self.target.physics.motionState = ms self.vel1 = v1.z self.vel2 = v2.z yield 0.0001
def __init__(self, r, mass=1, color=Color.red): colorname = 'sphere_%d' % id(self) self.shape = Primitives.getSphere(r) TextureManager.getInstance().addTexture( colorname, Texture(1, 1, color)) self.shape.setTexture(colorname) self.shape.build() shape = BoxShape(Vector3f(r / 2.0, r / 2.0, r / 2.0)) inertia = Vector3f(0, 0, 0) shape.calculateLocalInertia(mass, inertia) t = Transform() t.setIdentity() ms = DefaultMotionState(t) rb = RigidBodyConstructionInfo(mass, ms, shape, inertia) self.physics = RigidBody(rb)
def start(self): while True: physics = self.parent.parent.physics t = Transform() self.parent.physics.getWorldTransform(t) pt1 = Vector3f(self.origin) pt2 = Vector3f(self.dir) t.transform(pt1) t.transform(pt2) callback = CollisionWorld.ClosestRayResultCallback(pt1, pt2) physics.rayTest(pt1, pt2, callback) delta = Vector3f(callback.hitPointWorld) delta.sub(pt1) self.range = delta.length() yield self.timestep
def physics_dump(self): d = {} for obj in self.objects: if hasattr(obj, 'physics'): t = Transform() obj.physics.getMotionState().getWorldTransform(t) m = Matrix4f() t.getMatrix(m) trans = Matrix() trans.set(3, 0, m.m03) trans.set(3, 1, m.m13) trans.set(3, 2, m.m23) rot = Matrix() for i in range(3): for j in range(3): rot.set(i, j, m.getElement(j, i)) d[obj] = (trans, rot) return d
def __init__(self, x, y, z, mass=1, scale=1, draw_as_cylinder=False, color=Color.blue, overdraw_length=1, overdraw_radius=1, flat_shading=False): if draw_as_cylinder: if y is max(x, y, z): radius = (x + z) / 4 * overdraw_radius self.shape = Primitives.getCylinder( 90, radius, y / (radius * 2) * overdraw_length) if z is max(x, y, z): radius = (x + y) / 4 * overdraw_radius self.shape = Primitives.getCylinder( 90, radius, z / (radius * 2) * overdraw_length) self.shape.rotateX(math.pi / 2) self.shape.rotateMesh() else: self.shape = createBox(x / 2.0 * scale, y / 2.0 * scale, z / 2.0 * scale) if flat_shading: self.shape.shadingMode = Object3D.SHADING_FAKED_FLAT colorname = 'box%d' % id(self) TextureManager.getInstance().addTexture(colorname, Texture(1, 1, color)) self.shape.setTexture(colorname) self.shape.build() shape = BoxShape(Vector3f(x / 2.0, y / 2.0, z / 2.0)) inertia = Vector3f(0, 0, 0) shape.calculateLocalInertia(mass, inertia) t = Transform() t.setIdentity() ms = DefaultMotionState(t) rb = RigidBodyConstructionInfo(mass, ms, shape, inertia) self.physics = RigidBody(rb)
def add(self, obj, x, y, z=None): if z is None: t = Transform() min = Vector3f() max = Vector3f() obj.physics.getAabb(min, max) z = -min.z self.temp = obj if hasattr(obj, 'physics'): t = Transform() ms = obj.physics.motionState ms.getWorldTransform(t) m = Matrix4f() t.getMatrix(m) m.m03 = x m.m13 = y m.m23 = z t.set(m) ms.worldTransform = t obj.physics.motionState = ms self.physics.addRigidBody(obj.physics) if hasattr(obj, 'wheels'): tuning = VehicleTuning() obj.vehicle = RaycastVehicle( tuning, obj.physics, self.vehicle_raycaster) obj.physics.setActivationState( CollisionObject.DISABLE_DEACTIVATION) self.physics.addVehicle(obj.vehicle) obj.vehicle.setCoordinateSystem(1, 2, 0) for w in obj.wheels: wheel = obj.vehicle.addWheel(Vector3f(w.x, w.y, w.z), Vector3f(w.dir), Vector3f(w.axle), w.suspension_rest_len, w.radius, tuning, False) wheel.suspensionStiffness = w.suspension_stiffness wheel.maxSuspensionTravelCm = w.suspension_max_travel * 100 wheel.frictionSlip = w.friction wheel.wheelsDampingRelaxation = w.damping_relaxation wheel.wheelsDampingCompression = w.damping_compression wheel.rollInfluence = w.roll_influence self.objects.append(obj) self.world.addObject(obj.shape)
def __init__(self, x, y, z=1, gravity=10, color=[ Color(0xFFFFFF), Color(0xFFFFFF), Color(0xEEEEEE), Color(0xDDDDDD), Color(0xCCCCCC), Color(0xBBBBBB)], camera=(0, 0, 10), dt=0.0001): ccm.Model.__init__(self) self.size = (x, y, z) self.world = World() self.dt = dt brightness = 180 self.world.setAmbientLight(brightness, brightness, brightness) self.world.addLight(SimpleVector(2, 2, 5), 10, 10, 10) self.world.camera.setPosition(*camera) self.world.camera.setOrientation( SimpleVector(0, 0, -1), SimpleVector(0, -1, 0)) self.objects = [] collisionConfiguration = DefaultCollisionConfiguration() dispatcher = CollisionDispatcher(collisionConfiguration) overlappingPairCache = AxisSweep3( Vector3f(-x / 2.0, -y / 2.0, -z / 2.0), Vector3f(x / 2.0, y / 2.0, z / 2.0), 1024) solver = SequentialImpulseConstraintSolver() self.physics = DiscreteDynamicsWorld( dispatcher, overlappingPairCache, solver, collisionConfiguration) self.physics.setGravity(Vector3f(0, 0, -gravity)) t = Transform() t.setIdentity() rbi = RigidBodyConstructionInfo( 0, DefaultMotionState(t), StaticPlaneShape( Vector3f(0, 0, 1), 0), Vector3f(0, 0, 0)) self.physics.addRigidBody(RigidBody(rbi)) rbi = RigidBodyConstructionInfo( 0, DefaultMotionState(t), StaticPlaneShape( Vector3f(0, 1, 0), -y / 2.0), Vector3f(0, 0, 0)) self.physics.addRigidBody(RigidBody(rbi)) rbi = RigidBodyConstructionInfo( 0, DefaultMotionState(t), StaticPlaneShape( Vector3f(0, -1, 0), -y / 2.0), Vector3f(0, 0, 0)) self.physics.addRigidBody(RigidBody(rbi)) rbi = RigidBodyConstructionInfo( 0, DefaultMotionState(t), StaticPlaneShape( Vector3f(1, 0, 0), -x / 2.0), Vector3f(0, 0, 0)) self.physics.addRigidBody(RigidBody(rbi)) rbi = RigidBodyConstructionInfo( 0, DefaultMotionState(t), StaticPlaneShape( Vector3f(-1, 0, 0), -x / 2.0), Vector3f(0, 0, 0)) self.physics.addRigidBody(RigidBody(rbi)) self.vehicle_raycaster = DefaultVehicleRaycaster(self.physics) if not isinstance(color, (list, tuple)): room = createBox(x / 2.0, y / 2.0, z / 2.0) room.translate(0, 0, z / 2.0) room.invert() colorname = 'room%d' % id(self) TextureManager.getInstance().addTexture( colorname, Texture(1, 1, color)) room.setTexture(colorname) room.shadingMode = Object3D.SHADING_FAKED_FLAT room.build() self.world.addObject(room) else: room = createBoxPieces(x / 2.0, y / 2.0, z / 2.0) for i, r in enumerate(room): r.translate(0, 0, z / 2.0) r.invert() colorname = 'room%d_%d' % (id(self), i) TextureManager.getInstance().addTexture( colorname, Texture(1, 1, color[i % len(color)])) r.setTexture(colorname) r.shadingMode = Object3D.SHADING_FAKED_FLAT r.build() self.world.addObject(r) self.ratelimit = RateLimit()