当前位置: 首页>>代码示例>>Python>>正文


Python Vector.norm方法代码示例

本文整理汇总了Python中vector.Vector.norm方法的典型用法代码示例。如果您正苦于以下问题:Python Vector.norm方法的具体用法?Python Vector.norm怎么用?Python Vector.norm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在vector.Vector的用法示例。


在下文中一共展示了Vector.norm方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: move

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import norm [as 别名]
    def move(self, directions, *args):
        """ Move around in 3D space using the keyboard.
        Takes an array containing X , Y and Z axis directions.
        Directions must be either 1, -1 or 0."""
        acceleration = args[0]
        direction = Vector(directions)

        if direction.norm():
            direction = direction.normalize() * self._speed

        self._velocity = self._velocity + acceleration

        # Calculate new position
        movementDir = self._velocity + direction
        movement = movementDir.get_value()
        self._xPos += movement[0]
        self._yPos += movement[1]
        self._zPos += movement[2]
        return movementDir
开发者ID:axelri,项目名称:fluffy_spheres,代码行数:21,代码来源:shapes.py

示例2: Car

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import norm [as 别名]

#.........这里部分代码省略.........
            self.sprite.position = self.position.pair()
            self.last_ground = self.position

    def move(self, new_position):
        """move the car"""
        path = bresenham(self.position, new_position)
        for i, c in enumerate(path):
            if not self.state.aerial:  # a flying car can move over anything
                if self.race.track.type(c, hit=True) is WALL:
                    # hit a wall
                    if i == 0:
                        # car is stuck in a wall
                        return self.lakitu()
                    else:
                        # the car bounces against the wall
                        if path[i - 1][0] != c[0]:
                            self.set_speed(Vector(-0.5 * self.speed.x, 0.5 * self.speed.y))
                        else:
                            self.set_speed(Vector(0.5 * self.speed.x, -0.5 * self.speed.y))
                        new_position = self.position  # do not move
                    break
                if not self.state.jump:
                    if self.race.track.type(c) is DEEP:
                        # car is in a hole
                        return self.lakitu()
                    if self.racer.item is ITEMS[0] and self.race.track.type(c, special=True) is ITEM:
                        # the car touched an item block
                        block = self.race.track.item_blocks[(c[0], c[1])]  # find the block that was touched
                        if not block.delay:
                            block.activate()
                            self.racer.get_item()
                    if self.race.track.type(c) is JUMP:
                        # touch a bumper
                        if self.speed.norm() < 100:
                            self.set_speed(self.speed.normalize(100.0))
                        self.state.change(aerial=1.0)
                    if self.race.track.type(c) is BOOST and self.speed.norm() < 300.0:
                        # touch a booster
                        self.set_speed(self.speed.normalize(300.0))
        self.set_position(new_position)

    def stop(self):
        """stops the car by killing its speed"""
        self.set_speed(Vector(0.0, 0.0))

    def set_speed(self, new_speed):
        """changes the speed of the car"""
        self.speed = new_speed

    def get_direction_vector(self):
        """returns the orientation of the car as a unit vector"""
        return Vector(math.cos(self.direction), math.sin(self.direction))

    def set_direction(self, new_direction):
        """changes the orientation of the car"""
        self.direction = new_direction % (2 * math.pi)
        self.sprite.image = self.choose_sprite()  # update the sprite of the car

    def collision(self, car):
        """simulate an elastic collision between two cars"""
        direction_p = (car.position - self.position).normalize(1.0)
        direction_o = Vector(-direction_p.y, direction_p.x)
        u1p, u1o = self.speed * direction_p, self.speed * direction_o
        u2p, u2o = car.speed * direction_p, car.speed * direction_o
        if u1p > u2p:
            m1, m2 = self.mass(), car.mass()
开发者ID:zanapher,项目名称:MicroKart,代码行数:70,代码来源:car.py

