Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4098584
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:19:16+00:00 2026-05-20T20:19:16+00:00

I’ve been playing around with an accelerometer with 3 axis: X, Y and Z.

  • 0

I’ve been playing around with an accelerometer with 3 axis: X, Y and Z. It says on the supplier’s site that it measures gravitational force.

I’m sending this data to the blender games engine where I am rotating a cube in real time depending on the data values coming from the accelerometer. However the values coming through don’t seem to match up.

On each axis the accelerometer spits out values from -700 to 700 on each axis and I need to convert these values to something I can use in Blender. My maths knowledge is not up to scratch so I don’t know where to start with this one.

If anybody could shed some light on this, that would be great.

Many thanks
Will

EDIT
Currently I’m using a bit of python code to convert the rotation values to a matrix:

def reorient(alpha, beta, gamma):
  a = math.cos(alpha) 
  b = math.sin(alpha) 
  c = math.cos(beta) 
  d = math.sin(beta) 
  e = math.cos(gamma) 
  f = math.sin(gamma)    
  ad = a*d 
  bd = b*d 
  matrix = [[c*e, -a*f+b*d*e, b*f+a*d*e], [c*f, a*e+b*d*f, -b*e+a*d*f], [-d, b*c, a*c]]
  return matrix 

I am then using setOrientation(matrix) to affect the rotation of the cube. However I am currently throwing the wrong values into the matrix reorient() function

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:19:17+00:00Added an answer on May 20, 2026 at 8:19 pm

    I guess you are using the measured acceleration to find the direction of gravitational pull (ie, down). If you are moving the accelerometer, apart from just turning it, there will be some additional force; think of the accelerometer having a pendulum weight handing from it, as you move it the pendulum sways (although in this case it would be a very short, fast-reacting pendulum?). You could try doing some sort of movement compensation, but it might be simpler to just try to keep the sensor in a fixed location.

    Edit: ok, it looks like I totally misread the question – you want to know how to do the rotation in a script?

    It looks like each Blender object has three properties (.RotX, .RotY, .RotZ) which contain the current values (in radians) and a method (.rot(new_rotx, new_roty, new_rotz)) which performs a rotation (see documentation at http://www.blender.org/documentation/249PythonDoc/Object.Object-class.html). I am currently looking at how the rotations are applied; more shortly.

    Edit2: it looks like the angles are specified as Euler angles (http://en.wikipedia.org/wiki/Euler_angles); they give some conversion matrices. It also looks like your accelerometer data is underconstrained (you need one more constraint, specifying rotation about the ‘down’ direction – maybe some sort of inertial ‘least distance from previous position’ calculation?)

    Edit3: there is a sample script which may be helpful; on my machine it is at C:\Users\Me\AppData\Roaming\Blender Foundation\Blender.blender\scripts\object_random_loc_sz_rot.py It shows how to get the currently selected object and tweak its rotation. Hope that helps!

    Edit4: for sake of discussion, here is some sample code; it may be a bit redundant (I haven’t worked in Blender before) and it doesn’t solve the problem, but it will at least give us a common basis for further discussion 😉

    #!BPY
    
    """
    Name: 'Set rotation by accelerometer'
    Blender: 249
    Group: 'Object'
    Tooltip: 'Set the selected objects rotation by accelerometer'
    """
    
    __bpydoc__=\
    '''
    This script sets the selected objects rotation by accelerometer.
    '''
    
    from Blender import Draw, Scene
    import math
    
    def reorient(alpha, beta, gamma):
        a = math.cos(alpha)
        b = math.sin(alpha)
        c = math.cos(beta)
        d = math.sin(beta)
        e = math.cos(gamma)
        f = math.sin(gamma)
    
        ad = a*d
        bd = b*d
    
        return = [
            [c*e, -a*f+b*d*e, b*f+a*d*e],
            [c*f, a*e+b*d*f, -b*e+a*d*f],
            [-d,  b*c,        a*c      ]
        ]
    
    def getAccel():
        # test stub -
        # need to get actual values from accelerometer here
        dx = -700
        dy = 100
        dz = 250
        return (dx,dy,dz)
    
    def normalize(vec):
        "Return scaled unit vector"
        x,y,z = vec
        mag = (x*x + y*y + z*z)**0.5
        return (x/mag, y/mag, z/mag)
    
    def main():
        scn = Scene.GetCurrent()
        try:
            obj = scn.objects.context
            euler = (obj.RotX, obj.RotY, obj.RotZ)
        except AttributeError:
            return
    
        down = normalize(getAccel())
        matrix = None
        # do something here to find new rotation-matrix
        #   based on euler and down
        # then
    
        if matrix:
            obj.setOrientation(matrix)
        else:
            # test value:
            # if reorient() is working properly, the
            # object's rotation should not change!
            obj.setOrientation(reorient(*euler))
    
    if __name__=="__main__":
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.