# Quaternion rotation.

Page 1 of 1

## 3 Replies - 920 Views - Last Post: 21 July 2017 - 05:39 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=405325&amp;s=bb7efcb09a0c57090bb8acab47cb930d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JohnnyOneEye

Reputation: 0
• Posts: 6
• Joined: 08-January 16

# Quaternion rotation.

Posted 21 July 2017 - 01:40 AM

hi im trying to use quaternions for rotation. my test program simply rotates a cube. the froblem is that the cube not only rotates but it distorts. becomming smaller in one direction or another as it rotates, visualy all points share a plane or line as i increse the angle and then seporate again back into a cube. if anyone can explain where im going wrong i would be very greatfull;

thankyou

```public partial class Test: UserControl {
double camX = 0, camY = 0, camZ=5;

public bool start=false;

Point3 [ ] p;// points
Point2 [ ] l;// screen point locations
double theta=0; // angle

public Test ( ) {
InitializeComponent ( );

p = new Point3 [ 8 ];
l = new Point2 [ 8 ];

p [ 00 ]=new Point3 ( -1 , 01 , 01 );
p [ 01 ]=new Point3 ( 01 , 01 , 01 );
p [ 02 ]=new Point3 ( 01 , -1 , 01 );
p [ 03 ]=new Point3 ( -1 , -1 , 01 );

p [ 04 ]=new Point3 ( -1 , 01 , -1 );
p [ 05 ]=new Point3 ( 01 , 01 , -1 );
p [ 06 ]=new Point3 ( 01 , -1 , -1 );
p [ 07 ]=new Point3 ( -1 , -1 , -1 );
}

public static double rad ( double d ) {
return Math.PI * d / 180.0;
}

public Point3 Rotate ( Point3 Point , Quaternion RotationQ ) {

Quaternion VectorQ = new Quaternion ( 0 , Point.X , Point.Y , Point.Z );

Quaternion Result = RotationQ*VectorQ*Quaternion.Conj ( RotationQ );

return new Point3 ( Result.I , Result.J , Result.K );
}

Point2 toScreen ( Point3 p3 ) {
double rThetaOver2 = rad ( theta )/2;
Point3 axis = Point3.Unit ( Point3.Unit ( new Point3 ( 0 , 0 , 1 )*Sin ( rThetaOver2 ) ));
Quaternion rotationQ=  new Quaternion ( Cos ( rThetaOver2 ) ,axis);

Point3 result = Rotate ( p3 , rotationQ );

//translate cam position
result.X-=camX; result.Y-=camY; result.Z-=camZ;

// fit to screen width and center image
result*=1/( result.Z );
result*=Width/2;
result.X+=Width/2;
result.Y+=Width/2;
return new Point2 ( result.X , result.Y );
}

protected override void OnPaint ( PaintEventArgs e ) {
Graphics g = e.Graphics;
int ln = 0;
foreach ( Point3 point in p ) {
Point2 p2 = toScreen ( point );
l [ ln ]=p2;
g.DrawEllipse ( Pens.Black , ( float ) ( p2.X-3 ) , ( float ) ( p2.Y-3 ) , 6 , 6 );

ln++;
}
g.DrawLine ( Pens.Black , ( float ) l [ 0 ].X , ( float ) l [ 0 ].Y , ( float ) l [ 1 ].X , ( float ) l [ 1 ].Y );
g.DrawLine ( Pens.Black , ( float ) l [ 1 ].X , ( float ) l [ 1 ].Y , ( float ) l [ 2 ].X , ( float ) l [ 2 ].Y );
g.DrawLine ( Pens.Black , ( float ) l [ 2 ].X , ( float ) l [ 2 ].Y , ( float ) l [ 3 ].X , ( float ) l [ 3 ].Y );
g.DrawLine ( Pens.Black , ( float ) l [ 3 ].X , ( float ) l [ 3 ].Y , ( float ) l [ 0 ].X , ( float ) l [ 0 ].Y );

g.DrawLine ( Pens.Black , ( float ) l [ 4 ].X , ( float ) l [ 4 ].Y , ( float ) l [ 5 ].X , ( float ) l [ 5 ].Y );
g.DrawLine ( Pens.Black , ( float ) l [ 5 ].X , ( float ) l [ 5 ].Y , ( float ) l [ 6 ].X , ( float ) l [ 6 ].Y );
g.DrawLine ( Pens.Black , ( float ) l [ 6 ].X , ( float ) l [ 6 ].Y , ( float ) l [ 7 ].X , ( float ) l [ 7 ].Y );
g.DrawLine ( Pens.Black , ( float ) l [ 7 ].X , ( float ) l [ 7 ].Y , ( float ) l [ 4 ].X , ( float ) l [ 4 ].Y );

g.DrawLine ( Pens.Black , ( float ) l [ 0 ].X , ( float ) l [ 0 ].Y , ( float ) l [ 4 ].X , ( float ) l [ 4 ].Y );
g.DrawLine ( Pens.Black , ( float ) l [ 1 ].X , ( float ) l [ 1 ].Y , ( float ) l [ 5 ].X , ( float ) l [ 5 ].Y );
g.DrawLine ( Pens.Black , ( float ) l [ 2 ].X , ( float ) l [ 2 ].Y , ( float ) l [ 6 ].X , ( float ) l [ 6 ].Y );
g.DrawLine ( Pens.Black , ( float ) l [ 3 ].X , ( float ) l [ 3 ].Y , ( float ) l [ 7 ].X , ( float ) l [ 7 ].Y );

theta+=0.1;
if ( start )
Invalidate ( );
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Quaternion rotation.

### #2 JohnnyOneEye

Reputation: 0
• Posts: 6
• Joined: 08-January 16

## Re: Quaternion rotation.

Posted 21 July 2017 - 03:29 AM

is it possible ive got my multiplication wrong?
```return new Quaternion ( ( a.R * b.R ) - ( a.I * b.I ) - ( a.J * b.J ) - ( a.K * b.K ) , ( a.R * b.I ) - ( a.I * b.R ) - ( a.J * b.K ) - ( a.K * b.J ) , ( a.R * b.J ) - ( a.I * b.K ) - ( a.J * b.R ) - ( a.K * b.I ) , ( a.R * b.K ) - ( a.I * b.J ) - ( a.J * b.I ) - ( a.K * b.R ) );

```

### #3 JohnnyOneEye

Reputation: 0
• Posts: 6
• Joined: 08-January 16

## Re: Quaternion rotation.

Posted 21 July 2017 - 03:59 AM

there was a problem with that. i think this is correct but still not working correctly
``` return new Quaternion (
( a.R * b.R ) - ( a.I * b.I ) - ( a.J * b.J ) - ( a.K * b.K ) ,
( a.R * b.I ) + ( a.I * b.R ) + ( a.J * b.K ) + ( a.K * b.J ) ,
( a.R * b.J ) + ( a.I * b.K ) + ( a.J * b.R ) + ( a.K * b.I ) ,
( a.R * b.K ) + ( a.I * b.J ) - ( a.J * b.I ) + ( a.K * b.R ) );
}
```

### #4 JohnnyOneEye

Reputation: 0
• Posts: 6
• Joined: 08-January 16

## Re: Quaternion rotation.

Posted 21 July 2017 - 05:39 AM

solved. was a signing mistake im multiplion.