示例3: Surface

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import norm [as 别名]
class Surface(Shape):
    """ Defines a surface """

    def __init__(
        self,
        length=constants.SURFACE_SIZE,
        width=constants.SURFACE_SIZE,
        center=[0.0, constants.GROUND_LEVEL, 0.0],
        normal=Vector("e_y"),
        color=constants.SURFACE_COLOR,
        friction=constants.FRICTION,
    ):
        self._color = color
        self._length = length  # (if normal is 'e_y', this is size in z-direction)
        self._width = width  # (if normal is 'e_y', this is size in x-direction)
        self._normal = normal
        self._friction = friction
        if self._normal.norm() != 1.0:
            if self._normal.norm() == 0.0:
                raise Exception("The normal cannot be a zero vector!")
            self._normal = self._normal.normalize()
        self._center = center
        # The direction in which the "length will be drawn"
        self._lengthDir = Vector("e_x").cross(self._normal)
        if self._lengthDir.norm() == 0.0:  # The normal is parallell to 'e_x'
            self._lengthDir = Vector("e_z")
        # The direction in which the "width will be drawn"
        self._widthDir = self._normal.cross(Vector("e_z"))
        if self._widthDir.norm() == 0.0:  # The normal is parallell to 'e_z'
            self._widthDir = Vector("e_x")

        # The "absolute value of the length-position of the points"
        lengthPos = self._lengthDir * self._length
        # The "absolute value of the width-position of the points"
        widthPos = self._widthDir * self._width

        self._initPoints = [
            ((lengthPos + widthPos) * -1.0).get_value(),
            (widthPos - lengthPos).get_value(),
            (lengthPos + widthPos).get_value(),
            (lengthPos - widthPos).get_value(),
        ]

        # TODO: Quite ugly solution, fix?
        self._points = self._initPoints
        super(Surface, self).__init__()

        self.set_center(self._center)

        self.update_points()

    def draw_shape(self):
        glBegin(GL_QUADS)
        glColor4fv(self._color)
        glNormal3fv(self._normal.get_value())
        for point in self._points:
            glVertex3fv(point)
        glEnd()

    def update_points(self):
        """ Updates the surfaces points to the current location of the surface """
        self._points = [
            (Vector(self._initPoints[0]) + Vector(self._center)).get_value(),
            (Vector(self._initPoints[1]) + Vector(self._center)).get_value(),
            (Vector(self._initPoints[2]) + Vector(self._center)).get_value(),
            (Vector(self._initPoints[3]) + Vector(self._center)).get_value(),
        ]

        # TODO: Remove? Do we need them?
        self._edges = [
            [self._points[0], self._points[1]],
            [self._points[1], self._points[2]],
            [self._points[2], self._points[3]],
            [self._points[3], self._points[0]],
        ]

    def get_points(self):
        return self._points

    def get_edges(self):
        return self._edges

    def get_normal(self):
        return self._normal

    def get_surface_vectors(self):
        return [self._widthDir, self._normal, self._lengthDir]

    def get_size(self):
        return [self._length, self._width]

    def get_friction(self):
        return self._friction
开发者ID:axelri,项目名称:fluffy_spheres,代码行数:95,代码来源:shapes.py

示例4: Protagonist

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import norm [as 别名]
class Protagonist(object):
	def __init__(self, texture, program, x, y, position, collisionMap, mesh):
		super(Protagonist, self).__init__()
		self.tex = texture
		self.prog = program
		self.x = float(x)
		self.y = float(y)
		self.cooldown = 0.0
		self.collisionMap = collisionMap
		self.health = 100
		self.velocity = Vector(0.0, 0.0, 0.0)
		self.position = Vector(position[0], self.y/2, position[1])
		self.maxSpeed = 4
		self.modelview = Matrix().translate(position[0], self.y/2, position[1])
		self.moved = False
		self.angle = 0
		self.mesh = mesh
		with nested(self.tex):
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
	
	def update(self, delta, state):
		self.cooldown-= delta
		if self.moved:
			self.moved = False
		else:
			self.velocity = self.velocity * 0.8
			if self.velocity.len() < 0.1:
				self.velocity = Vector()
		facingVelocity = self.velocity.rotatey(self.angle)
		movedBy = facingVelocity * delta
		if not self.collisionMap.collision(self.position + movedBy, self.x/4):
			self.position = self.position + movedBy
			self.modelview = Matrix().translate(self.position.x, self.position.y, self.position.z) * Matrix().rotatey(-self.angle)
		
	def move(self, dvel):
		self.moved = True
		if self.velocity.len() == self.maxSpeed:
			return
		else:
			self.velocity = self.velocity + dvel
		if self.velocity.len() > self.maxSpeed:
			self.velocity.norm(self.maxSpeed)
			
	def teleport(self, x, y):
		self.position.x = x
		self.position.z = y
	
	def turn(self, angle):
		self.angle = self.angle + angle
		
	def point(self, angle):
		self.angle = angle
	
	def draw(self, projection, camera, player_position, r):
		self.prog.vars.mvp = projection * camera * self.modelview
		self.prog.vars.playerLight = r*0.1 + 0.9
		self.prog.vars.playerPosition = player_position.tuple()
		self.prog.vars.modelview = self.modelview
		with nested(self.prog, self.tex):
			self.mesh.draw(GL_QUADS)
			
	def hit(self):
		self.health -= 10
		
	def within(self, position):
		position = position - self.position
		
		for i in xfrange(0.0, 1.0, 0.05):
			point = self.position + (position * i)
			if self.collisionMap.collision(point, self.x/4):
				return False
		
		return True
		
	def depth(self, proj_cam):
		mat = proj_cam * self.modelview
		vector = mat.col(3)
		return -vector.z
		
开发者ID:seken,项目名称:nink,代码行数:81,代码来源:protagonist.py


注:本文中的vector.Vector.norm方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。