AS Trig Help
Hey can anyone tell me the formula for rotating an object based on 90 degrees. For example, I only want the characters rotation values to be 0, 90, 180, or 270 based on the angle between the mouse and an object. I used to know a formula that worked right into the atan2 method, however sadly I had forgotten it. Can anyone help me?
KirupaForum > Flash > ActionScript 1.0/2.0
Posted on: 07-08-2007, 02:36 PM
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Trig Help
I need to make something that always points towards the mouse. I have done everything and made a right angled triangle with the straight line from the object to the mouse as the hypotenuse. I have found the tan of the angle using x and y co-ordinates but got stuck there.
With Flash 5 actionscript, there is only a command to find the tan of a number using Math.tan(number) but not to find the number if you have its tan.
Please help.
[F8] Trig
Why does my program think
Code:
Math.sin(45)
equal 0.850903524534118?
I want it to equal one like it should.
Need A Little Help With This Trig...
I need some help with my math for this rotating menu. Please download the attached file and take a look. I have a menu with 8 buttons that rotates around a central point (obviously). What I want to happen is when the user clicks on a button, the menu rotates until the button they clicked is in the top position. At this point I almost have it working but I can't quite get it right.
When I init the menu, I attach a target clip to each button. When a button is clicked, it calls a function which attaches a script to the menu. The script calculates the angle between the button's target clip and a separate clip on the stage (point_mc) (click the "draw triangle" check to see the lines). The following code moves the menu (this._tab is a property referring to the target clip of the button which was clicked):
ActionScript Code:
gear.onEnterFrame = function(){
//this._rotation += 2;
var coords:Object = {x:this._tab._x, y:this._tab._y};
this.localToGlobal(coords);
var xd_1:Number = _root.point_mc._x - coords.x;
var xd_2:Number = coords.x - _root.point_mc._x
var yd:Number = coords.y - _root.point_mc._y;
var mult:Number = 1.5;
var y_lim:Number = 100;
if(_root.check_mc.selected){draw_triangle(this, coords)} else {_root.line_mc.unloadMovie();}
var radians:Number = Math.atan2(xd_2, yd);
var deg:Number = (radians * 180/Math.PI );
trace(deg + " degrees");
if(deg <= -2 || deg >= 2)
{
//trace(yd);
this._rotation -= (deg / 5);//this._speed;
} else {
trace("DENIED: " + deg);
delete this.onEnterFrame;
}
}
This code works for all the buttons except for the one at the very bottom position (-90 degrees?). Right now the script isn't very precise and sometimes causes the rotation to stop on a funny angle. Also, performance is a big factor for this deliverable so I want to keep calculations to a minimum in here so as to not use up too much CPU bandwidth. Can any of you please take a look and let me know either what I am doing wrong or a better way to do this?
thanks!
-Rhino
Help With A Little Trig
Hi guys
I've created a circular preloader with the moviecliploader function and it all works very well so far. I've enclosed the zip for you to have a look - you just need to create yourself a very large test.swf for it to preload.
Its my first outing with flash trigonometry, so I'm a little fresh with it all. I based the code around something I found on another forum. But I wasn't convinced that the guy's trig was correct (he was using sin for x and cos for y, which goes against everything that I thought that I knew), so I rewrote it to something like the following...
ActionScript Code:
xPos = this.centerX + Math.cos(this.angle) * this.maskRadius;
yPos = this.centerY + Math.sin(this.angle) * this.maskRadius;
maskMC.lineTo(xPos, yPos);
degrees = Math.round((loadedBytes/totalBytes)*360);
this.angle = degrees * Math.PI / 180;
...which sits in the onLoadProgress part of the moviecliploader and updates every time a byte loads (I think!).
Anyway, it all works cool - but the problem I'm having is that the circle begins drawing from the right side at 3 o'clock and I really want it to start from the top at 12 o'clock...can someone please help me make the adjustment so I can achieve this? I thought it was going to be as easy as adding 270 degrees, but that just has really odd results!
Please help if you can! Many thanks in advance.
Help With Trig
Hey,
I've never been good with trig. It is something i keep meaning to get better at. Anyway, attached is a sample of what I'm trying to make along with the code below.
Basically, I'm creating a circle of balls along with some text. I need the text to be in nice rotation along with the balls rotation. Additionally, it would be super if the text lined up better with the balls than it does now. If at all possible, I would like to avoid nesting the text into a container MC and rotation that. I need the memory.
Can anyone help me out? I promise not to be a script kiddy and I'll actually study your code
Cheers
ActionScript Code:
/**
* add "Times New Roman" font to the library so it can be embeded
* which in turn can be rotated
*/
var tf:TextFormat = new TextFormat();
tf.font = "Times New Roman";
tf.size = 12;
for (var i:Number=0; i<20; i++)
{
var mc:MovieClip = _root.createEmptyMovieClip("mc"+i, i);
mc.lineStyle(10, Math.random()*0xFFFFFF);
mc.lineTo(0.5, 0);
var degrees:Number = 360/20 * i;
var radians:Number = degrees * (Math.PI/180);
mc._x = 200 * Math.cos(radians) + Stage.width/2;
mc._y = 200 * Math.sin(radians) + Stage.height/2;
var txt:TextField = mc.createTextField("txt", 0, 0, 0, 0, 0);
txt.autoSize = true;
txt.embedFonts = true;
txt.setNewTextFormat(tf);
txt.text = "degrees " + Math.round(degrees);
if (degrees<=90)
txt._rotation = degrees;
if (degrees>=270)
txt._rotation = degrees;
if ( degrees > 90 && degrees <= 180)
{
txt._x -= txt._width;
//txt._rotation = degrees - 180;
}
if ( degrees > 180 && degrees < 270)
{
txt._x -= txt._width;
//txt._rotation = degrees - 180;
}
}
Trig
well, i was working on a football engine awhile back, an american football one, and i had it working well until i got to passing, long passes work just fine, but you get into short passes if statements that detect the x and y distances and lure them in at both x and y intervals dont work any more. if that makes any sense, anyways i was told i need some sort of trig function to bring the ball in a realistic looking manner
overview: you are a player, with the ball. you press space and the ball goes from one player to a player down field, my method works, but it just doesnt look right, please help
Help From Sen On Trig Tut
Sen,Could you please give me the source code for the part in your Trig tut where it says has a circle spinning around and the text underneath says:
[ sine and cosine for rotation ]
Bluemoon93
[FMX]Trig
ok this is what i have so far i want to make so ther is a triangle that changes shape like in Sen's Trig tut.
Code for everything(goes in frame):
ActionScript Code:
var originx = 275;var originy = 200;var radius = 150;var angle = 100;onEnterFrame = function () { _root.clip._x = originx+Math.cos(angle)*radius; _root.clip._y = originy+Math.sin(angle)*radius; _root.angle += .05; _root.createEmptyMovieClip("triangle", 1); _root.triangle.lineStyle(1, 0x000000, 100); _root.triangle.moveTo(originx, originy); _root.triangle.lineTo(_root.clip._x, _root.clip._y);};_root.createEmptyMovieClip("triangle2",1) _root.triangle2.lineStyle(1,0xff0000,100) _root.triangle2.moveTo(originx,originy) _root.triangle2.lineTo(_root.clip.Math.cos)
Trig Help
Hi,
I'm working on a class that basically takes a given rectangle or square and fills that square with spaced diagonal lines, which I then animate.
I've got it to work via brute force kinda approach, but I draw far more lines than I need and it becomes processor heavy.
So I'm trying to work out how to use Trig to start at say the bottom left of a square and iterate up the height etc of that square, drawing a single line (hypotenuse) across the square using a given angle for the line. Because I move the lines a little, there needs to be some overlap to allow for this movement.
Anyone got any idea how to do this? My trig brain has died and I can't seem to figure it out.
I'm trying to draw something like this http://www.noponies.com/lines.gif
Thanks in advance.
Ok, here is the class I'm using...
PHP Code:
package {
import flash.events.TimerEvent;
import flash.utils.Timer;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.display.*;
public class angledLines2 extends Sprite {
//vars
private var xpos:int;
private var ypos:int;
public var hsize:int;
public var vsize:int;
private var angle:int;
private var strokeColor:uint;
private var strokeWeight:int;
private var bgColor:uint;
private var speed:int;
private var spacing:int;
private var mult:int;
private var inc:int;
//vars passed from instance call
public function angledLines2(hsize, vsize, angle, strokeColor,strokeWeight, bgColor, spacing, distance, speed) {
//convert angle to radians
var radians = angle* (Math.PI/180);
//create a mask
var tempMask:Shape = new Shape();
tempMask.graphics.beginFill(0x000000,90);
tempMask.x = 0;
tempMask.y = 0;
tempMask.graphics.drawRect(0, 0, hsize, vsize);
tempMask.graphics.endFill();
//create a shape to draw into
var lines:Shape = new Shape();
lines.graphics.lineStyle(strokeWeight, strokeColor, 100);
lines.x = 0;
lines.y = 0;
//conditional switch based on what size we want our shape.
if(hsize >= vsize) {
mult = hsize
//position to draw from
inc = -hsize-distance;
}else{
mult = vsize
//position to draw from
inc = -vsize-distance;
}
//draw the lines
function drawLines() {
for (var i=1; i <= mult/(spacing/2); i++) {
inc+=spacing
lines.graphics.moveTo(inc, 0);
lines.graphics.lineTo(((vsize)* Math.sin(radians))+inc, vsize);
}
var myTween:Tween = new Tween(lines, "x", None.easeIn, 0, distance, speed, true);
myTween.looping = true;
lines.cacheAsBitmap = true;
tempMask.cacheAsBitmap = true;
addChild(lines);
addChild(tempMask);
//comment out the mask to see how many lines are drawn
lines.mask = tempMask;
}
drawLines()
}
}
}
Which I call from the main timeline like so:
var bob:angledLines2 = new angledLines2(600, 600, 50, 0xFF00FD,2, 0x00FF00, 10, -30, 4);
bob.x = 100
bob.y = 100
bob.mouseEnabled = false
addChild(bob);
Help From Sen On Trig Tut
Sen,Could you please give me the source code for the part in your Trig tut where it says has a circle spinning around and the text underneath says:
[ sine and cosine for rotation ]
Bluemoon93
[FMX]Trig
ok this is what i have so far i want to make so ther is a triangle that changes shape like in Sen's Trig tut.
Code for everything(goes in frame):
ActionScript Code:
var originx = 275;var originy = 200;var radius = 150;var angle = 100;onEnterFrame = function () { _root.clip._x = originx+Math.cos(angle)*radius; _root.clip._y = originy+Math.sin(angle)*radius; _root.angle += .05; _root.createEmptyMovieClip("triangle", 1); _root.triangle.lineStyle(1, 0x000000, 100); _root.triangle.moveTo(originx, originy); _root.triangle.lineTo(_root.clip._x, _root.clip._y);};_root.createEmptyMovieClip("triangle2",1) _root.triangle2.lineStyle(1,0xff0000,100) _root.triangle2.moveTo(originx,originy) _root.triangle2.lineTo(_root.clip.Math.cos)
Trig & Ball
I am in the process of improving a game of mine(check out my site game is called 'gun')
I want to make a moving ball hit the inside of a circle and bounce off at the reflected angle. (in relation to my game, it is so the player can't trap the ball in a corner and gain lots of points in a 'cheating' manner.) I have spent the last couple of hours trying different trig approaches. Some of them worked to a degree(pun not intended ), but I think that was just luck on where it hit the inside of the circle.
For coding purposes use x_v as x velocity and y_v as y velocity.
Thanks in advance. Jordz
Trig Problems PLEASE HELP
i need help making a rake and trail calculator like the one on this site(click rake and trail on the top menu):
perseperformance.com
i have an almost complete working version, but i dont know enough trig to get it to work correctly.
if you can help i would really appreciate it!
Trig Problems
hi
I have 2 points, how can I find the bearing from one to another?
thx all help appreciated
Trig Values
Can anyone tell me why the following code throws erroneous values when the variable rotate equals a multiple of 90?
sine = (math.sin(math.pi/180 * rotate))
cosine = (math.cos(math.pi/180 * rotate))
tan = (math.tan(math.pi/180 * rotate))
Also how can the number of decimal places be set?
Any help would be much appreciated.
Paul
Simple Trig...?
I have a line and it appears that i cannot make-it face the mouse, and allways have the right rotation...
here it is: http://www.games4fun.evonet.ro/trig/simpleTrig.fla
any ideeas why?
tnx in advance,
ovidiu
Trig Problem
Hi,
I have been using the script below as a simple exercise to get the distance between 2 clips traced into a dynamic text box.
Although the whole thing seems to work as intended at certain points the text field traces out “NaN” which I know means “Not a number” I would imagine that this has something to do with the Trig function of the square on the hypotenuse being = to the sum of the 2 squares on the other 2 sides, but can’t quite see why it is breaking down at these sectors as it seems to work through 360o with just a blip at about 10o through to 90o (standard 360 measure eg not interpreted through Flash) & 230o –270o Anyone know a way round this?
Fla attached.
code: function drag() {
this.startDrag();
}
function topDrag() {
this.stopDrag();
}
function tracy() {
distance = Math.sqrt((point2._x-point1._x)*(point2._y-point1._y)+(point2._y-point1._y)*(point2._y-point1._y));
hold.clip.dym_txt.text = distance;
clip2.dym_txt.text = distance;
}
point1.onPress = drag;
point2.onPress = drag;
point1.onRelease = topDrag;
point2.onRelease = topDrag;
setInterval(tracy, 10);
Simple Trig Help
The following code is driving me mad why is my trace cos value not zero but -1.836909530733566e-16 please im going out of my head checked it on 2 calculators gives 0 which is correct please help cheers.
var a:Number=(270*Math.PI)/180;
var xx:Number=Math.cos(a);
var yy:Number=Math.sin(a);
trace("cos x = "+xx); // why is this value not zero
trace("sin y = "+yy); // this is correct displaying -1
[F8] Flash Trig
http://dmwdev.com/flash_ex1.html
I created this thing yesterday.
Its sort of cool trig - If anyone finds themselves bored in a college class i recommend you try to create one on notebook paper.
I remember someone making one of these on this forum (probably years ago),
and I thought I remembered someone calling it a fancy name.
Does anyone know if this thing has a name?
Actionscript 2.0 + Trig
Well, right now im having trouble with my program. I want to make my homepage for my site, and I thought of a unique idea (Or I have not seen it).
I want the site to start off with tiny circles (100px radius) in a large circle (Based on width or height of the flash).
Right now I got it working, kind of. The smaller circles seem to "bunch up" in pairs or groups of 3. I uploaded the flash, and I will post the actionscript of the object (Using OOP).
Link to flash: http://dudeami.com/Opening.swf
Actionscript:
Code:
// Action script for the welcome screen of DudeAmI.com
// Shows a circle with a few icons on it, then explains with one clikc
// Goes to with two clicks (Or on the "Enter" button)
class IconRing extends MovieClip {
function begin() {
// What to do on creation
// Create the circles, base on a number for now
var circles = 10;
// Compute the size and location of each circle...
var cirWidth = 50;
trace(cirWidth);
var angle = 360/(circles);
var cirRadius = ((400)/2)-cirWidth;
var col = new Array(0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666, 0x777777, 0x888888, 0x999999);
for (var i = 0; i<circles; i++) {
var curAngle = angle*(i);
lineStyle(1, col[i], 100);
// Now that curAngle is setup, compute where the next goes
//beginFill(col[i], 100);
drawX(cirWidth/2, Math.cos(curAngle)*cirRadius, Math.sin(curAngle)*cirRadius);
//endFill();
trace("Drawing, CurAngle = " + curAngle + ", Width = "+(cirWidth)+", X = "+(Math.sin(curAngle+180)*cirRadius)+", Y = "+(Math.cos(curAngle+180)*cirRadius));
}
_x = 275;
_y = 200;
}
function drawX(r:Number, x:Number, y:Number):Void {
moveTo(x-r, y-r);
lineTo(x+r, y+r);
moveTo(x+r, y-r);
lineTo(x-r, y+r);
}
function drawCircle(r:Number, x:Number, y:Number):Void {
moveTo(x+r, y);
curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
curveTo(Math.tan(Math.PI/8)*r+x, -r+y, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);
}
}
Any help is appreciated. Im guessing its my trig thats off, I suck at it. The circle function I got from somewhere, and I know it works (Made the drawX to see the exact point).
Thanks!
Trig Stuff
The code below is slightly tweaked from a tutorial on the bit-101.com site. It rotates an object around an axis on in simulated 3-D environment. It works fine, as you'll see if you name a couple of movieclips as is done below. In practice, I'd put the code into classes and make some other structural changes, so ignore that aspect of things.
My questions(s) are about why it works. I'm not looking for a trigonometry tutorial, which I'm looking at in other areas, though if anyone can point to a good one for AS and trig functions, I'm interested. The one at http://www.codylindley.com seems helpful, as is the senocular one. It's been years since I've studied it and current circumstance demands that I pick some of it back up. But there are a few things I don't get.
I've commented some of the code with my sense of what's happening. Any recommenting of and additions to my sample code is welcome.
Thanks to anyone who's interested in a look.
ActionScript Code:
ball.y = 100; // level of orbit on y-axis
ball.speed = 8; // velocity of 'planet' in the orbit
ball.radius = 200; // width of orbit - is it not quite that simple?
ball.xcenter = 200; // i need help on this one. it moves the orbit on the
// x-axis left or right, but I'm not 100% clear on why
// does this represent the center of the x-axis?
ball.ycenter = 0; // same as above
ball.zcenter = 100; // narrows the width of the orbit. why? i understand
// the z value to represent depth. true?
ball.angle = 0; // determines location in the perimeter of the orbit
ball.fl = 200; // focal length
ball2.y = 100;
ball2.speed = 8;
ball2.radius = 200;
ball2.xcenter = 200;
ball2.ycenter = 0;
ball2.zcenter = 100;
ball2.angle = 180;
ball2.fl = 200;
ball.onEnterFrame = ball2.onEnterFrame = function() {
// i know what the following line does, but am not clear on exactly why
z = Math.sin(this.angle * Math.PI / 180) * this.radius + this.zcenter;
// help to set sizes of 'planets' based on their proximity to the viewer's eye
scale = this.fl / (this.fl + z);
// same issue for x as for z
x = Math.cos(this.angle * Math.PI / 180) * this.radius;
// set _x value
this._x = x * scale + this.xcenter;
// set _y value
this._y = this.y * scale + this.ycenter;
// set planet width and height based on scale
this._xscale = this._yscale = scale * 100;
// location in orbit
this.angle += this.speed;
if (this.angle > 359) {
this.angle -= 360;
}
}
Trig Functions
Hello! Does anyone know if there is a way to randomly generate between trig fucntions. For example to arbitarly choose a problem that has sin, cos, or tan. I have searched and cannot find anything relating to this. The only thing I see is
Example:
math.tan(x:Number)
Please Help. Thank you:
I'm Not Good With Trig >__<
Im not very good with trig. I am using this code to get a MC to chase the mouse:
Code:
MC.onEnterFrame = function() {
var s:Number = 5;
var adj:Number = _root._xmouse-this._x;
var opp:Number = _root._ymouse-this._y;
var radians:Number = Math.atan2(opp, adj);
var ang:Number = (radians/Math.PI)*180;
this._rotation = ang;
this._x += xs;
this._y += ys;
xs = s*Math.cos((this._rotation*Math.PI)/180);
ys = s*Math.sin((this._rotation*Math.PI)/180);
};
What would I have to do if I wanted the MC to stop if it was in a 30 pixel radius to the mouse?
Thanks in advance
Math And Trig
I'm looking for a place that I can brush up on my math, more specifically algebra and trig. I have a project that I'm working on that I'm going to be coding a lot of rotation and I don't know where to begin.
Any help would be greatly appreciated!
Thanks
Trig Question
Ok, I've been working on this for ages, I just can't get my head around it.
I've got the following setup :
I need to find out what x1,y1 and x2,y2 are.
I know what xPos and ypos are, as well as xorigin,and yorigin, and the radius of the circle.
The origin and xPos/yPos points move, so they're not going to remain the same unfortunately. Can anyone tell me how to go about this ?
Someone has suggested I find the following angle :
The one on the right hand side, and then find the others. But I'm not sure if this is the best way to go about it. Any help would be needed, my brain just isn't working right now.
Trig Help With Strafing-95% Done [.swf]
Hey ya'll here is my .swf
http://www.alanklement.com/Party.swf
the strafe part comes from the "+30" and "-30". But it just quite right....
Any ideas?
here is the code:
Code:
private function _aimDirection($evt:MouseEvent):void
{
_aimVectorX = stage.mouseX - x;
_aimVectorY = stage.mouseY - y;
_aimVectorXY = Math.atan2(_aimVectorY, _aimVectorX);
rotation = Math.round(_aimVectorXY * 180 / Math.PI);
}
private function _movePlayer($evt:Event):void
{
if(mouseX > 1)
{
switch(_moveDir)
{
case FORWARD:
x += (Math.cos(_aimVectorXY) * _maxSpeed);
y += (Math.sin(_aimVectorXY) * _maxSpeed);
break;
case BACK:
x -= (Math.cos(_aimVectorXY) * _maxSpeed);
y -= (Math.sin(_aimVectorXY) * _maxSpeed);
break;
case STRAFE_LEFT:
x += (Math.cos(_aimVectorXY+30) * _maxSpeed);
y += (Math.sin(_aimVectorXY+30) * _maxSpeed);
break;
case STRAFE_RIGHT:
x += (Math.cos(_aimVectorXY-30) * _maxSpeed);
y += (Math.sin(_aimVectorXY-30) * _maxSpeed);
break;
}
}
}
Other Trig Functions
I was just curious as to whether anyone knows how to use the cot, sec, and csc functions. They aren't natively defined in AS, so I can't use them in my plotting program I'm working on. If anyone knows how to implement them, or somewhere that does, let me know.
Trig Function
Hello! Does anyone know if there is a way to randomly generate trig functions (sin, cos, tan). I am trying to make poblems using these functions. The examples that I see are automatically assigning values to these functions however I want to randomly generate. Example from flash 8 help feature:
tan(x:Number)
I would greatly appreciate it if someone could be of assistance to me. Thanks.
Learning Some Trig
hi guys...doing an exercise with the mouse and some trig...i made this little car. Couple problems: the wheels get off _rotation if you move the mouse horizontaly...They are not parellel to the car, but rather , perpindicular. The reg point on both the wheels is in the center...if that helps. But i think this is an AS issue....how can i adjust for this?
#2: when the car does get to the mouse, it pivots back to it's original position (from authoring time), rather than retaining the rotaional direction in which it was moving.
Code:
Mouse.addListener(sWheel);
Mouse.hide();
sWheel.onMouseMove = function() {
driving = true;
drive();
};
function drive() {
if (driving) {
van.onEnterFrame = function() {
a = Math.atan2(dY, dX);
deg = a*180/Math.PI;
dX = _xmouse-van._x;
dY = _ymouse-van._y;
vx = dX*ease;
vy = dY*ease;
van._x += vx;
van._y += vy;
van._rotation = deg;
steer();
};
}
}
function steer() {
for (i=0; i<wheels.length; i++) {
wheels[i]._rotation = deg;
}
}
thanks for your help
Angle W/ Trig
I have a game that I'm making, and regardless of other stuff, I need to make essentially a turret. I know how to code where it should move towards (that's easy), and I know somewhat how to use math stuff on flash. But when I tried to implement it on this, it doesn't work. See code:
angleA = Math.atan2(TARGET._y-HERO._y, TARGET._x-HERO._x);
var degrees = angleA/(Math.PI/360);
BULLET._rotation = degrees;
It is correctly placed, don't worry about that.
Trig Question ; )
Hi,
I’m having a hard time figuring this one out and I’d appreciate a little help.
It’s a trigonometry Right Triangle question.
Lets say I have a starting point (x1,y1) "A" and a destination point (x2, y2) "B".
I know the length of that side of the triangle "h" (by calculating it) and the length of a second side (witch is arbitrary) "a".
How do I figure out (x3, y3) "C" knowing that the angle at that point will be 90 degrees?
Thanks in advance.
Fun With Trig And Tweens
I've built a fun little menu using some trigonometry and tweens. You can view it here:
http://www.morrise.com/weird.html
and you can get the source here:
http://www.morrise.com/weird.zip
-bmorrise
Trig Help In Flash?
Hi, newbie here in regards to flash's mathTrig functions. I have the ball travelling in the right direction and bouncing off horizontal and verticles movie clips in pretty easy...just reverse the direction of the speed. But I am having trouble bouncing the ball of diagonal lines and getting the correct angle when it comes off.
I was wondering if some of you might help me out. The function that does the calculation is recalc_vector()
code is below....
Derek
function hittest() {
// if I (the lonely lonely ball) am not hitting the movie clip do this
this.hit_test=false;
for (name in course) {
if (typeof(course[name]) == "movieclip") {
// if the ball is hitting the movieclip and I am not calculating
if ( course[name].hitTest( ball._x, ball._y, true )and this.calculating==false ){
// than set calculating to true to allow
this.calculating=true;
//than reset the hit test to be true
this.hit_test=true;
// this function to run
this.recalc_vector();
// get new position after rebound.
startX=new Array();
startX.push(ball._x);
startY=new Array();
startY.push(ball._y);
}
}
}
// if variable hit test is flagged to false
if (this.hit_test==false){
// set calcuating flag to flase
this.calculating=false;
}
}
function recalc_vector() {
theta=Math.atan2((ball._y-startY[0]),(ball._x-startX[0]));
trace(theta);
//ball_delta = ball.speedx / ball.speedy;
ball.speedx *=-1;
ball.speedy *=-1;
}
function puttBall(){
angle2=_root.percent;
ball._x = ball._x-ballRadius*Math.cos(angle2);
ball._y = ball._y-ballRadius*Math.sin(angle2);
ball.speedx -= speed*Math.cos(angle2);
ball.speedy -= speed*Math.sin(angle2);
// throw the starting ball's x and y position into the array
startX=new Array();
startX.push(ballClip._x);
startY=new Array();
startY.push(ballClip._y);
}
Trig, Triangles, AS3,
Hello All,
I am having a bit of a problem with some "Maths".
I have a right Triangle, the opposite length = stage.stageWidth and the adjacent length = stage.stageHeight.
There is a stage resize, and i want another object (rectangle) to be perpendicular to the hypotenuse.
From this with some trig I can get the β angle of the opposite, and then subtract 90 from this to get the angle of the adjacent. Then subtract that from 180 and it should give me the angle perpendicular to the triangle. Right?
then convert the result into radians.
any help in converting that into AS3 would be great.
Thanks
Jono
Simple Trig Help
It's been a long time since I took trig in high school ( and then i took it after I had gotten into college, so I bombed it )
here's where I'm having problem:
I know the y coordinate, and I can find the angle of the hypotenuse.
What i need is the x coordinate somewhere along that hypotenuse.
so since
tan(angle) = y/x
shouldn't i, using a bit of algebra, be able to find x by:
x = y / tan(angle)
or am i smoking crack?
Trig-tastic
Hi, I am trying to get a movie clip to move from one defined point to another in a curve. But only with action script...
Is this possible?
M@)
Carousel Trig
a client wants a carousel, and i cant talk her out of it. although it seemed easy enought to do, ive run into a brick wall on one point: Math. what she wants is a carousel where an item, when clicked, rotates all carousel items until the chosen candidate is at the front of the carousel (in other words stage middle and the lowest y coordinate of all items). i really did look through all 2789 carousel entries, as well as numerous carousel examples in other tutorial sites, but i dont really understand the math involved. up to this point all ive been able to do is to rotate the selected item to the front of the carousel, but all other items start to play dodge ball with it.
the class where the thing is created and controlled:
Code:
package {
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.filters.BitmapFilter;
import flash.filters.BitmapFilterQuality;
import flash.filters.GlowFilter;
import caurina.transitions.*;
import caurina.transitions.properties.FilterShortcuts;
import CarouselItem;
public class CarouselSmall2 extends MovieClip {
private var item:CarouselItem;
private var items:Array = [];
private var carousel:Sprite;
private var numOfItems:Number = 6;
private var centerX:Number;// = 438; //stage.stageWidth/2 - 74;
private var centerY:Number;// = 226; //stage.stageHeight/2 - 74;
private var radiusX:Number = 160;
private var radiusY:Number = 160;
private var speed:Number;
private var speedDivisor:Number = 8000; //Larger = Slower
private var scaleAtBack:Number = 1; //0.25;
private var scaleAtFront:Number = 1;
private var _xmlLoader:URLLoader;
private var _xmlDataList:XMLList;
private var _xml:XML;
private var _logo:mcLogoInner;
private var _glowFilter:GlowFilter;
public var lang:String;
public function CarouselSmall2(langu:String)
{
lang = langu;
_xmlDataList = new XMLList;
_xmlLoader = new URLLoader();
_xmlLoader.addEventListener(Event.COMPLETE, initCarousel);
_xmlLoader.load(new URLRequest(langu)); // "../xml/menu" + lang + ".xml"));
}
private function initCarousel(event:Event):void
{
_xml = new XML(event.target.data);
_xmlDataList = _xml.children();
carousel = new Sprite();
speed = 0;//.1; *******************************************************************************************************************************
for (var i:Number = 0; i < numOfItems; i++) {
item = new CarouselItem(_xmlDataList[i].attribute("image"), _xmlDataList[i].attribute("label"));
item.link = _xmlDataList[i].attribute("link");
carousel.addChild(item);
//ball.name = "ball" + String(i);
item.angle = i * ((Math.PI * 2) / numOfItems);
//item.addEventListener(MouseEvent.ROLL_OVER, onMenuOver);
item.addEventListener(MouseEvent.CLICK, onMenuOut);
items.push(item);
}
this.addEventListener(Event.ENTER_FRAME, onLoop);
addChild(carousel);
_logo = new mcLogoInner();
_logo.scaleX = _logo.scaleY = 0.55;
_logo.x = stage.stageWidth / 2;
_logo.y = stage.stageHeight / 2;
addChild(_logo);
FilterShortcuts.init();
_glowFilter = new GlowFilter(0x8C99CE, 25, 20, 20, 3, 3, true, false);
//stage bits must be used after something is instantiated
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved); //*******************************************************************************************************************************
centerX = stage.stageWidth / 2 - 74;
centerY = stage.stageHeight / 2 - 74;
_xmlLoader.removeEventListener(Event.COMPLETE, initCarousel);
}
public function changeLanguage():void
{
_xmlLoader = new URLLoader();
_xmlLoader.addEventListener(Event.COMPLETE, changeLabels);
_xmlLoader.load(new URLRequest("../xml/menu" + lang + ".xml"));
}
private function changeLabels(event:Event):void
{
_xml = new XML(event.target.data);
_xmlDataList = _xml.children();
for (var i:int = 0; i < numOfItems; i++)
{
items[i].loadText(_xmlDataList[i].attribute("label"));
}
}
private function onMenuOut(event:MouseEvent):void
{
locked();
dispatchEvent(new MenuOutEvent(true));
//rotateTo(event.currentTarget as CarouselItem, 1.3);
}
/*private function onMenuClick(event:MouseEvent):void
{
locked();
}*/
private function onLoop(e:Event):void
{
items.sortOn("y", Array.NUMERIC);
for (var i:uint = 0; i < items.length; i++)
{
carousel.addChild(items[i]);
items[i].x = centerX + Math.sin(items[i].angle) * radiusX;
items[i].y = centerY + Math.cos(items[i].angle) * radiusY;
items[i].angle += speed;
//items[i].scaleX = items[i].scaleY = scaleAtBack + ((items[i].y - centerY + radiusY)/(radiusY*2)) * (scaleAtFront - scaleAtBack);
}
}
private function mouseMoved(e:MouseEvent):void
{
speed = 0; // -Math.floor((mouseX - (stage.stageWidth/2)))/speedDivisor;
}
private function locked():void
{
//speed = 0;
this.removeEventListener(Event.ENTER_FRAME, onLoop);
}
public function unlocked():void
{
this.addEventListener(Event.ENTER_FRAME, onLoop);
}
}
}
looks like this. any math wizards out there who could at least explain how i should approach this?
Need Some Basic Trig Help
I'm trying to extend my knowledge of Flash and get into more trig so I can be creative with some projects but I'm running into problems already! What I want to be happening is have the ball follow the endpoint of the line that's rotating...but it only does so at certain angles?
Can anyone take a look and possibly tell me why the circle will move to the center of the stage when it hits quadrants IV and II?
I knew I should have paid more attention to this crap in highschool =
View SWF
View Source File
Thanks in advanced for the help! :D
Trig Question For Expert
I have a movie clip in the center of my screen. What would be the correct action script to have it rotate based on the location of the mouse.
I am not very good at trig so any help would be appreciated.
Thanks,
Ginz
The Forgotten Days Of Trig
in action script i want the code to turn the SIN of an angle back into an angle value, it's the inverse SIN i think
Can anybody help me out here
[Edited by multichild on 06-20-2002 at 11:06 AM]
Mouse Angles (Trig?)
Alright Im having a little problem figuring how to have a movie clip (Circle with a radius) move depending on where the mouse is located. The radius is supposed to follow the mouse. Sounds like simple trig but is there an easier way and/or can i get some help with the code?
Expert Trig Question
Hi All,
First - see the attached file!
Based on 10 values i need to draw a polygon.
In the code below I am able to draw a polygon but i need to dynamicly use the values in the array 'arrValues'
PHP Code:
ArrValues=[1,10,7,4,9,1,8,2,6,4];
point = 0;
noOfPoints = 10;
steps = (2*Math.PI)/noOfPoints;
_root.createEmptyMovieClip ("polygon", 1);
with (_root.polygon){
beginFill (0x0000FF, 50);
lineStyle (5, 0xFF00FF, 100);
moveTo (100*Math.cos(point), 100*Math.sin(point));
for (i=0; i<noOfPoints; i++) {
point += steps;
pointX = 100*Math.cos(point);
pointY = 100*Math.sin(point);
lineTo (pointX, pointY);
}
endFill();
}
Any help is rewarded with good good karma
//podenphant
Help With Trig Functions And Other Stuff.
Hi guys.
I'm trying to create a straight line graph (y=mx+c) where you can alter the gradient of the line AND the intercept (where it crosses the y axis).
I'm having various problems with both!! Can someone have a butchers at the attached file and help me fix it up! I'm a bit stumped.
Thanks in advance.
Trig Changing On Imput
man this has me stumped somthing nasty. I really hate maths .
ok what im trying to do is this
(for any one whos played games like worms, worms armagedon etc you'll understand the idea)..
i have a direction in which im launching a projectile.
this then is powered up . the longer the key is held the greater the power, all this code works fine.
but on launching i would like the projectile to folow the first half of a sin curve (the up then down part)
the y=sin X is the curve
the strength of the shot will change the sin curve to
y=(variable here)sinx;
and the angle the shot was fired will change the curve to
y=sin ((variable) X);
now im completely lost as to how to get the projectile to actualy moev along these curves and moreso as to how the curves will be created.
im no good with the api. and my maths is lacking somewhat.
if anyone could give me help in any way id be great full
edit ::
flash 8 only as its a uni assignment.
ActionScript & Trig Problems
I have began an asteroids game for uni, and i have managed to make the space ship move around the stage fine, and i have began to shoot my projectiles from my ship. The only problem i am having is that it appears to be shooting them at a right angle to the direction the ship is facing. And i am not sure as to why.
My actionscript is posted below:
PHP Code:
var speed:Number = 4;
var depth:Number = 0;
onLoad=function(){
//The ship's engine isn't on so there shouldn't be a flame
ship.flame._visible=0;
//The ship is stationary to begin with
xVelocity=0;
yVelocity=0;
maxVelocity=30;
thrust=1;
//The closer this number is to one, the slower your ship will lose momentum
decel=.99;
}
function wrapScreen(){
if(ship._x<-10){
ship._x=559;
}else if(ship._x>560){
ship._x=-9;
}
if(ship._y<-10){
ship._y=409;
}else if(ship._y>410){
ship._y=-9;
}
}
function setShipDirection(){
//Key.isDown returns true if the key specified in parenthesis is being pressed
if(Key.isDown(Key.LEFT)){
ship._rotation-=10;
} else if(Key.isDown(Key.RIGHT)){
ship._rotation+=10;
}
if(Key.isDown(Key.UP)){
if(xVelocity<=maxVelocity){
xVelocity+=thrust*Math.sin(ship._rotation*(Math.PI/180));
}
if(yVelocity<=maxVelocity){
yVelocity+=thrust*Math.cos(ship._rotation*(Math.PI/180));
}
ship.flame._visible=true; }else{
xVelocity=xVelocity*decel;
yVelocity=yVelocity*decel;
ship.flame._visible=false;
}
}
function moveShip(){
ship._x+=xVelocity;
ship._y-=yVelocity;
}
function fireGun(){
if(Key.isDown(Key.SPACE)){
//convert the angle in radians to angle in degrees
var startX:Number;
var startY:Number;
//get next highest depth
++depth;
//set name to projectile plus the value of the next highest depth
var name:String = "projectile"+depth;
//add the movie clip to the current movie
var clip:MovieClip = _root.attachMovie("projectile", name, depth);
//set the x coordinate of the projectile to the x position of the spaceship in the angle the spaceship is facing
clip._x = ship._x;
//set the y coordinate of the projectile to the y position of the spaceship in the angle the spaceship is facing
clip._y = ship._y
startX = ship._x;
startY = ship._y;
clip.xmov = speed*Math.cos(ship._rotation*(Math.PI/180));
clip.ymov = speed*Math.sin(ship._rotation*(Math.PI/180));
trace("Projectile Angle"+angle);
clip.onEnterFrame = function() {
this._x += this.xmov;
this._y += this.ymov;
};
}
}
onEnterFrame=function(){
wrapScreen();
setShipDirection();
moveShip();
fireGun();
}
I hope this will be enough to understand...thanks in advance for your assistance...the projectile is coming out 90 degrees in a clockwise direction to the direction of the ship. I have a feeling this is because of the trig im using but im sure its ok..
Rodent
Trig: Sin Curve Animation
Basically, I have a few problems with the coding that I find difficult.
Here's the code
Code:
// set up constants specific to this movie layout
var WAVESTART:Number = 10; // start point for sine wave
var WAVESTOP:Number = 189;
var WAVEMP:Number = 65; // y=0 point (midpoint of wave height)
// angle changes one radian each frame
var angle:Number = 0;
// set up 180 dots of the sine wave in initial position
for (var i=0; i<180; i++) {
angle -= Math.PI/180*2;
this.attachMovie("dot", "dot"+i, i, {_x:WAVESTART+i, _y:WAVEMP - Math.sin(angle)*50});
}
this.onEnterFrame = function() {
for (var i=0; i<360; i++) {
this["dot"+i]._x = (this["dot"+i]._x >= WAVESTOP) ? WAVESTART : this["dot"+i]._x + 1;
}
};
(in the attachment in fla)
I need help with a few things.
speed up the animation - compled (Thanks Darcey)
1. right now the illusion is going from right-> left. Can i reverse that?
harder:
2. light up the sections that look like m's or w's. i uploaded an example image (seen below) of the 'm' and 'w' highlighted. perhaps after every 3rd pass, highlight the m, then on the 4th highlight the w.
Any suggestions or fla changes are more than appreciated. Thank you guys. This would help me a lot. I am still a noob to actionscripting!
General Trig Question
i am doing a bullet calculation and need to figure out how to find out where two lines intersect
the bullet is fired from a spot and i have a number of walls on the screen. then i check if the bullets path would go through the wall and where to make a bullethole. how would i do this?
any help is greatly appreciated, thank you
Geometry/Trig Tutorials?
Hey guys,
Pixelwit's bobblehead code made me realize just how rusty my geometry/trigonometry skills have gotten in the 14 years since I graduated high school LOL
Does anyone out there have some good links to some refresher websites that I could use to brush up my skills?
Thanks in advance!
tpb
Trig. Equations Needed
I know some basic trig. but how do i find an angle from an anchor point and a mving point? To help you off ...
adjecent = _root.anchor._x-_root._xmouse;
opposite = _root.anchor._y-_root._ymouse;
hypotenuse = ((adjacent ^ 2)+(opposite ^ 2)) ^ 0.5;
If anyone knows thx in advance.
|