A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: sin, cos, collision and accurate bounce off

  1. #1
    Professional Bodger
    Join Date
    Aug 2003
    behind you!

    sin, cos, collision and accurate bounce off


    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:
    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.

    thankyou in advance for any help you can give me
    I reject your reality and Substitute my own!

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Los Angeles
    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

Click Here to Expand Forum to Full Width

HTML5 Development Center