|
||
|
|
#1 (permalink) |
|
Join Date: May 2003
Posts: 2,848
|
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();
}
Last edited by Cmonkey123; 12-11-2005 at 11:10 PM. |
|
|
|
|
|
#4 (permalink) |
|
The noob formerly known as Jakob
Join Date: Jan 2005
Posts: 316
|
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();
}
Last edited by Serp; 12-12-2005 at 12:14 AM. |
|
|
|
|
|
#5 (permalink) |
|
Join Date: May 2003
Posts: 2,848
|
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();
}
|
|
|
|
|
|
#7 (permalink) |
|
Join Date: May 2003
Posts: 2,848
|
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. ![]() |
|
|
|
|
|
#9 (permalink) |
|
Join Date: May 2003
Posts: 2,848
|
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() );
}
}
}
|
|
|
|
|
|
#10 (permalink) |
|
Moderate
Join Date: Nov 2002
Location: USA
Posts: 6,598
|
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 |
|
|
|
|
|
#11 (permalink) |
|
Join Date: May 2003
Posts: 2,848
|
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? |
|
|
|
|
|
#13 (permalink) | |
|
Forum Expert
Join Date: Sep 2002
Location: Houston, Texas
Age: 22
Posts: 3,933
|
Quote:
|
|
|
|
|
|
|
#14 (permalink) |
|
Join Date: May 2003
Posts: 2,848
|
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. |
|
|
|
|
|
#15 (permalink) |
|
Join Date: Dec 2005
Location: Montreal
Age: 23
Posts: 3
|
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 ) ) 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 )); Code:
sphereX =this.ClientSize.Width / 2 + 100; sphereY =this.ClientSize.Height / 2 Last edited by p4tRi0t3; 12-18-2005 at 12:38 AM. |
|
|
|
|
|
#17 (permalink) | |
|
Join Date: Oct 2002
Age: 23
Posts: 4,689
|
Quote:
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? |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|