Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 4, 2017, 4:17:59 PM (7 years ago)
Author:
kohlia
Message:

Position/velocity setting works now, relative script paths not, added test script

Location:
code/branches/ScriptableController_HS17/src/orxonox
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ScriptableController_HS17/src/orxonox/infos/GametypeInfo.cc

    r11606 r11638  
    318318        {
    319319            this->getLevel()->getScriptableController()->setPlayer(player);
    320             this->getLevel()->getScriptableController()->runScript(this->getLevel()->getScript());
     320            // TODO Fix for relative paths
     321            this->getLevel()->getScriptableController()->runScript(this->getLevel()->getFilename() + "/" + this->getLevel()->getScript());
    321322        }
    322323    }
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc

    r11606 r11638  
    6464}
    6565
     66void ScriptableController::registerMobileEntity(std::string id, MobileEntity *entity)
     67{
     68    this->mobileEntities_[id] = entity;
     69}
     70
    6671void ScriptableController::registerPawn(std::string id, Pawn *pawn)
    6772{
     
    124129}
    125130
     131MobileEntity *ScriptableController::getMobileEntityByID(std::string id) const
     132{
     133    if(id == "player" || id == "Player" || id == "PLAYER")
     134        return this->player_->getControllableEntity();
     135
     136    auto entity = this->mobileEntities_.find(id);
     137    return entity != this->mobileEntities_.end() ? entity->second : nullptr;
     138
     139}
     140
    126141Pawn *ScriptableController::getPawnByID(std::string id) const
    127142{
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h

    r11606 r11638  
    5959
    6060    /**
     61     * @brief Register a MobileEntity to the ScriptableController
     62     * @param id The ID of the MobileEntity
     63     * @param entity The MobileEntity
     64     *
     65     * The ScriptableController needs a list of all MobileEntity's so it can
     66     * convert an ID to a MobileEntity.
     67     */
     68    void registerMobileEntity(std::string id, MobileEntity *entity);
     69
     70    /**
    6171     * @brief Register a Pawn to the ScriptableController
    6272     * @param id The ID of the Pawn
     
    101111
    102112    /**
     113     * @brief Convert an ID to a MobileEntity pointer
     114     * @param id The ID of the MobileEntity
     115     * @return A pointer to the MobileEntity, nullptr if it's not found
     116     */
     117    MobileEntity *getMobileEntityByID(std::string id) const;
     118
     119    /**
    103120     * @brief Convert an ID to a Pawt pointer
    104121     * @param id The ID of the Pawn
     
    111128    PlayerInfo *player_;
    112129    std::map<std::string, WorldEntity*> worldEntities_;
     130    std::map<std::string, MobileEntity*> mobileEntities_;
    113131    std::map<std::string, Pawn*> pawns_;
    114132    std::map<Pawn*, std::string> pawnsReverse_;
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc

    r11606 r11638  
    2626    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnHit)>::registerFunction<&ScriptableControllerAPI::registerAtPawnHit>(this, lua, "registerAtPawnHit");
    2727
     28    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setPosition)>::registerFunction<&ScriptableControllerAPI::setPosition>(this, lua, "setPosition");
     29    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setOrientation)>::registerFunction<&ScriptableControllerAPI::setOrientation>(this, lua, "setOrientation");
     30    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setVelocity)>::registerFunction<&ScriptableControllerAPI::setVelocity>(this, lua, "setVelocity");
     31    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setAngularVelocity)>::registerFunction<&ScriptableControllerAPI::setAngularVelocity>(this, lua, "setAngularVelocity");
     32
    2833    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn");
    2934
     
    98103}
    99104
     105void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z)
     106{
     107    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
     108    if(entity == nullptr)
     109    {
     110        orxout(user_warning) << "Trying to set position of an unknown object" << std::endl;
     111        return;
     112    }
     113
     114    const Vector3 &old = entity->getPosition();
     115
     116    x = std::isnan(x) ? old.x : x;
     117    y = std::isnan(y) ? old.y : y;
     118    z = std::isnan(z) ? old.z : z;
     119
     120    entity->setPosition(x, y, z);
     121}
     122
     123void ScriptableControllerAPI::setOrientation(std::string id, double x, double y, double z, double angle)
     124{
     125    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
     126    if(entity == nullptr)
     127    {
     128        orxout(user_warning) << "Trying to set orientation of an unknown object" << std::endl;
     129        return;
     130    }
     131
     132    Vector3 old_axis;
     133    Degree old_angle;
     134
     135    entity->getOrientation().ToAngleAxis(old_angle, old_axis);
     136
     137    x = std::isnan(x) ? old_axis.x : x;
     138    y = std::isnan(y) ? old_axis.y : y;
     139    z = std::isnan(z) ? old_axis.z : z;
     140    angle = std::isnan(x) ? old_angle.valueDegrees() : angle;
     141
     142    entity->setOrientation(Vector3(x, y, z), Degree(angle));
     143}
     144
     145void ScriptableControllerAPI::setVelocity(std::string id, double x, double y, double z)
     146{
     147    MobileEntity *entity = this->controller_->getMobileEntityByID(id);
     148    if(entity == nullptr)
     149    {
     150        orxout(user_warning) << "Trying to set velocity of an unknown object" << std::endl;
     151        return;
     152    }
     153
     154    const Vector3 &old = entity->getVelocity();
     155
     156    x = std::isnan(x) ? old.x : x;
     157    y = std::isnan(y) ? old.y : y;
     158    z = std::isnan(z) ? old.z : z;
     159
     160    entity->setVelocity(x, y, z);
     161}
     162
     163void ScriptableControllerAPI::setAngularVelocity(std::string id, double x, double y, double z)
     164{
     165    MobileEntity *entity = this->controller_->getMobileEntityByID(id);
     166    if(entity == nullptr)
     167    {
     168        orxout(user_warning) << "Trying to set angular velocity of an unknown object" << std::endl;
     169        return;
     170    }
     171
     172    const Vector3 &old = entity->getAngularVelocity();
     173
     174    x = std::isnan(x) ? old.x : x;
     175    y = std::isnan(y) ? old.y : y;
     176    z = std::isnan(z) ? old.z : z;
     177
     178    entity->setAngularVelocity(x, y, z);
     179}
     180
    100181void ScriptableControllerAPI::pawnKilled(std::string id)
    101182{
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h

    r11606 r11638  
    3939    ~ScriptableControllerAPI();
    4040
    41     // --- API ----------------------------------
     41// ### API ####################################################################
    4242
    4343    /**
     
    139139    void killPawn(std::string id);
    140140
    141     // ------------------------------------------
     141    /**
     142     * @brief Set the position of an object
     143     * @param id The ID of the object
     144     * @param x The position on the x-axis
     145     * @param y The position on the y-axis
     146     * @param z The position on the z-axis
     147     */
     148    void setPosition(std::string id, double x, double y, double z);
     149
     150    /**
     151     * @brief Set the orientation of an object
     152     * @param id The ID of the object
     153     * @param x The x component of the axis vector
     154     * @param y The y component of the axis vector
     155     * @param z The z component of the axis vector
     156     * @param angle The angle around the axis
     157     *
     158     * To set the orientation, you have to specify the direction that the
     159     * object should be facing with the vector (x, y, z) and the rotation
     160     * of the object around this axis with 'angle', which has to be given
     161     * in degrees, NOT radian. The vector does not have to be normalized.
     162     */
     163    void setOrientation(std::string id, double x, double y, double z, double angle);
     164
     165    /**
     166     * @brief Set the velocity of an object
     167     * @param id The ID of the object
     168     * @param x The velocity in x-direction
     169     * @param y The velocity in y-direction
     170     * @param z The velocity in z-direction
     171     *
     172     * The velocity is in units per second.
     173     */
     174    void setVelocity(std::string id, double x, double y, double z);
     175
     176    /**
     177     * @brief Set the angular velocity of an object
     178     * @param id The ID of the object
     179     * @param x The rotation velocity around the x-axis
     180     * @param y The rotation velocity around the y-axis
     181     * @param z The rotation velocity around the z-axis
     182     */
     183    void setAngularVelocity(std::string id, double x, double y, double z);
     184
     185// ### API END ################################################################
    142186
    143187    /**
  • code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc

    r11583 r11638  
    7474                this->setAngularVelocity(rotationAxis.normalisedCopy() * rotationRate.valueRadians());
    7575        }
     76
     77        if(!this->id_.empty() && this->getLevel() != nullptr)
     78            this->getLevel()->getScriptableController()->registerMobileEntity(this->id_, this);
    7679    }
    7780
Note: See TracChangeset for help on using the changeset viewer.