Esempio n. 1
0
    def avoid_light_old(self, player_diff, player_distance):
        #Get the brightness at a few points and go in the direction that it's
        #Use green light for some reason
        elapsed = globals.time - self.last_random
        if elapsed < self.random_segments:
            return
        self.last_random = globals.time
        self.fleeing = True
        sp = self.screen_pos
        self.pixel_data = glReadPixels(sp.x - self.width * 2,
                                       sp.y - self.width * 2, self.width * 4,
                                       self.height * 4, GL_RGB, GL_FLOAT)[:, :,
                                                                          1:3]
        max_index = numpy.argmax(self.pixel_data[:, :, 1:2])
        min_index = numpy.argmin(self.pixel_data[:, :, 1:2])
        indices = numpy.unravel_index((max_index, min_index),
                                      self.pixel_data.shape)
        max_index = indices[0][0], indices[1][0], indices[2][0]
        min_index = indices[0][1], indices[1][1], indices[2][1]
        #print 'max=',max_index,self.pixel_data[max_index]
        #print 'test',self.pixel_data[indices]
        d = Point(indices[0][0] - indices[0][1],
                  indices[1][0] - indices[1][1]).unit_vector()
        if d.length() == 0:
            self.seek_player(player_diff, player_distance)
            self.move_direction *= -1

        self.move_direction = d.unit_vector() * self.speed
Esempio n. 2
0
    def Push(self):
        power = ((globals.game_view.mode.power_box.power_level)**
                 2) * self.push_strength
        globals.game_view.mode.power_box.Disable()

        self.push_start = None
        if not self.IsGrabbed():
            return
        obj = self.other_obj
        self.Ungrab()
        #Push on another object. Equal and opposite forces and what not
        #Fire a ray from my player to the object. Where it meets is where the force should be applied
        centre = self.body.GetWorldPoint([0, self.midpoint[1] * 1.01])
        front = self.body.GetWorldPoint([0, self.midpoint[1] * 100])

        cast_segment = box2d.b2Segment()
        cast_segment.p1 = centre
        cast_segment.p2 = front

        #This is a hack. My contact filtering messes up with rays, so turn it off for the duration of the ray cast
        self.physics.contact_filter.collide = True
        try:
            lam, normal, shape = self.physics.world.RaycastOne(
                cast_segment, True, None)
        finally:
            self.physics.contact_filter.collide = False
        if shape == self.shapeI:
            return
        if shape != obj.shapeI:
            return
        if abs(normal[0]) < 0.5 and abs(normal[1]) < 0.5:
            #print 'updating normal!',normal
            normal = Point(*(centre - front))
            normal /= normal.length()
            normal = normal.to_vec()

        #self.physics.contact_filter.pushed = (self,obj,globals.time+500)
        self.body.ApplyForce(normal * power, centre)
        intersection_point = self.body.GetWorldPoint((front - centre) * lam)
        shape.userData.body.ApplyForce(-normal * power, intersection_point)
Esempio n. 3
0
    def avoid_light_old(self, player_diff, player_distance):
        #Get the brightness at a few points and go in the direction that it's
        #Use green light for some reason
        elapsed = globals.time - self.last_random
        if elapsed < self.random_segments:
            return
        self.last_random = globals.time
        self.fleeing = True
        sp = self.screen_pos
        self.pixel_data = glReadPixels(sp.x-self.width*2,sp.y-self.width*2,self.width*4,self.height*4,GL_RGB,GL_FLOAT)[:,:,1:3]
        max_index = numpy.argmax(self.pixel_data[:,:,1:2])
        min_index = numpy.argmin(self.pixel_data[:,:,1:2])
        indices = numpy.unravel_index((max_index,min_index),self.pixel_data.shape)
        max_index = indices[0][0],indices[1][0],indices[2][0]
        min_index = indices[0][1],indices[1][1],indices[2][1]
        #print 'max=',max_index,self.pixel_data[max_index]
        #print 'test',self.pixel_data[indices]
        d = Point(indices[0][0]-indices[0][1],indices[1][0]-indices[1][1]).unit_vector()
        if d.length() == 0:
            self.seek_player(player_diff, player_distance)
            self.move_direction *= -1

        self.move_direction = d.unit_vector()*self.speed
Esempio n. 4
0
    def PhysUpdate(self):
        super(SeekingMissile,self).PhysUpdate()
        #Apply a thrust in the direction of the target
        target_pos = self.target.GetPos()
        self.pos = self.GetPos()
        if not target_pos or not self.pos:
            return
        diff = target_pos - self.pos
        direction = self.GetAngle() + (math.pi/2)
        distance,angle = cmath.polar(complex(diff.x,diff.y))
        #angle = (angle - (math.pi/2) - self.GetAngle())%(math.pi*2)
        angle = (angle - direction)%(math.pi*2)
        
        if angle < (math.pi):
            #need to turn right, but by how much?
            amount = angle/math.pi
        else:
            amount = -(2*math.pi - angle)/math.pi

        desired_av = 10*amount
        f = 1 - abs(amount)
        current_speed = Point(*self.body.linearVelocity)
        if f < 0.8 or current_speed.length() > 80:
            desired_velocity = 0
            #Apply a force in the direction we're not going to slow us down
            force = Point(-current_speed.x,-current_speed.y)
            self.body.ApplyForce(tuple(force),self.body.position)
        else:
            desired_velocity = 1
            thrust = 20
            vector = cmath.rect(thrust,direction)
            self.body.ApplyForce((vector.real,vector.imag),self.body.position)

        
        torque = (desired_av - self.body.angularVelocity)
        self.body.ApplyTorque(torque)