View Single Post
# 4 17-01-2008 , 02:44 PM
t1ck135's Avatar
Registered User
Join Date: May 2004
Location: UK
Posts: 1,991
Here you go - again its in actionscript but it shows the principles of using sin and cosine to figure out the position in 3D space and create an orbit. You might need to read around it a bit to get what you want. The main bit you'll be interested in is the loop3D function at the bottom:

Code:
class org.papervision3d.samples.orbit.orbit_test extends MovieClip 
{
	// _______________________________________________________________________
	//                                                                  vars3D
	var container :MovieClip;
	var scene     :Scene3D;
	var camera    :FreeCamera3D;
	var sphere    :Ase;

	// _______________________________________________________________________
	//                                                                    Main
	function orbit_test()
	{
		init3D();

		this.onEnterFrame = loop3D;
	}


	// _______________________________________________________________________
	//                                                                  Init3D
	function init3D()
	{
		// Create container movieclip and center it
		container = this.createEmptyMovieClip( "container", this.getNextHighestDepth() );
		container._x = 320;
		container._y = 240;

		// Create scene
		scene = new Scene3D( container );

		// Create camera
		camera = new FreeCamera3D();

		// Create material
		var texture  :BitmapData = BitmapData.loadBitmap( "Bitmap" );
		var material :BitmapMaterial = new BitmapMaterial( texture );

		material.oneSide = true; // Make it single sided

		// Load sphere
		sphere = new Ase( material, "world.ase", .1 );
		sphere.rotationX = 0;
		
		sphere2 = new Ase( material, "world.ase", .025 );
		sphere2.rotationX = 0;
		sphere2.rotationZ = 45;
		// set the moon to be 125 away from the planet
		sphere2.x = sphere.x + 125;
		sphere2.y = sphere.y + 125;
		sphere2.radians = 0.785
		sphere2.degrees = 45;
		
		sphere3 = new Ase( material, "world.ase", .025 );

		sphere3.x = sphere.x + 125;
		sphere3.y = sphere.y + 125;
		sphere3.z = sphere.y + 125;
		sphere3.radiansX = -2;
		sphere3.radiansY = 0;
		sphere3.radiansZ = 0;
		
		sphere4 = new Ase( material, "world.ase", .025 );
		sphere4.x = sphere.x + 0;
		sphere4.y = sphere.y + 176;
		sphere4.radians = 0
		sphere4.degrees = 0;
		
		// Include in scene
		scene.push( sphere );
		//scene.push( sphere2 );
		scene.push( sphere3 );
		//scene.push( sphere4 );
	}

	// _______________________________________________________________________
	//                                                                    Loop
	function loop3D()
	{
		// calculate the distance between the two (based on the origin being the planet at 0,0,0
		dist = Math.sqrt((sphere2.x * sphere2.x) + (sphere2.y * sphere2.y));
		_root.distance.text = dist;
		// calculate distance between two (z axis ball)
		distz = Math.sqrt((sphere3.x * sphere3.x) + (sphere3.y * sphere3.y));
		
		angle = sphere2.radians;

		sx_pos = Math.sin(angle) * dist;
		sy_pos = Math.cos(angle) * dist;
		
		sphere2.x = sx_pos;
		sphere2.y = sy_pos;
		
		sphere2.radians += 0.01;
		sphere2.radians += 0.01;
		sphere2.radians += 0.01;
		
		// combined
		anglex = sphere3.radiansX;
		angley = sphere3.radiansY;
		anglez = sphere3.radiansZ;
		sx_pos = Math.sin(anglex) * dist;
		sy_pos = Math.cos(anglex) * dist;
		cx_pos = Math.sin(anglez) * dist
		cz_pos = Math.cos(anglez) * dist
		vy_pos = Math.sin(angley) * dist
		vz_pos = Math.cos(angley) * dist
		sphere3.x = sx_pos;
		sphere3.z = cz_pos;
		sphere3.y = vy_pos;
		sphere3.radiansX -= 0.03;
		sphere3.radiansY += 0.03;
		sphere3.radiansZ += 0.03;

		sphere3.rotationY = sphere3.rotationY + 0.2;
		
		//sphere.rotationX = sphere.rotationX; //container._ymouse /3;
		sphere.rotationY = sphere.rotationY + 0.4;

		// Render
		scene.renderCamera( camera );
	}
	
	function toRadians(degree:Number):Number {
		return degree / 180 * Math.PI;
	}
	
	function toDegrees(radian:Number):Number {
		return radian * 180 /Math.PI;
	}
}
and an example of 2 moons orbiting around a planet (in 3D in flash) - although the camera angle makes it look 2D. Minimise the page to make it run faster:
https://www.flash-fx.net/3D/masters/i...on3D/orbit.swf

Simon


Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net

Last edited by t1ck135; 17-01-2008 at 02:49 PM.