Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object rotation from mouse 1

Status
Not open for further replies.

brainpudding

Technical User
Sep 29, 2002
84
US
ok Mr Bill Watson worked this code up for me and it works.. but i have tried tweaking the numbers but i cant get it to work acuratley. what this does is makes a movie clip of a gun that sits in the bottom right hand corner of a 750X450 movie rotate with the cursor .. so the gun is pointing where the cursor is.

gun.onMouseMove = function(){
temp1 = _xmouse;
temp2 = _ymouse;
wildGuess = 30;
len = (650-temp1)*(650-temp1)+(311-temp2)*(311-temp2)
hyp = Math.sqrt(len);
len2 = (650-temp1)/hyp;
angle = Math.sin(len2);
this._rotation = -angle*wildGuess;
}

that works but it dosent follow accuratley

any one have suggestions on tweaking it?
 
It's only fair that Mr Bill Watson would have the first go at it! [bigcheeks]

Regards,

cubalibre2.gif
 
so do you have any ideas on it? the width chagned to 500
 
You need work with arctangent rather than sin:

Code:
convert = function (radians) {
	// convert angle to degrees
	return radians/Math.PI*180;
};
//
gun.onMouseMove = function() {
	x1 = _xmouse;
	y1 = _ymouse;
	x2 = this._x;
	y2 = this._y;
	yDiff = y2-y1;
	xDiff = x2-x1;
	//returns angle in radians
	angle = Math.atan2(yDiff, xDiff);
	angle = convert(angle);
	this._rotation = angle;
};
 
... also worth sticking an updateAfterEvent(); in as the last line of the mouseMove code to smooth out the motion...
 
thanks i am at work now but soon as i get home ill plug that in and try her out.
 
that seems to work for the full range of motion but it is about 45 degress off .. meaning when the mouse is at the top right of the screen, the gun is pointing of 2 about 2 o'clock and when the mouse is at the bottom left its pointing 2 about 10:30
 
OK i got it i just added a -50 to the last line and it works great thanks wang
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top