Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 1.0 (and below)

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-11-2004, 08:15 PM   #1
fuzion
Registered User
 
Join Date: Mar 2003
Posts: 5
Default How do i move an object by angle?

Hello guys!

Im pretty new to flash and trigonometry, and when i read the "detecting the mouse angle"-tut, i wanted to create a minigolf-style game. I have created the mouse-angle MC, and made it return values between 0 and 360 degrees.

My problem now, is that i want the ball to be shot in the selected angle - no powermeter yet Lets say i stop the rotation at 120 degrees (it would then strike the top left corner), how do i get the ball to move, from where its located, by an angle of 120 degrees and the exit the screen?

To simplify my question: I want to move an object by 120 degrees from its current location. How do i do that? The movement must be animated

Hope you understood my quasi-english guys! Greetings from a stupid norwegian
fuzion is offline   Reply With Quote
Old 08-12-2004, 06:27 AM   #2
Laguana
Well known nobody
 
Join Date: Jul 2004
Posts: 818
Default

I think i know what you mean. As for the animation, that's just you making a movie clip which has an animated bit and telling it to play it at the opportune moment.

For the 120 degrees bit, trigonometry comes into it. What we need to do is find out what x and y changes need to be made to go in the direction of 120 degrees. For the following i will assume a total movement of 100 per move.

We have an angle of 120 degrees, which when taken from the x axis means it's pointing up and left. This means that the y difference should be positive, and the x negative. the y difference will need to be inverted as flash does things upside down, but that comes later. if you recall a right angled triangle, the long bit is called the hypotenuse. in this case it is 100. trig says that sin theta is the opposite side over the hypotenuse. This means that the opposite is the hypotenuse times theta, or 100 * Sin Theta. The y value is therefore the power of 100 times Math.Sin(angle). Cos does the adjacent side over the hypotenuse, and so the x value is 100 times Math.Cos(angle)

If you didn't understand, that, i'll simplify it here:

ChangeX = Power * Math.Cos(angle)
ChangeY = (Power * Math.Sin(angle)) * -1

on(enterFrame) {
ball._x = ball._x + ChangeX
ball._y = ball._y + ChangeY
}

I think that that should all work, but i'm not sure if that's flash's sine and cosine syntax. Hope it helps
Laguana is offline   Reply With Quote
Old 08-12-2004, 09:20 AM   #3
fuzion
Registered User
 
Join Date: Mar 2003
Posts: 5
Default

You have no idead about how grateful i am... Thank you so f***** much!

I guess i cant get anyway without trigonometry
I actually figured out the idea, but since im so fresh to flash and trigonometry, i didnīt manage to write the code myself. I actually tried to solve the problem with if()-brackets

Thanks! Be prepared for more questions, the trigonometry-noob is writing a game
fuzion is offline   Reply With Quote
Old 11-13-2004, 02:54 PM   #4
Hybrid-Halo
Registered User
 
Join Date: Nov 2004
Posts: 20
Default

I have a question that *appears* to be very similiar to this.

I have a character object also facing my mouse and I need to get a bullet object from the character to the mouse position. Variables named "dist_x and dist_y" are already telling me the distance from the origin to the mouse, I'm just trying to get the code right on the bullet so that when it's duplicated at the origin it'll move in the direction of the mouse position.

Here's the code for the bullet object I have currently;

//Speed
onClipEvent (load)
{
BulletMoveSpeed=100;
//birthplace
this._y=_root.player1._y+25;
this._x=_root.player1._x+30;
}

//direction
onClipEvent (enterFrame)
{
this._x = this._x +
this._y = this._y +


//removal settings
if (this._x>600)
{
this.removeMovieClip();
}
}

I've bolded the parts that I really need the help with/think where the code will need to go. I'm not particuarly used to actionscript yet and sorting this part of the code out will help greatly so I can move on and actually start drawing proper graphics and scripting the game.

Thanks in advance for any help, this ones been doing my head in for a week or so now
Hybrid-Halo is offline   Reply With Quote
Old 11-14-2004, 01:38 AM   #5
Laguana
Well known nobody
 
Join Date: Jul 2004
Posts: 818
Default

Ok, the way I see your problem is that there are two ways of doing it, depending on the outcome you want. You can either pick a time for hte bullet to take, or a speed for the bullet to go at. The time is easier, but it seems you want to use the speed, which is fine.

This will use some stuff i learnt in advanced maths extended. Yay for me :P

You've already got the distance from your character to the point on both the x and y axis, which is good. If you drew a triangle from one point to the end of those two lines, or vectors as they are, the distance of the line you draw is the square root of the sum of the squares, due to pythagorus theorum. a^2 = b^2 + c^2 if you know what i mean.

distance = Math.sqrt(Math.pow(dist_x, 2) + Math.pow(dist_y, 2))

that just takes the square root of the square of the x + the square of the y.

Then we divide each of those by this distance, to get what is known as the "unit vector" which is effectively just an angle, but not quite

unit_x = distance_x / distance
unit_y = distance_y /distance

Then all you need to do, is every time you want the bullet to move, you want it's x to be increased by the speed * unit_x and the y to be increased by speed * unit_y

I hope this makes sense to you. The end result should look something like this:

ActionScript Code:
onClipEvent(load) { BulletMoveSpeed  = 100 this._x = _root.player1._x +30 this._y = _root.player1._y +25 distance = Math.sqrt(Math.pow(dist_x, 2) + Math.pow(dist_y, 2)) unit_x = dist_x / distance unit_y = dist_y / distance } onClipEvent(enterFrame) { this._x = this._x + (unit_x*BulletMoveSpeed) this._y = this._y + (unit_y*BulletMoveSpeed) }

Hope this helps
Laguana is offline   Reply With Quote
Old 11-14-2004, 03:46 AM   #6
Hybrid-Halo
Registered User
 
Join Date: Nov 2004
Posts: 20
Default

That does make sense, but I couldn't implement it :/ I really don't think I have enough experience or intelligence to do that.

But I have got it working a different way, my character is always rotating to face the mouse, since the bullet movie duplicates on the characters location, I set the bullet,_rotation property to mirror the player rotation and inside the bullet movieclip I added another symbol named "slug" which moves instead of the bullet.

So my code is now;

onClipEvent (enterFrame)
{
this.slug._x+=BulletMoveSpeed;
}

which works so far, may have to re-address this issue if problems arise, thankyou very much for your input
Hybrid-Halo 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 Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:38 PM.

///
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.