A Flash Developer Resource Site

# Thread: sin, cos, collision and accurate bounce off

1. ## sin, cos, collision and accurate bounce off

hi

some who visit the games forum may know about my hovercraft game. i have one major problem, on which i have no clue what-so-ever on how to fix it. for reference to the game, here it is http://dhost.info/crazystuff/crazyst...raftbetav4.php

ok, basically i have this code to make the hovercraft go forwards in the direction it is pointing:
Code:
```acc_x = Math.cos((Math.PI*this._rotation)/180);
acc_y = Math.sin((Math.PI*this._rotation)/180);
vel_x += acc_x/6;
vel_y += acc_y/6;```
then i adjust the rotation accordingly to make it turn and also add some more slidyness to make it seem more like a hovercraft turning...

now all that works a treat... but i have one problem, i want to employ accurate collision, and make walls that the hovercraft can bounce off of. i can quite easyly say if (hitTest.wall(this._x, this._y, true) then make the hovercraft stop by making vel_x and vel_y == 0. but i dont want that. i need the hovercraft to bounce off the wall like a ball or something, and by wall i mean any angle not just right angle and straight( it could be a circular wall or other shapes), once i know how to do this, i can go on and complete my game, along with many other games i have stumbled upon this grey area with and stoped.

im looking for hints on the theory and/or snippits of code which would work in my circumstance.

2. When you hit a horizontal wall, reverse the horizontal velocity, and leech off a little of the energy.

For example:

// hit left wall?
if (this._x < 0) {
vel_x = -(vel_x * .8);
}

The .8 causes you to leech off some energy due to the collision. The minus sign handles the bounce.

Do the same with vertical walls and y.

- Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap