コード例 #1
0
class CameraController(object):
    """
    Here we have our camera attached to a node, looking at then node.
    
    """

    event.add_event_types([
        'CAM_FORWARD', 'CAM_LEFT', 'CAM_BACK', 'CAM_RIGHT', 'CAM_UP',
        'CAM_DOWN', 'CAM_PITCH_DOWN', 'CAM_PITCH_UP', 'CAM_TOGGLE_FOLLOW',
        'CAM_ZOOM_IN', 'CAM_ZOOM_OUT', 'CAM_YAW_LEFT', 'CAM_YAW_RIGHT'
    ])

    def __init__(self, camera):
        self._camera = camera.camera
        self._camera_node = camera.node
        self.original_parent = None

        self.handler_map = {'CAM_TOGGLE_FOLLOW': self.test_follow}
        #            'CAM_INDEPENDENT' : self._make_independent}

        event.register_handlers(self.handler_map)

        # This sets up automatic setting of the key down properties
        watched_buttons = {
            '_forward': ['CAM_FORWARD'],
            '_backward': ['CAM_BACK'],
            '_left': ['CAM_LEFT'],
            '_right': ['CAM_RIGHT'],
            '_down': ['CAM_DOWN'],
            '_up': ['CAM_UP'],
            '_pitch_up': ['CAM_PITCH_UP'],
            '_pitch_down': ['CAM_PITCH_DOWN'],
            '_zoom_in': ['CAM_ZOOM_IN'],
            '_zoom_out': ['CAM_ZOOM_OUT'],
            '_yaw_left': ['CAM_YAW_LEFT'],
            '_yaw_right': ['CAM_YAW_RIGHT']
        }

        self.key_observer = ButtonStateObserver(self, watched_buttons)

    def __del__(self):
        # Make sure to remove event handlers so they are called after the
        # object is gone
        event.remove_handlers(self.handler_map)
        pass

    def update(self, time_since_last_frame):
        moveUnit = 5 * time_since_last_frame

        # A really bad way to generate the rotation vectors I want
        # Moves us in the Z (up/down direction)
        height = Ogre.Vector3(0, 0, moveUnit)

        # We ignore z, because that always go up and down relative to the world
        # axes.  Here we want forward and side to be relative to the direction
        # the camera is facing.
        toCamera = self._camera.getRealPosition() - self._camera_node.position
        toCamera.z = 0
        quat = Ogre.Vector3.UNIT_X.getRotationTo(toCamera)

        # Moves us in the X (Forward/back direction)
        trans = quat * Ogre.Vector3(-moveUnit, 0, 0)
        trans.z = 0

        # Moves us in the Y (Left/Right direction)
        strafe = quat * Ogre.Vector3(0, moveUnit, 0)
        strafe.z = 0

        if self.original_parent is None:
            if self._forward:
                self._camera_node.translate(trans)  #, Ogre.Node.TS_WORLD)
            if self._backward:
                self._camera_node.translate(trans *
                                            -1.0)  #, Ogre.Node.TS_WORLD)

            if self._up:
                self._camera_node.translate(height)  #, Ogre.Node.TS_WORLD)
            if self._down:
                self._camera_node.translate(height *
                                            -1.0)  #, Ogre.Node.TS_WORLD)

            if self._left:
                self._camera_node.translate(strafe *
                                            -1.0)  #, Ogre.Node.TS_WORLD)
            if self._right:
                self._camera_node.translate(strafe)  #, Ogre.Node.TS_WORLD)

            pos = self._camera.position
            if self._zoom_in:
                self._camera.setPosition(pos + (pos * -moveUnit / 3))
            if self._zoom_out:
                self._camera.setPosition(pos + (pos * moveUnit / 3))

            if self._pitch_up:
                self._camera_node.yaw(Ogre.Degree(moveUnit * 6))
            if self._pitch_down:
                self._camera_node.yaw(Ogre.Degree(-moveUnit * 6))

            if self._yaw_left:
                self._camera_node.roll(Ogre.Degree(-moveUnit * 6),
                                       Ogre.Node.TS_WORLD)
            if self._yaw_right:
                self._camera_node.roll(Ogre.Degree(moveUnit * 6),
                                       Ogre.Node.TS_WORLD)

    @toggle('CAM_TOGGLE_FOLLOW')
    def test_follow(self, state):
        print 'CAM_TOGGLE', state

    def _mouse_moved(self, arg):
        """
        If the shift key is down, swing the camera
        """

        if self.shift_key:
            ms = arg.get_state()
            # Rotate around our object
            self._camera_node.pitch(Ogre.Radian(ms.Y.rel * -0.5))
            self._camera_node.yaw(Ogre.Radian(ms.X.rel * -0.5),
                                  Ogre.Node.TS_WORLD)

            # Zoom in or out of our objective
            if ms.Z.rel < 0 or ms.Z.rel > 0:
                pos = self._camera.position
                self._camera.setPosition(pos + (pos * ms.Z.rel * 0.002))

    def _follow_node(self, node):
        if self.original_parent is not None:
            raise GraphicsError('Camera is already free')

        # Remove node from its current parent
        self.original_parent = self._camera_node.parent
        self.original_parent.removeChild(self._camera_node)

        # Reparent node and
        node.addChild(self._camera_node)
        self._camera_node.setPosition(Ogre.Vector3(0, 0, 0))
        #self.camera_node = node
        #self.camera_node.attachObject(self.camera)
        self._camera.lookAt(node._getDerivedPosition())

    def _make_independent(self):
        if self.original_parent is None:
            raise GraphicsError('Camera is already free floating')

        self._camera_node.parent.removeChild(self._camera_node)
        self.original_parent.addChild(self._camera_node)
        self._camera.lookAt(self._camera_node._getDerivedPosition())
        self.original_parent = None
