Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 08-11-2010, 05:00 AM   #1
Rowland.Maxwell
Registered User
 
Join Date: Mar 2010
Posts: 5
Default An object that orbits another object

Hello,

I have been trying to build a simple shooter game in which the player is moving forward constantly (to simulate flying), but can turn left or right to avoid objects. What I want to do is to have the airplane fixed in place so that when the player presses the keys to turn, the rest of the objects on the screen rotate around the player. I currently have this:

Code:
public function adjustFieldObjects(xSp, ySp) {
			for (i=0; i<fieldObjectsArray.length; i++) {
				var object = fieldObjectsArray[i];
				radius = Math.sqrt(((object.x-fighter.x)*(object.x-fighter.x))+((object.y-fighter.y)*(object.y-fighter.y)));
					//trace("radius",radius);
				if(xRotation != oldXR) {
					
					p = new Point(Math.cos(Math.atan2(object.y-fighter.y, object.x-fighter.x) +(Math.PI/144*xSp))*radius, Math.sin(Math.atan2(object.y-fighter.y, object.x-fighter.x) +(Math.PI/144*xSp))*radius);
					//trace(p);
					object.x = fighter.x-p.x;
					object.y = fighter.y-p.y;
					
				}
				//object.x +=xSp;
				object.y +=ySp;
			}
			oldXR = xRotation;
		}
		}
The problem is that the object seems to jump back and forth 180 degrees constantly. The object moves at a good speed around the player, in a full circle, but I can't for the life of me figure out why the object is jumping back and forth.

Just to break down the code: given the fixed position of the player and the position of the object, the distance between the two is calculated as the radius. The x and y difference between the two is then used to figure out the angle in radians that the two points make. The angle is added to by a set increment, multiplied by 1 or -1 (xSp) to give it a direction. Given the increment, the new angle can then be used to figure out the adjusted position of the object using Math.sin and Math.cos.

Does anyone else have any issues with this? Any suggestions would be greatly appreciated.

Thank you,
Maxwell

Essentially, the
Rowland.Maxwell is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT. The time now is 11:51 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.