Exemplo n.º 1
0
    def update(self, task: Task):
        dt, ft = globalClock.getDt(), globalClock.getFrameTime()
        heading, position, pitch = self.player.get_h(), self.player.get_pos(), self.player.get_p()

        # self.sun.set_p(self.sun.get_p() - (dt * 20))
        # self.sun.set_h(self.sun.get_h() - (dt * 20))

        if KEY_MAP["up"]:
            position.z += self.SPEED * dt
            self.player.set_pos(position)

        if KEY_MAP["down"]:
            position.z -= self.SPEED * dt
            self.player.set_pos(position)

        if KEY_MAP["q"]:
            position.x -= self.SPEED * dt
            self.player.set_pos(position)

        if KEY_MAP["e"]:
            position.x += self.SPEED * dt
            self.player.set_pos(position)

        if KEY_MAP["w"]:
            dx = (self.SPEED * dt) * np.cos(np.radians(heading + 90))
            dy = (self.SPEED * dt) * np.sin(np.radians(heading + 90))
            position.y, position.x = position.y + dy, position.x + dx
            self.player.set_pos(position)

        if KEY_MAP["s"]:
            dx = - (self.SPEED * dt) * np.cos(np.radians(heading + 90))
            dy = - (self.SPEED * dt) * np.sin(np.radians(heading + 90))
            position.y, position.x = position.y + dy, position.x + dx
            self.player.set_pos(position)

        if KEY_MAP["d"] and not KEY_MAP["s"]:
            heading -= self.SPEED * dt * 2
            self.player.set_h(heading)

        if KEY_MAP["a"] and not KEY_MAP["s"]:
            heading += self.SPEED * dt * 2
            self.player.set_h(heading)

        if KEY_MAP["d"] and KEY_MAP["s"]:
            heading += self.SPEED * dt * 2
            self.player.set_h(heading)

        if KEY_MAP["a"] and KEY_MAP["s"]:
            heading -= self.SPEED * dt * 2
            self.player.set_h(heading)

        if KEY_MAP["left"]:
            pitch += self.SPEED * dt * 2
            self.player.set_p(pitch)

        if KEY_MAP["right"]:
            pitch -= self.SPEED * dt * 2
            self.player.set_p(pitch)

        return task.cont
Exemplo n.º 2
0
    def update(self, task: Task):
        dt, ft = globalClock.getDt(), globalClock.getFrameTime()

        if KEY_MAP["up"]:
            self.bc304.set_pos(self.x, self.y, self.z)
            self.z += self.speed * dt

        if KEY_MAP["down"]:
            self.bc304.set_pos(self.x, self.y, self.z)
            self.z -= self.speed * dt

        if KEY_MAP["right"]:
            self.bc304.set_pos(self.x, self.y, self.z)
            self.x += self.speed * dt

        if KEY_MAP["left"]:
            self.bc304.set_pos(self.x, self.y, self.z)
            self.x -= self.speed * dt

        if KEY_MAP["e"]:
            pos = self.sphere.get_pos()
            pos.z += self.speed * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["q"]:
            pos = self.sphere.get_pos()
            pos.z -= self.speed * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["a"]:
            pos = self.sphere.get_pos()
            pos.x -= self.speed * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["d"]:
            pos = self.sphere.get_pos()
            pos.x += self.speed * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["w"]:
            pos = self.sphere.get_pos()
            pos.y += self.speed * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["s"]:
            pos = self.sphere.get_pos()
            pos.y -= self.speed * dt
            self.sphere.set_pos(pos)

        # self.sphere.set_pos(float(np.cos(ft) * 4), float(np.sin(ft) * 4), float(np.sin(ft) * 10))

        # self.z -= 0.5 * dt
        # self.bc304.set_hpr(self.h, self.p, 0)
        # self.h += 0.1
        # self.p += 0.1

        return task.cont
