Adobe Dreamweaver Forums



Last 10 THreads :         Side Bar Appears On Top (Last Post : TravB5 - Replies : 0 - Views : 1 )           »          Webcam shows black box in flash player (Last Post : Masics - Replies : 0 - Views : 1 )           »          CS4: CSS export not working for text with image (Last Post : dv8kiwi - Replies : 12 - Views : 13 )           »          why doesn't my form work? (Last Post : Alan - Replies : 1 - Views : 2 )           »          An exception occurred when instantiating a Com object. (Last Post : BKBK - Replies : 1 - Views : 2 )           »          How do I hide CFIDE when using CF ajax? (Last Post : BKBK - Replies : 5 - Views : 6 )           »          Best way to communicate between custom components (Last Post : ironblunt - Replies : 0 - Views : 1 )           »          remove text area borders (Last Post : webqaflash - Replies : 1 - Views : 2 )           »          PDF HotspotsLinks DO NOT Work (Last Post : Dave Hogue - Replies : 2 - Views : 3 )           »          links from thumbnails (Last Post : Dominic Michael - Replies : 2 - Views : 5 )           »         


Home Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
User Info Statistics
Go Back   Adobe Dreamweaver Forums > Other Macromedia/Adobe Products > Flash > Actionscript
 
Tags:



Reply
  #1 (permalink)  
Old 10-06-2008, 03:54 AM
chinmayagoyal
 
Posts: n/a
Diggs:
Default Character Movement + Rotation

Hello,
I am currently working on a car racing game but I am new to designing the
engine for my game such as the physics for my car. So, I took help from a
tutorial but could'nt quite understand the trigonometry part, can you explain
it.
The part I did not understand was,
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.hit.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
}
}
Hope you can help me out,
Thanking You,
Chinmaya

P.S. I know how to convert degrees to radians and know a little about unit
circles. The main part I dont understand is that why do we multiply the sin of
angle(in radians) to the speed!

onClipEvent (load) {
speed = 0;
}
onClipEvent (enterFrame) {
// make the car go forward
speed += 0.1;
// tells the car to slow down after the speed of 20
if (Math.abs(speed)>20) {
speed *= .7;
}
// you can change the rotation of the car to your desire
if (Key.isDown(Key.LEFT)) {
_rotation -= 15;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 15;
}
// here is where the hittest is for the boundary
speed *= .98;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.hit.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
}
}



Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-06-2008, 04:14 AM
Rothrock
 
Posts: n/a
Diggs:
Default Re: Character Movement + Rotation

I'm not sure if I understand. Do you just need to know why you are multiplying
the speed or is there something that isn't working?

The multiplication is easy. Basically you have some speed, let's say 5. Now if
you are going perfectly to the right along the x axis then you have 5 in the x
direction and you have 0 in the y direction.

If you are going down the screen you have 5 in the y direction and 0 in the x
direction. Easy.

But what if you are going at some angle? That is when it gets a little more
tricky. Well back to your lovely unit circle. If you speed was 1 we have the
same issue for the x and y directions. But if you go off at an angle you've got
your sin component in the x direction and your cos in the y direction. And
there you go, cos(0) = 1 and sin(0) = 0 meaning at an angle of zero all your
motion is in the x direction.

So for a given angle cos shows the amount you've got in the x direction and
sin shows the y direction. That way you can tell your object how many x and y
to move and it looks like movement in at an angle. You multiply them because
you aren't always using a unit circle.




Reply With Quote
  #3 (permalink)  
Old 10-07-2008, 03:13 AM
chinmayagoyal
 
Posts: n/a
Diggs:
Default Re: Character Movement + Rotation

Thank you very much for your reply,
So I am correct in saying that we multiply the speed to change the radius of
the unit circle(as you said it isn't always a unit).In other words, are we just
increasing the hypotenuse of the right triangle inside the circle?
Chinmaya


Reply With Quote


  #4 (permalink)  
Old 10-07-2008, 04:26 AM
Rothrock
 
Posts: n/a
Diggs:
Default Re: Character Movement + Rotation

Well I think both are just different ways of saying the same thing.

The unit circle, and its associated "unit triangle," will tell you the
components of your velocity (velocity has a direction, speed doesn't) for a
velocity of 1.

So you are increasing the hypotenuse of the right triangle, but it isn't
inside a unit circle anymore!

And there is the principle of similar triangles. If we take a triangle and
keep the angles the same the lengths of the sides will keep the same
proportions.

So if the velocity is seven and we take the same unit triangle (all unit
triangles have a hypotenuse of 1, right?) and then with the same angle we
increase the hypotenuse to our new velocity, say 7, the x and y components (the
other sides of the triangle) will both also be multiplied by 7.

Or alternately you are resolving a velocity vector into its orthonormal basis
vectors. Just to muddy the waters. This is a fairly common mathematical problem
that many people have resolved in a lot of different ways -- all related all
with a little something different.

Reply With Quote
  #5 (permalink)  
Old 10-07-2008, 04:49 AM
chinmayagoyal
 
Posts: n/a
Diggs:
Default Re: Character Movement + Rotation

I've been trying to understand this for quite a while now and.............
I've finally UNDERSTOOD IT!
Thank you,
Now to get to the collision detection part for my car racing track which is
another problem as flash only recognizes the boundary of my movie clip track.
So no hitTest will work.
Chinmaya

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
All times are GMT. The time now is 05:42 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
Inactive Reminders By Mished.co.uk