コード例 #2
0
import ogre.physics.OgreNewt as ogrenewt

# Project Imports
import ext.core as core
import ram.sim.simulation as simulation
import ram.sim.input as input
import ram.module
import ram.event as event

# Ensure the module manager is started up (legacy code)
moduleManager = None
if not hasattr(ram.module.ModuleManager, "get"):
    moduleManager = ram.module.ModuleManager()

# Special Event types
event.add_event_types(["SCREENSHOT"])


class WindowListener(ogre.WindowEventListener):
    def __init__(self, closeHandler):
        ogre.WindowEventListener.__init__(self)

        self._closeHandler = closeHandler

    # Window Event Listener Methods
    def windowClosed(self, window):
        self._debug_print("WINDOW SHUTDOWN")
        self._closeHandler(window)

    def windowMoved(self, window):
        pass
コード例 #3
0
ファイル: module.py プロジェクト: ChrisCarlsen/tortuga
# File:  packages/python/ram/module.py

"""
This encapsulates the module system.  It allows seperate parts of the program
to be aware of other portions starting and stopping without having to be 
explictly joined.
"""

# Move me into the core?

# Project Imports
import ram.event as event
from ram.core import Singleton, Interface, verifyClass, Attribute, implements, Component
from decorator import decorate, make_weak_signature

event.add_event_types(['MODULE_CREATED','MODULE_START', 'MODULE_PAUSE', 
                       'MODULE_SHUTDOWN'])

class IModule(Interface):
    """
    A module can be started and stopped.
    """

    def start():
        """
        If threaded this will cause the module to call there update function
        in a contious loop.  In synchonous mode this will enable the update
        function.
        
        Raises MODULE_START event.
        """
        pass
コード例 #4
0
ファイル: control.py プロジェクト: stormageAC/tortuga
import ext.math as math
import ext.control as control
import ext.network as network
import ext.vehicle as vehicle
import ext.estimation as estimation

import ram.sim.input as input
import ram.motion.basic as motion
import ram.motion.trajectories as trajectories
import ram.timer as timer

import ram.event as event

event.add_event_types([
    'THRUST_FORE', 'THRUST_BACK', 'TURN_LEFT', 'TURN_RIGHT', 'DIVE', 'SURFACE',
    'PITCH_UP', 'PITCH_DOWN', 'ROLL_PORT', 'ROLL_STARBOARD', 'STRAFE_RIGHT',
    'STRAFE_LEFT', 'STOP'
])


class KeyboardController(core.Subsystem):
    TARGET_VELOCITY_UPDATE = core.declareEventType('TARGET_VELOCITY_UPDATE')
    TARGET_DEPTH_UPDATE = core.declareEventType('TARGET_DEPTH_UPDATE')

    def __init__(self, config, deps):
        core.Subsystem.__init__(self, config.get('name', 'KeyboardController'))

        self._controller = core.Subsystem.getSubsystemOfType(
            control.IController, deps)
        self._stateEstimator = core.Subsystem.getSubsystemOfType(
            estimation.IStateEstimator, deps)
コード例 #5
0
ファイル: control.py プロジェクト: ChrisCarlsen/tortuga
import ext.core as core
import ext.math as math
import ext.control as control
import ext.network as network
import ext.vehicle as vehicle
import ext.estimation as estimation

import ram.sim.input as input
import ram.motion.basic as motion
import ram.motion.trajectories as trajectories
import ram.timer as timer

import ram.event as event

event.add_event_types(['THRUST_FORE', 'THRUST_BACK', 'TURN_LEFT', 'TURN_RIGHT',
                       'DIVE', 'SURFACE', 'PITCH_UP', 'PITCH_DOWN', 
                       'ROLL_PORT', 'ROLL_STARBOARD', 'STRAFE_RIGHT',
                       'STRAFE_LEFT', 'STOP'])

