# \[AS2\] Vector based bouncing (reflection)

**URL:** https://forum.kirupa.com/t/as2-vector-based-bouncing-reflection/309849
**Category:** flash
**Created:** [May 26, 2010, 4:33am UTC](https://forum.kirupa.com/t/as2-vector-based-bouncing-reflection/309849 "2010-05-26T04:33:52Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Psykloak1](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@Psykloak1](https://forum.kirupa.com/u/Psykloak1)
#### Post date: [May 26, 2010, 4:33am UTC](https://forum.kirupa.com/t/as2-vector-based-bouncing-reflection/309849/1 "2010-05-26T04:33:52Z")

</div>

Hi guys. I am working on a simple game where a ball bounces off the floors and ceilings. It’s movement direction is based on vectors, so it has an angle (in radians) and figures out the X and Y from that. Everything works, except I can’t get it to bounce off the left and right walls correctly. Top and bottom bouncing works fine, but not left and right.

It’s literally one line of code that I can’t get (making an object reflect it’s angle after hitting a wall). I know this has been asked before, but I’ve literally been searching for hours for something that explains this part of it and can’t find anything.

Any help would be great!

```auto
onClipEvent (load) {
	speed=15; 
	dir=0.8; //angle in radians
}
onClipEvent (enterFrame) {
//xm and ym are the amount to increment _x and _y
	if (_x+xm>550) { //Right wall
		dir*=-1; //this is wrong...
	}
	if (_x+xm<0) { //left wall
		dir*=-1; //this is wrong...
	}
	if (_y+ym>400) { //floor
		dir*=-1;
	}
	if (_y+ym<0) { //roof
		dir*=-1;
	}
	
//trigonometry to figure x and y movement based on angle (dir)
	xm = speed * Math.cos(dir)
	ym = xm * Math.tan(dir);
	
	_x+=xm;
	_y+=ym;
}
on (keyPress "<Up>") {
	dir+=.3;
}
on (keyPress "<Down>") {
	dir-=.3;
}

```
