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


Python Vector.mod方法代码示例

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


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

示例1: Cavallo

# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import mod [as 别名]
class Cavallo():
    def __init__(self, pos = [130,0],vmax = 60., thePath=[]):
        self.currentNode = 0
        self.pathDir = 1
        self.nodes = thePath.getNodes()
        self.t = self.nodes[self.currentNode]
        self.max_v = vmax
        self.p = Vector(pos)
        self.v = Vector([0., 0.])
        self.max_see_ahead = 30
        self.max_avoid_force = 1.5
        self.obstacles = []

    def setObstacles(self, obs):
        self.obstacles = obs

    def updatePosition(self, dt):
        self.p = self.p + (self.v*dt)

    def collisionDetector(self):
        dynamic_length = self.v.mod()/self.max_v*self.max_see_ahead
        ahead = self.p + self.v.norm(dynamic_length)
        ahead2 = self.p + self.v.norm(dynamic_length*0.5)
        obstacle = self.takeOver(ahead, ahead2)
        avoidance = Vector()
 
        if (obstacle.p.coord() != [-1, -1]):
            avoidance = ahead-obstacle.p
            if (avoidance.x() == 0):
                avoidance = Vector([self.max_avoid_force, 0])
            elif (avoidance.y() == 0):
                avoidance = Vector([0, self.max_avoid_force])

        return avoidance

    def takeOver(self, ahead, ahead2):
        obstacle = Obstacle([-1, -1])
        min_distance = 9999.
        for o in self.obstacles:
            collision = self.lineIntersectsCircle(o, ahead, ahead2)
            if (o.p.coord() != self.p.coord()): # avoid self veto
                distance = o.p - self.p
                if (distance.dot(ahead) > 0.):
                    if (collision and distance.mod() < min_distance):
                        obstacle = o
                        min_distance = obstacle.p.distance(self.p)

        return obstacle
                
    def lineIntersectsCircle(self, o, ahead, ahead2):
        dist1 = o.p.distance(self.p)
        dist2 = o.p.distance(ahead2)
        return (o.p.distance(ahead) <= o.radius*1.5) or (dist2 <= o.radius*1.5) or (dist1<=(o.radius+15.))
 
    def steering(self):
        current_v = self.v.mod()
        desired_velocity = (self.t-self.p).norm(current_v)
        steering = (desired_velocity-self.v)
        avoid = self.collisionDetector()
        if (steering.mod() < avoid.mod()*2.5):
            steering = avoid
        else:
            steering = steering + avoid

        if (steering.mod() > 2.):
            steering = steering.norm(2.)

        # r = v*v/a_t
        #if (steering.transv(self.v) != 0):
            #r = self.v.mod()*self.v.mod()/steering.transv(self.v)
            #k = 0.5
            #print math.sqrt(k*r)
            
        self.v = self.v + steering
        self.v = self.v.norm(current_v)

    def move(self, dt):
        # FIXME SCEGLI STRATEGIA
        self.pathFollowing()
        self.speedUp()
        self.steering()
        self.updatePosition(dt)

    def speedUp(self):
        #FIXME assegna uno scatto
        current_v = self.v.mod()
        if (current_v == 0.):
            # deve cambiare con il tipo di mossa
            self.direzione = -0.7854
            self.v = Vector([math.cos(self.direzione), math.sin(self.direzione)])
        else:
            updated_v = current_v + 1.
            if (updated_v >= self.max_v):
                updated_v = self.max_v
            
            self.v = self.v.norm(updated_v)

    def pursuit(self):
        pass
        #public function pursuit(t :Boid) :Vector3D {
#.........这里部分代码省略.........
开发者ID:matteosan1,项目名称:python_code,代码行数:103,代码来源:steering2.py

示例2: Cavallo

# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import mod [as 别名]
class Cavallo():
    def __init__(self, currentNode, offset, thePath=[]):
        #FIXME Caratteristiche cavallo
        self.currentNode = currentNode+1
        self.pathDir = 1
        self.nodes = thePath.nodes
        self.t     = self.nodes[self.currentNode].waypoint
        self.max_v = self.nodes[self.currentNode].vmax
        #self.p = Vector(pos)
        self.p     = self.nodes[currentNode].waypoint+Vector(offset)
        self.v     = Vector([0., 0.])
        self.max_see_ahead = 30
        self.max_avoid_force = 1.5
        self.obstacles = []
        self.strategy = "mossa"

    def setObstacles(self, obs):
        self.obstacles = obs

    def updatePosition(self, dt):
        self.p = self.p + (self.v*dt)

    def collisionDetector(self):
        if (self.max_v != 0):
            dynamic_length = self.v.mod()/self.max_v*self.max_see_ahead
        else:
            return Vector()

        ahead = self.p + self.v.norm(dynamic_length)
        ahead2 = self.p + self.v.norm(dynamic_length*0.5)
        obstacle = self.takeOver(ahead, ahead2)
        avoidance = Vector()
 
        if (obstacle.p.coord() != [-1, -1]):
            avoidance = ahead-obstacle.p
            if (avoidance.x() == 0):
                avoidance = Vector([self.max_avoid_force, 0])
            elif (avoidance.y() == 0):
                avoidance = Vector([0, self.max_avoid_force])

        return avoidance

    def takeOver(self, ahead, ahead2):
        obstacle = Obstacle([-1, -1])
        min_distance = 9999.
        for o in self.obstacles:
            collision = self.lineIntersectsCircle(o, ahead, ahead2)
            if (o.p.coord() != self.p.coord()): # avoid self veto
                distance = o.p - self.p
                if (distance.dot(ahead) > 0.):
                    if (collision and distance.mod() < min_distance):
                        obstacle = o
                        min_distance = obstacle.p.distance(self.p)

        return obstacle
                
    def lineIntersectsCircle(self, o, ahead, ahead2):
        dist1 = o.p.distance(self.p)
        dist2 = o.p.distance(ahead2)
        return (o.p.distance(ahead) <= o.radius*2.) or (dist2 <= o.radius*2.) or (dist1<=(o.radius*2))
 
    def steering(self):
        current_v = self.v.mod()
        #print "POS", self.p.coord()
        #print "TAR", self.t.coord()
        
        desired_velocity = (self.t-self.p).norm(current_v)
        steering = (desired_velocity-self.v)
        avoid = self.collisionDetector()
        steering = steering + avoid

        if (steering.mod() > 2.5):
            steering = steering.norm(2.5)

        # r = v*v/a_t
        #if (steering.transv(self.v) != 0):
            #r = self.v.mod()*self.v.mod()/steering.transv(self.v)
            #k = 0.5
            #print math.sqrt(k*r)
            
        self.v = self.v + steering

    def thrust(self, slowDown=False):
        current_v = self.v.mod()
        #if (current_v == 0.):
        #    self.v = Vector([-1., -1.])
        if (current_v > self.max_v):
            if ((current_v - 0.5) <= 0):
                self.v = self.v.norm(0)
            else:
                self.v = self.v.norm(current_v - 0.5)
        elif (current_v < self.max_v):
            self.v = self.v.norm(current_v + 0.5)
        else:
            self.v = self.v.norm(current_v)

    def postoAlCanape(self, posto):
        self.nodes = copy.deepcopy(self.nodes)
        self.nodes[23].nextNode = 24
        self.nodes[-posto-3].vmax = 1
#.........这里部分代码省略.........
开发者ID:matteosan1,项目名称:python_code,代码行数:103,代码来源:Cavallo.py


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