Exemplo n.º 3
0
    def fire(self):
        timeNow = globalClock.getFrameTime()
        #small check for reloading condition

        if self.isReloading != False:
            #if reloading is not false, is a time firing kosher again. check and see if past
            if timeNow > self.isReloading:
                self.isReloading = False
                #also fill mag
                self.ammo['currentMag'] = self.ammo['magSize']
            else:
                #otherwise return with nothing to show for it
                return
        if (timeNow - self.lastFired) < self.fireRate:
            # if difference is SMALLER than firerate, don't do anything - exit
            return
        #targetVector will be owner's target if AI

        #targetVector = base.render.getRelativeVector(self.owner.target, Vec3(0, 1, 0))
        startPos = base.render.getRelativePoint(
            self.model, Vec3(0, self.length, -self.height / 2))
        #account for target height - shoot at center mass
        targetVector = self.owner.target.getPos() + Vec3(
            0, 0, self.owner.target.height / 2)
        targetVector = (targetVector - startPos)
        targetVector.normalize()

        #create projectile at set offset in front of weapon
        bullet(self, startPos, targetVector, 3, 480, 400)
        #play firing noise
        self.firingNoise.play()
        #record in lastFired
        self.lastFired = timeNow
        #decrement ammo
        self.ammo['currentMag'] -= 1
        #if this makes currentMag 0, reload
        if self.ammo['currentMag'] < 1:
            self.reload(timeNow)
Exemplo n.º 4
0
    def fire(self):
        '''
        if owner is a player:
            Create projectile, play noise, decrement ammo counter
            Projectile goes towards joint owner.headgun
        otherwise just create projectile aimed at owner.target
        '''

        #check if time since last fired > firerate
        timeNow = globalClock.getFrameTime()
        if (timeNow - self.lastFired) < self.fireRate:
            # if difference is SMALLER than firerate, don't do anything - exit
            return

        #initialize variables
        targetVector = None
        startPos = None
        if self.owner.isPlayer:
            #get player headgun
            targetVector = base.render.getRelativeVector(
                self.owner.headgun, Vec3(0, 1, 0))

        #start pos should always be self.length from pos. Will experiment with
        # https://discourse.panda3d.org/t/calculating-forward-vector-from-hpr/6261/2
        # Post describes how to get the forward vector from Panda's HPR format, particularly
        # in the context of a parent coordinate space rather than local.
        # This was critical to getting a vector that bullets should follow that approximates the
        # direction of the weapon, see line 78.
        startPos = base.render.getRelativePoint(
            self.model, Vec3(0, -self.length, -self.height / 2))

        #create projectile at set offset in front of weapon
        bullet(self, startPos, targetVector, 6, 480, 400)
        self.firingNoise.play()
        #record in lastFired
        self.lastFired = timeNow
