Rotating an object around another object with a script

From the Oblivion ConstructionSet Wiki
Revision as of 00:24, 1 April 2007 by imported>Indefiance
Jump to navigation Jump to search

See this discussion for background.

The example proof-of-concept script rotates the object it's attached to horizontally around the player at a constant speed. Replacing the player reference with any other would let this object rotate around any other reference (for example, creating a bunch of slowly rotating rocks and platforms around a tower; although that's better done via an animation). This will mess up Havok collisions, though - changes to the script to deal with it are highy welcome.

The script uses the ESM Math Library for its trigonometric functions. Read the Stage function repository for a detailed description. Any other function returning cos() and sin() values - including a simple table lookup with optional linear interpolation - will suffice.

ScriptName CirclingAroundThePlayerScript

short constantsset ; did we set the constants?

float rspeed ; in degrees/s
float hdist ; horizontal distance from the player
float rpos ; position on the circle
float mod_x ; positions relative to the player
float mod_y
float mod_z
float org_x ; position of the player
float org_y
float org_z

Begin GameMode

    ; setting up constants
    if constantsset == 0
        set rspeed       to  10 ; constant radial speed
        set mod_z        to 128 ; constant height
        set hdist        to 160 ; constant distance
        set constantsset to   1 ; done setting up the constants
    endif

    ; updating the position on the circle (rpos)
    set rpos to rpos + ( rspeed * getSecondsPassed )
    if rpos > 360
        set rpos to rpos - 360
    endif

    ; setting the mods, via the SinCosTan function
    set math.fin1 to rpos
    setStage math 15                ; SinCosTan(rpos) function call
    set mod_x to math.fout  * hdist ; sin(rpos) * hdist
    set mod_y to math.fout2 * hdist ; cos(rpos) * hdist

    ; getting the player's position
    set org_x to Player.getPos X
    set org_y to Player.getPos Y
    set org_z to Player.getPos Z

    ; setting my own position
    set org_x to org_x + mod_x
    setPos X, org_x + mod_x
    set org_y to org_y + mod_y
    setPos Y, org_y
    set org_z to org_z + mod_z
    setPos Z, org_z

End


This script may or may not compile correctly...