class KeyboardController(core.Subsystem):
    TARGET_VELOCITY_UPDATE = core.declareEventType('TARGET_VELOCITY_UPDATE')
    TARGET_DEPTH_UPDATE = core.declareEventType('TARGET_DEPTH_UPDATE')

    def __init__(self, config, deps):
        core.Subsystem.__init__(self, config.get('name', 'KeyboardController'))

        self._controller = core.Subsystem.getSubsystemOfType(
            control.IController, deps)
        self._stateEstimator = core.Subsystem.getSubsystemOfType(
            estimation.IStateEstimator, deps)
        self._motionManager = core.Subsystem.getSubsystemOfType(
            motion.MotionManager, deps)
コード例 #6
0
ファイル: simulation.py プロジェクト: stormageAC/tortuga
import ogre.renderer.OGRE as Ogre
import ogre.physics.OgreNewt
warnings.simplefilter('default', RuntimeWarning)

# Project Imports
import ram.event as event
import ram.sim.scene as scene
from ram.sim.input import InputSystem
from ram.sim.util import SimulationError
import ram.sim.defaults as defaults

from ram.core import Singleton, log, log_init, implements, Component, environmentSub
from ram.module import Module, IModule

# Events
event.add_event_types(['SIM_UPDATE', 'OGRE_RENDERER_CREATED'])


class GraphicsError(SimulationError):
    """ Error from the graphics system """
    pass


class Simulation(Singleton, Module):
    """
    The root object of the simulation, this is a singleton object just use
    'Simulation.get()' to access it.
    """
    implements(IModule)

    #@log_init(defaults.simulation_log_config)
コード例 #7
0
ファイル: module.py プロジェクト: stormageAC/tortuga
# Author: Joseph Lisee <*****@*****.**>
# File:  packages/python/ram/module.py
"""
This encapsulates the module system.  It allows seperate parts of the program
to be aware of other portions starting and stopping without having to be 
explictly joined.
"""

# Move me into the core?

# Project Imports
import ram.event as event
from ram.core import Singleton, Interface, verifyClass, Attribute, implements, Component
from decorator import decorate, make_weak_signature

event.add_event_types(
    ['MODULE_CREATED', 'MODULE_START', 'MODULE_PAUSE', 'MODULE_SHUTDOWN'])


class IModule(Interface):
    """
    A module can be started and stopped.
    """
    def start():
        """
        If threaded this will cause the module to call there update function
        in a contious loop.  In synchonous mode this will enable the update
        function.
        
        Raises MODULE_START event.
        """
        pass
コード例 #8
0
ファイル: control.py プロジェクト: JasonHagler/tortuga
import ext.core as core
import ext.control as control

import ram.sim.input as input

import ram.event as event

event.add_event_types(
    [
        "THRUST_FORE",
        "THRUST_BACK",
        "TURN_LEFT",
        "TURN_RIGHT",
        "DIVE",
        "SURFACE",
        "PITCH_UP",
        "PITCH_DOWN",
        "ROLL_PORT",
        "ROLL_STARBOARD",
        "STRAFE_RIGHT",
        "STRAFE_LEFT",
        "STOP",
    ]
)


class KeyboardController(core.Subsystem):
    def __init__(self, config, deps):
        core.Subsystem.__init__(self, config.get("name", "KeyboardController"))
        #        self._controller = control.IController.castTo(deps[0])
        self._controller = deps[0]
コード例 #9
0
ファイル: simulation.py プロジェクト: ChrisCarlsen/tortuga
import ogre.renderer.OGRE as Ogre
import ogre.physics.OgreNewt
warnings.simplefilter('default', RuntimeWarning)

# Project Imports
import ram.event as event
import ram.sim.scene as scene
from ram.sim.input import InputSystem
from ram.sim.util import SimulationError
import ram.sim.defaults as defaults

from ram.core import Singleton, log, log_init, implements, Component, environmentSub
from ram.module import Module, IModule

# Events
event.add_event_types(['SIM_UPDATE',
                       'OGRE_RENDERER_CREATED']) 

class GraphicsError(SimulationError):
    """ Error from the graphics system """
    pass

class Simulation(Singleton, Module):
    """
    The root object of the simulation, this is a singleton object just use
    'Simulation.get()' to access it.
    """
    implements(IModule)
    
    #@log_init(defaults.simulation_log_config)
    def init(self, config = {}):
        self._ogre_root = None
コード例 #10
0
import ogre.physics.OgreNewt as ogrenewt

# Project Imports
import ext.core as core
import ram.sim.simulation as simulation
import ram.sim.input as input
import ram.module
import ram.event as event

# Ensure the module manager is started up (legacy code)
moduleManager = None
if not hasattr(ram.module.ModuleManager, 'get'):
    moduleManager = ram.module.ModuleManager()

# Special Event types
event.add_event_types(['SCREENSHOT'])


class WindowListener(ogre.WindowEventListener):
    def __init__(self, closeHandler):
        ogre.WindowEventListener.__init__(self)

        self._closeHandler = closeHandler

    # Window Event Listener Methods
    def windowClosed(self, window):
        self._debug_print('WINDOW SHUTDOWN')
        self._closeHandler(window)

    def windowMoved(self, window):
        pass