AS3 Collision response for a game

1.5k views Asked by At

I hav bee struggling with a collision response method, im writing in actionscript 3, for a long time now, and I am hoping someone can explain what this code means, for I have only modified it. I did not write it from scratch. also I am using the Collision Detection Toolkit to find where the collisions are happening.
What im trying to do is make it so that Ball (which is the object that you move around the screen with your keyboard) hit's a wall or object it will not go through the wall or object.

my understanding of this is that you are finding the angle that the collision is happening at and then you need to find the angle which is adjacent which is the new vector and push back the object by however much it's overlapping, but I dont understand how this function works for instance what is

vx0:Number = ball.vx * cos + ball.vy * sin;

finding?. in addition I dont want the ball to accelerate I want it's velocity to be constant until it hits an object. also would it be faster to search through this array backwards?

-- I appreciate any one who takes the time to try and help me with this, and im sorry if I have some misspellings.

     for(i = 0; i < collisions.length; i++) 
        {
            var collision:Object = collisions[i];

            var angle:Number = collision.angle;
            var overlap:int = collision.overlapping.length;
            var ball:Ball = collision.object1;

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

            var vx0:Number = ball.vx * cos + ball.vy * sin;
            var vy0:Number = ball.vy * cos - ball.vx * sin;

            vx0 = .4;
            ball.vx = vx0 * cos - vy0 * sin;
            ball.vy = vy0 * cos + vx0 * sin;

            ball.vx -= cos * overlap / ball.radius;
            ball.vy -= sin * overlap / ball.radius;
        }
1

There are 1 answers

3
AudioBubble On BEST ANSWER

Keith Peters has a wonderful book called Foundation Actionscript 3.0 Animation: Making Things Move.

Chapter 9 covers the topic you're having trouble with.

This page in particular should be of assistance.

http://books.google.ca/books?id=nzyu5TytMEUC&lpg=PP1&dq=actionscript%203.0%20animation&pg=PA235#v=onepage&q&f=false

He uses home-baked, trigonometric collision testing, but you should be able to replace his distance checking with the functions and properties built into Collision Detection Kit.