hey,
i am using the value returned from the accelerometer. it returns me the "roll" value which is between -180 to 180 that adds up to 360. what i want to do is just simply produce this on the screen by drawing a line (g1.drawLine();) that would rotate in the same angle corresponding to the output from the accelerometer.

any ideas how this could be done?

all the suggestions will be greatly appreciated!!
THANK YOU

The trig function tangent (AKA tan) gives you the x vs y slope of a line from its angle - so for example the tan of 45 degrees is 1, so a line from 0,0 to 1,1 will be at 45 degrees. The Math class has a tan method.

> all the suggestions will be greatly appreciated!!

Assuming the normal quadrant and angle measuring convention:

x = cx + r * cos(theta)
y = cy + r * sin(theta)

where:
x, y = coordinates of the point on the circumference of the circle
cx, cy = coordinates of the center of the circle
r = radius
theta = angle in "radians"

After you get (x, y), it's just a matter of drawing a line from the center to (x, y) and you should be good to go. Of course, this would work assuming you convert the angle values from degree to radians and adjust the adjust the read angle values based on how your accelerometer is oriented.

Good read: http://www.yaldex.com/games-programming/0672323699_ch08lev1sec4.html

Edited 5 Years Ago by ~s.o.s~: n/a

thanks for this, this is a genius solution!!!
but i am actually receiving wrong result. instead of the line spinning accordingly to my accelerometer it spins much faster. a slight turn of the accelerometer makes the line rotate a full 360 degrees.

this is what i have written

float roll =orientation.getRoll();
                        int xcenter =100;
                        int ycenter =100;
                        int radius = 50;

                        int x = (int) (xcenter + radius * java.lang.Math.cos(roll));
                        int y =(int) (ycenter + radius * java.lang.Math.sin(roll));

                   
                        g2.drawLine(xcenter, ycenter, x, y);

THANK YOU

yeah i did forget the conversion of degrees in to radians ,it works now!!!:)

thank you, you clever people :)

i was still wondering how could i make this line rotate accordingly to the accelerometer instead of spinning around one point.

thanks again

i was still wondering how could i make this line rotate accordingly to the accelerometer instead of spinning around one point.

What exactly do you mean?

well currently the output is being displayed, where the beginning of the line is set and does not change

int xcenter =100;
int ycenter =100;

and the radius from that point spins according to the output produced from the accelerometer.
in other words the output looks like analog clock ( i hope you understand what im trying to say)


what i want is that the line would be, lets say 10pixels wide and the center of the line would stay still but the edges would turn accordingly to my accelerometer.

i am not sure how to describe it in other words, hope this makes it clearer.

I guess I'm having a bit of a "slow" day today! I get the current situation OK, but I still can't visualise the desired one. Do you mean its a rectangle, 10 pixels by (something long) that rotates about its center point?

This article has been dead for over six months. Start a new discussion instead.