Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Rotation of coordinates and collision with an angled surface with Flash CS3

This is a discussion on Rotation of coordinates and collision with an angled surface with Flash CS3 within the Tutorials forums, part of the English Forums category; Hi all, This tutorial is a start in the field of advanced effect applied to Flash CS3 with Actionscript 3....


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 21-03-08, 09:30
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
Rep Power: 6
Flep is on a distinguished road
Rotation of coordinates and collision with an angled surface with Flash CS3

Hi all,


This tutorial is a start in the field of advanced effect applied to Flash CS3 with Actionscript 3.0.

We saw how to rotate the coordinates of a MovieClip using a trigonometric formula.


We will now see how to rotate a whole system of coordinates.


Surely, it will have happened to you to have to intercept a collision in between two MovieClip as for instance a small ball and a vertical or horizontal surface.

We have seen how to do it in the tutorial that explains how to bounce a ball.


Instead what would happen if the surface would not be anymore horizontal but angled?

I mean as if the surface was going downward.


How to simulate the physical situation as if in reality we took a ball and made it bounce against a 45° tilted surface?


This is when the thing gets a great deal harder to realise and none of the formula used before and even less the hitTestObject can resolve this problem.

So?how can we do it?


There is a unique answer: rotate the coordinate of the system, apply the bounce and bring back the coordinates of the system to the initial state.









I create a FLA and I save it as ?trigonometria7.fla?.

Into which, I design an horizontal line, I convert it into a MovieClip and I assign to it the instance name ?line_mc?.

I create, next, a second MovieClip of rounded shape to simulate the shape of a ball and I assign to it the instance name ?ball_mc?.

I now create a new level, I open the action panel and I write:


Code:
line_mc.rotation=20;

var vx:Number=0;
var vy:Number=0;
const GRAVITY:Number=.5;
const BOUNCE:Number=-.7;
var angle:Number=(line_mc.rotation/180)*Math.PI;

addEventListener(Event.ENTER_FRAME,go);

function go(evt:Event):void
{
	vy+=GRAVITY;
	ball_mc.x+=vx;
	ball_mc.y+=vy;
	
	var sine:Number=Math.sin(angle);
	var cosine:Number=Math.cos(angle);
	
	var xx:Number=ball_mc.x-line_mc.x;
	var yy:Number=ball_mc.y-line_mc.y;
	
	var x1:Number=cosine*xx+sine*yy;
	var y1:Number=cosine*yy-sine*xx;
	
	var vx1:Number=cosine*vx+sine*vy;
	var vy1:Number=cosine*vy-sine*vx;
	
	if(y1>-ball_mc.height/2)
	{
		y1=-ball_mc.height/2;
		
		vy1*=BOUNCE;
	}
	xx=cosine*x1-sine*y1;
	yy=cosine*y1+sine*x1;
	vx=cosine*vx1-sine*vy1;
	vy=cosine*vy1+sine*vx1;
	
	ball_mc.x=line_mc.x+xx;
	ball_mc.y=line_mc.y+yy;
}

Let us analyse the code


I assign a rotation of 20° to line_mc

I create two variables into which I insert the speed for x and y

var vx:Number=0;

var vy:Number=0;

a constant that contains the value of gravity

const GRAVITY:Number=.5;

a constant that contains the value of bounce height

const BOUNCE:Number=-.7;

a variable that contains the value in radiant of the rotation of 20° applied to line_mc

var angle:Number=(line_mc.rotation/180)*Math.PI;


I add an ENTER_FRAME that calls the function go

addEventListener(Event.ENTER_FRAME,go);


Into the function go:

I progressively increase the speed on the y axis of ball_mc adding to it the constant GRAVITY

vy+=GRAVITY;

I pass to the properties x and y of ball_mc the values of the speeds (vx and vy)

ball_mc.x+=vx;

ball_mc.y+=vy;

I create two variables that respectively contain the sinus and cosinus of the angle, value of the variable angle

var sine:Number=Math.sin(angle);

var cosine:Number=Math.cos(angle);

I create two variables that contain the distance in between the x and y of ball_mc and line_mc

var xx:Number=ball_mc.x-line_mc.x;

var yy:Number=ball_mc.y-line_mc.y;

I apply the formula of rotation as seen in tutorial 6 of <URL> trigonometry </URL> and doing so,

I rotate the coordinates of the system

var x1:Number=cosine*xx+sine*yy;

var y1:Number=cosine*yy-sine*xx;

I apply the same formula of rotation to the speed (in physics, it would be like changing the direction of a vector)

var vx1:Number=cosine*vx+sine*vy;

var vy1:Number=cosine*vy-sine*vx;

I apply the collision and the bounce effect

if(y1>-ball_mc.height/2)

{

y1=-ball_mc.height/2;


vy1*=BOUNCE;

}

I bring back the coordinates of the system to the initial state

xx=cosine*x1-sine*y1;

yy=cosine*y1+sine*x1;

vx=cosine*vx1-sine*vy1;

vy=cosine*vy1+sine*vx1;

I assign the values of xx and yy respectively to the x and y of ball_mc

ball_mc.x=line_mc.x+xx;

ball_mc.y=line_mc.y+yy;


See you soon !

__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Actionscript 3 Getting started with a 3d image rotation, help... wuzzi2ya advanced Actionscript 3.0 0 07-11-08 22:12
distanziamento rotation lorenzz Actionscript 3.0 avanzato 4 12-04-08 13:38
Multiple collision on angled surface Flep Tutorials 0 03-04-08 06:47
Collision between MCs niubbo HELP free utilities 0 25-02-08 13:08
Pythagorean theorem - collision detection Flep Tutorials 0 27-09-07 09:10


All times are GMT. The time now is 19:41.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406