Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 12-11-2005, 11:05 PM   #1 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default 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.

Last edited by Cmonkey123; 12-11-2005 at 11:10 PM.
Cmonkey123 is offline   Reply With Quote
Old 12-11-2005, 11:17 PM   #2 (permalink)
Forum Expert
 
TheOutkastDev's Avatar
 
Join Date: Sep 2002
Location: Houston, Texas
Age: 22
Posts: 3,933
Default

You're casting it to an int. You need to do floats, or doubles because Cos( 20 ) is always less than 1.
TheOutkastDev is offline   Reply With Quote
Old 12-11-2005, 11:45 PM   #3 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

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?
Cmonkey123 is offline   Reply With Quote
Old 12-12-2005, 12:11 AM   #4 (permalink)
The noob formerly known as Jakob
 
Serp's Avatar
 
Join Date: Jan 2005
Posts: 316
Default

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. :-)

Last edited by Serp; 12-12-2005 at 12:14 AM.
Serp is offline   Reply With Quote
Old 12-12-2005, 12:16 AM   #5 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

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 is offline   Reply With Quote
Old 12-12-2005, 12:20 AM   #6 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

I'm way past trig, but I never learned circular motion... haha.

I think I got it rotating right now, thanks much.
Cmonkey123 is offline   Reply With Quote
Old 12-12-2005, 12:25 AM   #7 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

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.
Cmonkey123 is offline   Reply With Quote
Old 12-12-2005, 04:04 AM   #8 (permalink)
Forum Expert
 
TheOutkastDev's Avatar
 
Join Date: Sep 2002
Location: Houston, Texas
Age: 22
Posts: 3,933
Default

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.
TheOutkastDev is offline   Reply With Quote
Old 12-12-2005, 10:46 PM   #9 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

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() );
		}
	}
}
Cmonkey123 is offline   Reply With Quote
Old 12-13-2005, 12:55 AM   #10 (permalink)
Moderate
 
David's Avatar
 
Join Date: Nov 2002
Location: USA
Posts: 6,598
Default

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?
__________________
David Forum Moderator
The RunUO.com Forum Moderator Team

Forum Rules and Guidelines
RunUO Forum Search Engine
Download RunUO 2.0 RC2
David is offline   Reply With Quote
Old 12-13-2005, 04:33 PM   #11 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

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?
Cmonkey123 is offline   Reply With Quote
Old 12-13-2005, 11:40 PM   #12 (permalink)
 
Join Date: Oct 2002
Age: 23
Posts: 4,689
Default

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 is offline   Reply With Quote
Old 12-14-2005, 12:44 AM   #13 (permalink)
Forum Expert
 
TheOutkastDev's Avatar
 
Join Date: Sep 2002
Location: Houston, Texas
Age: 22
Posts: 3,933
Default

Quote:
Originally Posted by XxSP1DERxX
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
TheOutkastDev is offline   Reply With Quote
Old 12-14-2005, 06:17 PM   #14 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

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.
Cmonkey123 is offline   Reply With Quote
Old 12-18-2005, 12:22 AM   #15 (permalink)
 
Join Date: Dec 2005
Location: Montreal
Age: 23
Posts: 3
Default

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

Last edited by p4tRi0t3; 12-18-2005 at 12:38 AM.
p4tRi0t3 is offline   Reply With Quote
Old 12-18-2005, 01:26 AM   #16 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

I got it figured out a while ago...

Your help is still appreciated though.
Cmonkey123 is offline   Reply With Quote
Old 12-18-2005, 05:55 AM   #17 (permalink)
 
Join Date: Oct 2002
Age: 23
Posts: 4,689
Default

Quote:
Originally Posted by TheOutkastDev
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?
XxSP1DERxX is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5