RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Circular Motion

Cmonkey123

Wanderer
Circular Motion

I've started playing around with C# again, making simple games and other experiments in the hopes of making more complex interactive programs.

While working on a simple revolving sphere, I realized I can't figure out for the life of me how to make it revolve properly.

Does anyone know what equations or methods or anything I could possibly use to make an object revolve? Any help would be appreciated.

/edit
This is what I have so far (I found this equation for circular motion online, not sure if that's even close to what I want), but it doesn't work:
Code:
		public void RotationTimerTick( object sender, System.EventArgs e )
		{
			int x = sphereX;
			int y = sphereY;

			int cos20 = Convert.ToInt32( Math.Cos( 20 ) );

			sphereX = 100 * cos20 + x;
			sphereY = 100 * cos20 + y;

			Invalidate();
		}

Obviously, I then use a draw command to draw the sphere at sphereX, sphereY. This doesn't freeze the program like many of my previous attempts, but the sphere stays at (0,0) and doesn't move anywhere.
 

Cmonkey123

Wanderer
Hmm okay, I got the sphere moving.

But now it just moves in a line diagonally to the right...

I wanted the sphere to move on a circle I have drawn in my program with a radius of 100 pixels. Anyone have any ideas?
 

Serp

Sorceror
Make Y depend on sin. Oh... and you'll want to cos and sin the same number...

Code:
		double number;

		public void RotationTimerTick( object sender, System.EventArgs e )
		{
			number += 0.1;

			sphereX = 100 * Math.Cos(number) + 100;
			sphereY = 100 * Math.Sin(number) + 100;

			Invalidate();
		}

Having basic understanding of trigonometry definately helps, too. :)
 

Cmonkey123

Wanderer
That still makes it go in a diagonal line.

I even tried converting the 20 degrees into radians and it still moves in a line.

Here's what I have so far:
Code:
		public void RotationTimerTick( object sender, System.EventArgs e )
		{
			float x = sphereX;
			float y = sphereY;

			sphereX = ( 50 * (float)Math.Cos( 20 ) ) + x;
			sphereY = ( 50 * (float)Math.Sin( 20 ) ) + y;

			Invalidate();
		}
 

Cmonkey123

Wanderer
Okay it's rotating, but now I can't get the damn thing to rotate on my circle.

What part of the equation do I have to change to make it start and move on my circle?

Sorry, I probably sound retarded, but this is bugging the crap outta me. :mad:
 
Multiply the sine and cosine of the angle by the radius of your circle, and add the center point's x and y values to sphereX and sphereY respectively.
 

Cmonkey123

Wanderer
Urg!

It's still not working... It keeps rotating off the circle even if I add the center points x and y of the circle to the sin and cos.

