#1 (permalink)  
Old 21-03-08, 08:30
Administrator
Living At The FlepStudio!
 
Join Date: Jul 2007
Location: Cesenatico
Posts: 4,917
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 !

__________________
_________________________________________
VIDEO CORSI ACTIONSCRIPT 3.0 creati da FlepStudio
I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread please !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
sponsor links
Flashmint flash templates FlippingBook-PDF publisher Flash Media Server Hosting
sponsor links
Reply

Bookmarks

Tags
angled, collision, coordinates, cs3, flash, rotation, surface

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 urgent! collision/gravity + rotation nasgerl Actionscript 3.0 newbies 0 18-11-08 07:31
Actionscript 3 Getting started with a 3d image rotation, help... wuzzi2ya advanced Actionscript 3.0 0 07-11-08 21:12
distanziamento rotation lorenzz Actionscript 3.0 avanzato 4 12-04-08 12:38
Multiple collision on angled surface Flep Tutorials 0 03-04-08 05:47
Collision between MCs niubbo HELP free utilities 0 25-02-08 12:08



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
vBulletin Skin developed by: vBStyles.com
FlepStudio 2007-2009