Exemplo n.º 5
0
    def control(self,ValveCommands):

        self.gimbalX = 0
        self.gimbalY = 0

        self.Valves = ValveCommands-(ValveCommands-self.Valves)*np.exp(-self.dt/self.tau)

        self.EngObs = self.vulcain.predict_data_point(np.array(self.Valves).reshape(1,-1 ))
        #Brennkammerdruck, Gaskammertemp, H2Massenstrom, LOX MAssentrom, Schub
        #Bar,K,Kg/s,Kg/s,kN
        #self.dt = globalClock.getDt()

        pos = self.rocketNP.getPos()
        vel = self.rocketNP.node().getLinearVelocity()
        quat = self.rocketNP.getTransform().getQuat()
        Roll, Pitch, Yaw = quat.getHpr()
        rotVel = self.rocketNP.node().getAngularVelocity()

        # CHECK STATE
        if self.fuelMass <= 0:
            self.EMPTY = True
        #if pos.getZ() <= 36:
        #    self.LANDED = True
        self.LANDED = False
        self.processContacts()

        P, T, rho = air_dens(pos[2])
        rocketZWorld = quat.xform(Vec3(0, 0, 1))

        AoA = math.acos(min(max(dot(norm(vel), norm(-rocketZWorld)), -1), 1))

        dynPress = 0.5 * dot(vel, vel) * rho

        dragArea = self.rocketCSLon + (self.rocketCSLat - self.rocketCSLon) * math.sin(AoA)

        drag = norm(-vel) * dynPress * self.Cd * dragArea

        time = globalClock.getFrameTime()

        liftVec = norm(vel.project(rocketZWorld) - vel)
        if AoA > 0.5 * math.pi:
            liftVec = -liftVec
        lift = liftVec * (math.sin(AoA * 2) * self.rocketCSLat * dynPress)

        if self.CONTROL:
            self.throttle = self.heightPID.control(pos.getZ(), vel.getZ(), 33)

            pitchTgt = self.XPID.control(pos.getX(), vel.getX(), 0)
            self.gimbalX = -self.pitchPID.control(Yaw, rotVel.getY(), pitchTgt)

            rollTgt = self.YPID.control(pos.getY(), vel.getY(), 0)
            self.gimbalY = -self.rollPID.control(Pitch, rotVel.getX(), -rollTgt)



        self.thrust = self.EngObs[0][4]*1000
        #print(self.EngObs)
        quat = self.rocketNP.getTransform().getQuat()
        quatGimbal = TransformState.makeHpr(Vec3(0, self.gimbalY, self.gimbalX)).getQuat()
        thrust = quatGimbal.xform(Vec3(0, 0, self.thrust))
        thrustWorld = quat.xform(thrust)

        #print(thrustWorld)

        self.npDragForce.reset()
        self.npDragForce.drawArrow2d(Vec3(0, 0, 0), quat.conjugate().xform(drag) / 1000, 45, 2)
        self.npDragForce.create()

        self.npLiftForce.reset()
        self.npLiftForce.drawArrow2d(Vec3(0, 0, 0), quat.conjugate().xform(lift) / 1000, 45, 2)
        self.npLiftForce.create()

        self.npThrustForce.reset()
        if self.EMPTY is False & self.LANDED is False:
            self.npThrustForce.drawArrow2d(Vec3(0, 0, -0.5 * self.length),
                                           Vec3(0, 0, -0.5 * self.length) - thrust / 30000, 45, 2)
            self.npThrustForce.create()

        self.rocketNP.node().applyForce(drag, Vec3(0, 0, 0))
        self.rocketNP.node().applyForce(lift, Vec3(0, 0, 0))
        #print(self.EMPTY,self.LANDED)
        if self.EMPTY is False & self.LANDED is False:
            self.rocketNP.node().applyForce(thrustWorld, quat.xform(Vec3(0, 0, -1 / 2 * self.length)))
            self.updateRocket(self.EngObs[0][2]+self.EngObs[0][3], self.dt)
        self.rocketNP.node().setActive(True)
        self.fuelNP.node().setActive(True)

        self.processInput()

        self.world.doPhysics(self.dt)
        self.steps+=1


        if self.steps > 1000:
            self.DONE = True

        telemetry = []

        telemetry.append('Thrust: {}'.format(int(self.EngObs[0][4])))
        telemetry.append('Fuel: {}%'.format(int(self.fuelMass / self.fuelMass_full * 100.0)))
        telemetry.append('Gimbal: {}'.format(int(self.gimbalX)) + ',{}'.format(int(self.gimbalY)))
        telemetry.append('AoA: {}'.format(int(AoA / math.pi * 180.0)))
        telemetry.append('\nPos: {},{}'.format(int(pos.getX()), int(pos.getY())))
        telemetry.append('Height: {}'.format(int(pos.getZ())))
        telemetry.append('RPY: {},{},{}'.format(int(Roll), int(Pitch), int(Yaw)))
        telemetry.append('Speed: {}'.format(int(np.linalg.norm(vel))))
        telemetry.append('Vel: {},{},{}'.format(int(vel.getX()), int(vel.getY()), int(vel.getZ())))
        telemetry.append('Rot: {},{},{}'.format(int(rotVel.getX()), int(rotVel.getY()), int(rotVel.getZ())))
        telemetry.append('LANDED: {}'.format(self.LANDED))
        telemetry.append('Time: {}'.format(self.steps*self.dt))
        telemetry.append('TARGET: {}'.format(self.targetAlt))
        #print(pos)
        if self.VISUALIZE is True:
            self.ostData.setText('\n'.join(telemetry))
            self.cam.setPos(pos[0] - 120 * self.scale, pos[1] - 120 * self.scale, pos[2] + 80 * self.scale)
            self.cam.lookAt(pos[0], pos[1], pos[2])
            self.taskMgr.step()
