-2

Help please write a function of the projectile bounce off the ground. How do I find the angle of the collision, and what should be the reflection angle or at least learn the point of collision?

package {
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BlendMode;
    import flash.geom.Point;
    import flash.geom.Matrix;
    import flash.events.Event;
    import flash.events.MouseEvent;
    public class worms extends Sprite {
        public var terrain_bmpd=new BitmapData(550,200,true,0xFF00FF00);//This is the Bitmap of the terrain
        public var terrain_bmp=new Bitmap(terrain_bmpd);//and this the BitmapData
        public var character=new Sprite();//The character will work as a worm
        public var hole=new Sprite();//That's the hole we need
        public var hole_matrix:Matrix;//The hole_matrix is used to set the position of the hole
        public var left_foot:Point;
        public var right_foot:Point;//These are the feet of the character. We will use it to check collisions
        public function worms() {
            draw_objects();//This function draws the character, the terrain and the hole.
            stage.addEventListener(Event.ENTER_FRAME,fall);
        }
        public function fall(e:Event) {
            /*This function will move down the character if there isn't a collision
            between the terrain and the "feet" of the character*/
            for (var i:int=0; i<10; i++) {//We want to check every pixel if there's acollision, so we won't move the character 10 pixels all at once
                left_foot=new Point(character.x-5,character.y+10);
                right_foot=new Point(character.x+5,character.y+10);
                if (!(terrain_bmpd.hitTest(new
                Point(terrain_bmp.x,terrain_bmp.y),0x01,left_foot))&&!(terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,right_foot))) {
                    character.y++;//If there aren't any collisions, make the character fall one pixel
                }
            }
        }
        public function draw_objects() {
            terrain_bmp.y=200;//The terrain shouldn't be at the top of the stage!
            stage.addChild(terrain_bmp);//We can make the terrain visible
            character.graphics.beginFill(0x0000FF);//Let's draw the character. It will be a blue rectangle.
            character.graphics.drawRect(-5,-10,10,20);
            character.x=250;
            stage.addChild(character);
            hole.graphics.beginFill(0x000000);//Now we draw the hole. It doesn't matter the colour.
            hole.graphics.drawCircle(0,0,30);
        }
    }
}
wewewe777
  • 11
  • 1

3 Answers3

1

There's several parts to this, and it seems you don't really know where to begin.

Like the others have mentioned, your object (projectile, character, etc) movement should be represented by a vector, ie x and y velocity. (A Point can represent a vector.)

Next, to detect angle of collision on pixel-based terrain like you have, you can do a series of point (pixel) hit tests in a radius around the object. This will give you a collection of hit points, which you define as vectors from the center of the object. Average the vectors and you have your collision angle.

Here's a function I've used to do this:

/**
 * Test for a hit against a shape based on a point and radius, 
 * returns the angle of the hit or NaN if no hit.
 * Like hitTestPoint, xPos and yPos must be in stage coordinates.
 */
function hitTestAngle(shape:Sprite, xPos:Number, yPos:Number, radius:Number, samples:uint = 90):Number {
    const PI2:Number = Math.PI * 2, SAMPLE:Number = 1 / samples;
    var dx:Number, dy:Number, a:Number, tx:Number = 0, ty:Number = 0, hits:int = 0;
    var i:int = samples;
    while(i--){
        a = PI2 * (i * SAMPLE);
        dx = radius * Math.cos(a);
        dy = radius * Math.sin(a);
        if(shape.hitTestPoint(xPos + dx, yPos + dy, true)){
            hits++;
            tx += dx;
            ty += dy;
        }
    }
    if(!hits)
        return NaN;
    
    return Math.atan2(ty, tx) * (180 / Math.PI);
}

This uses hitTestPoint() but since your terrain is BitmapData you could just use getPixel32() and check the alpha value.

You could also use Collision Detection Kit, which uses a similar approach under the hood, with a bunch of extra features.

Once you have the angle of collision, you can define a perpendicular vector as the collision surface to reflect the projectile's vector.

That should get you started.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Aaron Beall
  • 49,769
  • 26
  • 85
  • 103
0

Flat, horizontal ground:

//Check next movementstep
if((vel_y + pos_y) > terrainBottom) {
   vel_y = -vel_y*DampingFactor;
}

for the general case you need to transform vel_x, vel_y into the case shown above using trigonometric functions.

David O.
  • 50
  • 7
0

As in the previous answer, use vel_x and vel_y to represent the motion of your character in vector form. Use these values to increase your x and y co-ordinates at each iteration. In your example you are using vel_x = 0, vel_y = 1, because you are increasing the y-coordinate by 1 each time.

If a surface has an an angle A measured counter-clockwise from the horizontal then x_vel and y_vel after bouncing (relfecting) on the surface will be x_vel.cos2A + y_vel.sin2A and x_vel.sin2A - y_vel.cos2A respectively. To see how this is derived see planetmath

This is for a perfectly elastic collision, ie no loss of speed on impact.