Exemplo n.º 1
0
	def __init__(self, q = None, s = None, v = None, theta = None, v1 = None, v2 = None):
		
		if not q is None:
			# copy
			self.s = q.s
			self.v = q.v
		if not s is None:
			# components
			self.s = s
			self.v = np.array(v)
		elif (not v1 is None) and (not v2 is None):
			# rotation between two vectors
			v1 = vec.normalize(v1)
			v2 = vec.normalize(v2)
			self.v = np.cross(v1,v2)
			self.s = np.dot(v1,v2)
			self = self.normalize()
		elif not theta is None:
			# axis-angle
			self.s = math.cos(theta/2.0)
			self.v = np.array(v) * math.sin(theta/2.0)
		else:
			# zero
			self.s = 1.0
			self.v = np.zeros(3)

		if self.isnan():
			raise ValueError('Quat nan')
Exemplo n.º 2
0
    def calcAction(self):
        self.action.direction[0] = self.inputDevice.getX()
        self.action.direction[1] = self.inputDevice.getY()

        if vec.isAlmostZero(self.action.direction):
            vec.zeroize(self.action.direction) # TODO: necessary?
            self.action.speed = 0.0
        else:
            self.action.speed = min(1.0, vec.length(self.action.direction))
            vec.normalize(self.action.direction, self.action.direction)
Exemplo n.º 3
0
    def calcAction(self):
        self.action.direction[0] = self.inputDevice.getX()
        self.action.direction[1] = self.inputDevice.getY()

        if vec.isAlmostZero(self.action.direction):
            vec.zeroize(self.action.direction) # TODO: necessary?
            self.action.speed = 0.0
        else:
            self.action.speed = min(1.0, vec.length(self.action.direction))
            vec.normalize(self.action.direction, self.action.direction)
Exemplo n.º 4
0
def draw_circle(bot, center: Vec3, normal: Vec3, radius: float, pieces: int,
                color_func):
    # Construct the arm that will be rotated
    arm = normalize(cross(normal, center)) * radius
    angle = 2 * math.pi / pieces
    rotation_mat = axis_to_rotation(angle * normalize(normal))
    points = [center + arm]

    for i in range(pieces):
        arm = dot(rotation_mat, arm)
        points.append(center + arm)

    bot.renderer.draw_polyline_3d(points, color_func())
Exemplo n.º 5
0
    def predict_hit_pos(self, from_pos):
        speed = SUPER_SPEED if self.next_is_super else NORMAL_SPEED
        ball_prediction = self.get_ball_prediction_struct()

        if ball_prediction is not None:
            TIME_PER_SLICES = 1 / 60.0
            SLICES = 360

            # Iterate to find the first location which can be hit
            for i in range(0, 360):
                time = i * TIME_PER_SLICES
                rlpos = ball_prediction.slices[i].physics.location
                pos = Vec3(rlpos.x, rlpos.y, rlpos.z)
                dist = norm(from_pos - pos)
                travel_time = dist / speed
                if time >= travel_time:
                    # Add small bias for aiming
                    tsign = -1 if self.team == 0 else 1
                    enemy_goal = Vec3(0, tsign * -5030, 300)
                    bias_direction = normalize(pos - enemy_goal)
                    pos = pos + bias_direction * GOAL_AIM_BIAS_AMOUNT
                    return pos, travel_time

            # Use last
            rlpos = ball_prediction.slices[SLICES - 1].physics.location
            return Vec3(rlpos.x, rlpos.y, rlpos.z), 6

        return Vec3(0, 0, 0), 5
Exemplo n.º 6
0
    def calcAction(self):
        v = self.percepts.myPosition() - self.targetPositionPercept(self.percepts)

        # TODO: consider predicating on v.length() e.g. inversely proportional so
        # that speed increases as tagged character gets closer
        # max(0.0, min(1.0, 1.0 - (0.25*tagDist)/tagFar))
        self.action.setSpeed(1.0)
        self.action.setDirection(vec.normalize(v, self.tmp))
 def setVelocity(self, velocity):
     s = vec.length(velocity)
     if util.isAlmostZero(s):
         self.setSpeed(0)
         # Don't change the orientation if the speed is zero.
     else:
         self.setSpeed(s)
         self.shape.setOrientation(vec.normalize(velocity, self.velocity))
Exemplo n.º 8
0
 def setVelocity(self, velocity):
     s = vec.length(velocity)
     if util.isAlmostZero(s):
         self.setSpeed(0)
         # Don't change the orientation if the speed is zero.
     else:
         self.setSpeed(s)
         self.shape.setOrientation(vec.normalize(velocity, self.velocity))
Exemplo n.º 9
0
    def calcAction(self):
        v = self.percepts.myPosition() - self.targetPositionPercept(self.percepts)

        # TODO: consider predicating on v.length() e.g. inversely proportional so
        # that speed increases as tagged character gets closer
        # max(0.0, min(1.0, 1.0 - (0.25*tagDist)/tagFar))
        self.action.setSpeed(1.0)
        self.action.setDirection(vec.normalize(v, self.tmp))
Exemplo n.º 10
0
	def to_omega(self, dt):
		m = vec.mag(self.v)
		
		#print self.s,s

		if m > 0.0:
			omega = 2.0 * math.asin(m) / dt * vec.normalize(self.v)
		else:
			omega = np.zeros(3)
			
		ver = False
		if ver:
			print 'dt   ',dt
			print 'm    ',m
			print 'r    ',self.s,self.v
			print 'omega',omega



		return omega