Exemplo n.º 6
0
    def update(self, task: Task):
        if self.f.state == settings.IN_MENU_STATE:
            return task.cont

        dt, ft = globalClock.getDt(), globalClock.getFrameTime()
        tank_heading, tank_position = self.tank.tank.get_h(
        ), self.tank.tank.get_pos()

        #     # self.sun.set_p(self.sun.get_p() - (dt * 40))
        #     # self.sun.set_h(self.sun.get_h() - (dt * 20))

        # self.tank.hit()
        z_pos = 0.01 if settings.DISABLE_Z_MOV else tank_position.z
        self.tank.tank.set_fluid_pos(tank_position.x, tank_position.y, z_pos)
        self.tank.handle_sfx()

        if self.enemy.hp <= 0 and self.enemy.is_alive:
            self.enemy.tank.removeNode()
            del self.environment["enemy"]
            self.enemy.is_alive = False
            print("defeated")

        if KEY_MAP["up"]:
            tank_position.z += self.SPEED * dt
            self._tank.set_fluid_pos(tank_position)

        if KEY_MAP["down"]:
            tank_position.z -= self.SPEED * dt
            self._tank.set_fluid_pos(tank_position)

        if KEY_MAP["q"]:
            tank_position.x -= self.SPEED * dt
            self._tank.set_fluid_pos(tank_position)

        if KEY_MAP["e"]:
            tank_position.x += self.SPEED * dt
            self._tank.set_fluid_pos(tank_position)

        if KEY_MAP["w"]:
            dx = (self.SPEED * dt) * np.cos(np.radians(tank_heading + 90))
            dy = (self.SPEED * dt) * np.sin(np.radians(tank_heading + 90))
            tank_position.y, tank_position.x = tank_position.y + dy, tank_position.x + dx
            self._tank.set_fluid_pos(tank_position)

        if KEY_MAP["s"]:
            dx = -(self.SPEED * dt) * np.cos(np.radians(tank_heading + 90))
            dy = -(self.SPEED * dt) * np.sin(np.radians(tank_heading + 90))
            tank_position.y, tank_position.x = tank_position.y + dy, tank_position.x + dx
            self._tank.set_fluid_pos(tank_position)

        if KEY_MAP["d"] and not KEY_MAP["s"]:
            tank_heading -= self.TURN_SPEED * dt * 2
            self._tank.set_h(tank_heading)

        if KEY_MAP["a"] and not KEY_MAP["s"]:
            tank_heading += self.TURN_SPEED * dt * 2
            self._tank.set_h(tank_heading)

        if KEY_MAP["d"] and KEY_MAP["s"]:
            tank_heading += self.TURN_SPEED * dt * 2
            self._tank.set_h(tank_heading)

        if KEY_MAP["a"] and KEY_MAP["s"]:
            tank_heading -= self.TURN_SPEED * dt * 2
            self._tank.set_h(tank_heading)

        return task.cont
Exemplo n.º 7
0
    def render(self, visualize=False):
        #self.sim.setVisualization(visualize)
        ...

    def close(self):
        ...


if __name__ == "__main__":
    env = LearningRocket()
    observation = env.reset()
    done = False
    while done is False:
        observation, reward, done, info = env.step([0.1, 0, 0])
        # print(observation)
    time = globalClock.getFrameTime()
    print(time)
    print(observation)

    env.sim.VISUALIZE = True
    observation = env.reset()
    for i in range(1000):
        observation = env.step([0.1, 0, 0])
        # print(observation)
    time = globalClock.getFrameTime()
    print(time)
    print(observation)
    # observation = env.reset()

    # If the environment don't follow the interface, an error will be thrown
    # check_env(env, warn=True)