Sign in to follow this  
Abion47

3D Rotation in 2D

Recommended Posts

I'm trying to render some dots to the screen using just 2D and rotate them when my mouse moves across the screen so it looks like a rotating cube. I'm having issues with the rotation equations, and I am sure I have them wrong somewhere. Here is my program (in C#):

[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace 3DRotation {
class Program : Form {
Font f;

float mouseX, mouseY;
float oldMouseX, oldMouseY;

Point3D[] points;
float pointOffsetX = 50;
float pointOffsetY = 50;

public Program() {
f = new Font("Courier New", 12);

points = new Point3D[8];
points[0] = new Point3D(-25, -25, -25);
points[1] = new Point3D(-25, 25, -25);
points[2] = new Point3D(25, -25, -25);
points[3] = new Point3D(25, 25, -25);
points[4] = new Point3D(-25, -25, 25);
points[5] = new Point3D(25, -25, 25);
points[6] = new Point3D(-25, 25, 25);
points[7] = new Point3D(25, 25, 25);

this.MouseMove += new MouseEventHandler(onMouseUp);
this.Paint += new PaintEventHandler(painter);

this.DoubleBuffered = true;
}

public void onMouseUp(object sender, MouseEventArgs e) {
oldMouseX = mouseX;
oldMouseY = mouseY;

mouseX = e.X;
mouseY = e.Y;

foreach (Point3D point in points) {
point.rotateX((oldMouseX - mouseX) / 100);
point.rotateY((oldMouseY - mouseY) / 100);
}

this.Text = "Mouse Position: " + mouseX + ", " + mouseY;
this.Invalidate();
}

public void painter(object sender, PaintEventArgs e) {
Pen p = new Pen(Brushes.Black, 1);

foreach (Point3D point in points) {
e.Graphics.FillEllipse(Brushes.Black, (float)point.x + pointOffsetX, (float)point.y + pointOffsetY, 5, 5);
}
}

static void Main(string[] args) {
Application.Run(new Program());
}

class Point3D {
public double x, y, z;

public Point3D(double x = 0d, double y = 0d, double z = 0d) {
this.x = x;
this.y = y;
this.z = z;
}

//In rotateX, the point is rotated around the X-axis at 0, 0.
//x should remain the same, while y and z rotate.
public void rotateX(double radians) {
System.Console.WriteLine(radians);
y = (y * Math.Cos(radians)) - (z * Math.Sin(radians));
z = (y * Math.Sin(radians)) + (z + Math.Cos(radians));
}

//In rotateY, the point is rotated around the Y-axis at 0, 0.
//y should remain the same, while x and z rotate.
public void rotateY(double radians) {
x = (x * Math.Cos(radians)) - (z * Math.Sin(radians));
z = (x * Math.Sin(radians)) + (z + Math.Cos(radians));
}
}
}
}
[/code]

I'm using my own 3D point class to hold the x, y, and z values, but when I draw it to the screen I only really use x and y. (I will use z later for scale)

Can someone who knows 3D rotation functions tell me what I am doing wrong?

Share this post


Link to post
Share on other sites
Rewrite the 2 occurrences of

[code]
z = (x * Math.Sin(radians)) + (z + Math.Cos(radians));
[/code]
to look like

[code]
z = (x * Math.Sin(radians)) + (z * Math.Cos(radians));
[/code]

Where to use +sine and where to use -sine is a convention. And how to map mouse pointer movement to rotation also. And whether the chosen composition of rotations is what you want in the end has to be investigated, too. Hence, after correcting the above shown mistake the result may still not look at expected.

Share this post


Link to post
Share on other sites
Wow, I feel like an idiot... but thanks to alvaro for that catch. That one definitely would've stumped me for a while.

And in hindsight this question would probably have felt more at home in the math section. :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this