Exemplo n.º 11
0
    def do_aiming_state(self):
        self.expected_hit_pos, travel_time = self.predict_hit_pos(
            self.info.my_car.aim_pos)
        self.expected_hit_time = self.info.time + travel_time
        self.direction = d = normalize(self.expected_hit_pos -
                                       self.info.my_car.aim_pos)

        rotation = Rotator(math.asin(d.z), math.atan2(d.y, d.x), 0)
        car_state = CarState(
            Physics(location=to_fb(self.info.my_car.aim_pos),
                    velocity=Vector3(0, 0, 0),
                    rotation=rotation,
                    angular_velocity=Vector3(0, 0, 0)))
        game_state = GameState(cars={self.index: car_state})
        self.set_game_state(game_state)

        if self.next_flight_start_time < self.info.time:
            self.state = self.FLYING
            if self.next_is_super:
                self.doing_super = True
                self.next_is_super = False
            else:
                self.doing_super = False
Exemplo n.º 12
0
    def calcAction(self):
        # TODO: make this settable
        soonThreshold = 50.0

        # TODO: consider re-factoring using BrainConditional (or something like it)
        # TODO: this is hardwired to only avoid static obstacles

        if soonThreshold < self.percepts.myTimeToCollision() or (self.percepts.myNextCollider() and Inf != self.percepts.nextCollider.mass):
             # No collision danger
             time = self.percepts.getTime()
             # How many milliseconds to wait after a potential collision was detected before
             # resuming with the default controller.  TODO: consider making a settable class variable.
             delay = 0.5
             if self.timeLastCollisionDetected < 0 or delay < time - self.timeLastCollisionDetected:
                 self.defaultBrain.calcAction()
                 self.action = self.defaultBrain.action
             else:
                 # Just continue with last action.
                 # TODO: consider some time discounted blend of default controller and
                 # avoidance vector.
                 pass

             return

        self.timeLastCollisionDetected = self.percepts.getTime()

        # Collision danger present so need to take evasive action.
        self.rp = vec.normalize(self.percepts.myNextCollisionPoint() - self.percepts.myPosition(), self.rp)

        self.tmp = util2D.perpendicularTo(self.rp, self.percepts.myNextCollider().normalTo(self.percepts.getMe(), self.tmp), self.tmp)[0]

        assert util.isAlmostZero(vec.dot(self.rp, self.tmp))

        self.action.setDirection(self.tmp)
        # TODO: modulate the speed based on time until collision and whatever the defaultControler
        # set it to.
        self.action.setSpeed(1.0)
Exemplo n.º 13
0
    def calcAction(self):
        # TODO: make this settable
        soonThreshold = 50.0

        # TODO: consider re-factoring using BrainConditional (or something like it)
        # TODO: this is hardwired to only avoid static obstacles

        if soonThreshold < self.percepts.myTimeToCollision() or (self.percepts.myNextCollider() and Inf != self.percepts.nextCollider.mass):
             # No collision danger
             time = self.percepts.getTime()
             # How many milliseconds to wait after a potential collision was detected before
             # resuming with the default controller.  TODO: consider making a settable class variable.
             delay = 0.5
             if self.timeLastCollisionDetected < 0 or delay < time - self.timeLastCollisionDetected:
                 self.defaultBrain.calcAction()
                 self.action = self.defaultBrain.action
             else:
                 # Just continue with last action.
                 # TODO: consider some time discounted blend of default controller and
                 # avoidance vector.
                 pass

             return

        self.timeLastCollisionDetected = self.percepts.getTime()

        # Collision danger present so need to take evasive action.
        self.rp = vec.normalize(self.percepts.myNextCollisionPoint() - self.percepts.myPosition(), self.rp)

        self.tmp = util2D.perpendicularTo(self.rp, self.percepts.myNextCollider().normalTo(self.percepts.getMe(), self.tmp), self.tmp)[0]

        assert util.isAlmostZero(vec.dot(self.rp, self.tmp))

        self.action.setDirection(self.tmp)
        # TODO: modulate the speed based on time until collision and whatever the defaultControler
        # set it to.
        self.action.setSpeed(1.0)
def normalTo(c0, c1, n):
    return vec.normalize(c1.position - c0.position, n)
Exemplo n.º 15
0
    def calcAction(self):
        v = self.targetPositionPercept(self.percepts) - self.percepts.myPosition()

        # TODO: consider predicating speed on v.length()
        self.action.setSpeed(1.0)
        self.action.setDirection(vec.normalize(v, self.tmp))
Exemplo n.º 16
0
    def calcAction(self):
        v = self.targetPositionPercept(self.percepts) - self.percepts.myPosition()

        # TODO: consider predicating speed on v.length()
        self.action.setSpeed(1.0)
        self.action.setDirection(vec.normalize(v, self.tmp))
Exemplo n.º 17
0
 def calcAction(self):
     vec.normalize(vec.randomVec(self.action.direction), self.action.direction)
     self.action.speed = util.clamp(util.uniform(), 0.25, 1.0)
Exemplo n.º 18
0
def normalTo(c0, c1, n):
    return vec.normalize(c1.position - c0.position, n)
Exemplo n.º 19
0
 def calcAction(self):
     vec.normalize(vec.randomVec(self.action.direction), self.action.direction)
     self.action.speed = util.clamp(util.uniform(), 0.25, 1.0)