Here's the entire script, maybe I'm doing something else wrong:

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace rotation_test
{
	public class rotationtest : System.Windows.Forms.Form
	{
		Timer rotationtimer = new Timer();

		Image sphere;
		Bitmap offScreenBuffer;

		float sphereX, sphereY;
		
		double rotationfactor;

		const string sphere_file_name = "..\\..\\sphere.ico";

		private System.ComponentModel.Container components = null;

		public rotationtest()
		{
			sphere = Image.FromFile( sphere_file_name );

			InitializeComponent();
		}

		public void RotationTimerTick( object sender, System.EventArgs e )
		{
			rotationfactor += 0.1;

			sphereX = 100 * (float)Math.Cos( rotationfactor ) + ( ( this.ClientSize.Width / 2 ) - 100 );
			sphereY = 100 * (float)Math.Sin( rotationfactor ) + ( ( this.ClientSize.Height / 2 ) - 100 );

			Invalidate();
		}

		public void DoPaint( Graphics g, PaintEventArgs e )
		{
			System.Drawing.Pen ellipsepen = new Pen( Color.Red, 1 );

			g = e.Graphics;
			base.OnPaint( e );

			g.DrawImage( sphere, sphereX, sphereY );
			g.DrawEllipse( ellipsepen, ( this.ClientSize.Width / 2 ) - 100, ( this.ClientSize.Height / 2 ) - 100, 
				200, 200 ); 
		}

		private void InitializeComponent()
		{
			// 
			// rotationtest
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.Black;
			this.ClientSize = new System.Drawing.Size(792, 466);
			this.ForeColor = System.Drawing.Color.Black;
			this.MaximumSize = new System.Drawing.Size(800, 500);
			this.MinimumSize = new System.Drawing.Size(800, 500);
			this.Name = "rotationtest";
			this.Text = "rotation test";

			rotationtimer.Enabled = false;
			rotationtimer.Interval = 30;
			rotationtimer.Tick += new System.EventHandler( RotationTimerTick );
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		protected override void OnKeyDown( KeyEventArgs e )
		{
			string input = e.KeyData.ToString();

			switch( input )
			{
				case "Enter":
					rotationtimer.Enabled = true;
					break;
			}

			Invalidate();
		}

		protected override void OnPaint( PaintEventArgs e )
		{
			Graphics gOffScreen;
			Graphics g = e.Graphics;

			if( offScreenBuffer == null || offScreenBuffer.Width != this.ClientSize.Width || offScreenBuffer.Height != this.ClientSize.Height )
			{
				offScreenBuffer = new Bitmap( this.ClientSize.Width, this.ClientSize.Height );
			}
			
			gOffScreen = Graphics.FromImage( offScreenBuffer );
			DoPaint( gOffScreen, e );
			gOffScreen.Dispose();

			g.DrawImage( offScreenBuffer, 0, 0 );
		}

		[STAThread]
		static void Main() 
		{
			Application.Run( new rotationtest() );
		}
	}
}
 

David

Moderate
lol, sorry man, I tried to get some help for you. My daughter was just exempted from her trig final and I asked her if she could help. All I got was something about living the rest of her life without trig? What's that all about? :D
 

Cmonkey123

Wanderer
Haha, you're telling me :(

but seriously though, why isn't it working? It should be rotating on the red circle, but it's not; it's about 100 pixels off in the x and y directions... the client size thing/2 -100 should be the right spot, so what's wrong with it?
 

Kamron

Knight
To get a circle, COS(X) + SIN(Y) = 1 where X and Y are two numbers less than 1.

So COS and SIN of the same number is wrong...

thats my hint to you ;)

If you need more help, please ask.
 
XxSP1DERxX said:
To get a circle, COS(X) + SIN(Y) = 1 where X and Y are two numbers less than 1.

So COS and SIN of the same number is wrong...

thats my hint to you ;)

If you need more help, please ask.

its cos(X)^2 + sin(y)^2 = 1
 

Cmonkey123

Wanderer
Uh... thanks for the help.

But I made a stupid and simple error, I subtracted 100 from the x and y for the sphere's coordinates and it put it at the wrong spot. Subtracting by 16 instead (the radius of the sphere) put it on the right path.
 

p4tRi0t3

Wanderer
from rotation Matrix

Where A is the angle of rotation in degrees and P the coordinate of the sphere
Ra Is the Rotation Matrix
P' is the coordinates of the moved point

Code:
      Ra       *  P    =                P'
( CosA -SinA ) * (x)   =   ( (CosA * x) + (-SinA * y ) )
( SinA CosA  ) * (y)   =   ( (SinA * x) + (CosA * y )  )

that's give us :

Code:
sphereX = ( (float)Math.Cos( 20 ) * x ) + ( -(float)Math.Sin( 20 ) * y ) + ( ( this.ClientSize.Width / 2 ));
sphereY = ( (float)Math.Sin( 20 ) * x ) + ( (float)Math.Cos( 20 ) * y )+ ( ( this.ClientSize.Height / 2 ));

make your sphere start at

Code:
sphereX =this.ClientSize.Width / 2 + 100;
sphereY =this.ClientSize.Height / 2

You should see your sphere in circular motion
 

Kamron

Knight
TheOutkastDev said:
its cos(X)^2 + sin(y)^2 = 1

Ya, it was a simple typo... I had the logic correct in my head.

That is the actual formula that you use, correct? Where 1 = the radius, and you change the formula as needed. The formula then turns into X^2 + Y^2 = 2R, blah blah.

I think I provided incorrect information anyways.... Because I realized you wanted it to rotate in a circle. Or did I give correct information?
 
Top