Advertisement

bug invaders

Started by April 13, 2019 12:26 AM
59 comments, last by Tom Sloper 5 years, 10 months ago

I will work on my collision code.

What I think is good here, to keep using the words to the best of our ability. When we hear the right thing over and over, maybe we start to think these on our own. One day. 

Dev careful. Pixel on board.
Buckle up. Everything will be revealed.

Advertisement
8 hours ago, kseh said:

Change the if statement that you call your collision test so it compares the bullet location (your x and y variables) with the bug location (bug.px and bug.py). Make sure to account for the width and height of the bug.

Maybe my flowchart will be useful here.

https://drakonhub.com/ide/doc/8observer8/14

20190412234905.png.b1183362b60265df584e74c6ad71fd3d.png

well this flowchart is similar to AABB collision detection

well I am going to draw a hex grid for a tank game I am going to develop, this is a board game I like called panzer blitz.

Nothing wrong with switching projects for awhile if you like.

Whatever game project that you work on, you will probably find a need to work with some kind of collision detection. And you'll want that collision detection to work for every instance of a sprite, tile, bug, actor, or whatever character or thing you add to your game. What you have with your bug invaders code, (as of your code post last Monday), is in an ok position to work for every instance of a bug. I stand by my last suggestion for how to proceed if you want to take another shot at it. There would be a lot more to solve after that, but having just one bug disappear when a bullet hits it would be an accomplishment.

Collision detection and processing is something that I have to rework in some way just about every project that I work on. That is not including differences in what I want the game to do on a collision, it is just detecting whether one occurred at all and then having the game do what I want to do. What I have is currently a more advanced system than what you have but basic AABB collision detection is where it starts.

Advertisement

thanks kseh I have used AABB collision detection in my breakout and pong game. it has been a while so I will have to study up on collision algorithms.

So, did you want to close this thread as well? 

Dev careful. Pixel on board.
Buckle up. Everything will be revealed.

yes

I solved my task that I showed in my flowchart above. You can run my solution in sandbox. I wrote it in TypeScript + Unit Tests in Jasmine. Try to rewrite it in C#.

Collisions.spec.ts

Spoiler


import { Collisions } from "./Collisions";
import { Point } from "./Point";
import { Rectangle } from "./Rectangle";

describe("Collisions", () =>
{
    it("IsPointInsideRectangle_PointIsInsideRectangle_ReturnsTrue", () =>
    {
        // Arrange
        let collisions = new Collisions();
        let point = new Point(10, 30);
        let rectangle = new Rectangle(-70, -50, 140, 100);

        // Act
        let actual = collisions.IsPointInsideRectangle(point, rectangle);

        // Assert
        expect(actual).toEqual(true);
    });

    it("IsPointInsideRectangle_PointIsOutsideOnTheLeft_ReturnsFalse", () =>
    {
        // Arrange
        let collisions = new Collisions();
        let point = new Point(-90, 10);
        let rectangle = new Rectangle(-70, -50, 140, 100);

        // Act
        let actual = collisions.IsPointInsideRectangle(point, rectangle);

        // Assert
        expect(actual).toEqual(false);
    });

    it("IsPointInsideRectangle_PointIsOutsideOnTheTop_ReturnsFalse", () =>
    {
        // Arrange
        let collisions = new Collisions();
        let point = new Point(-50, 55);
        let rectangle = new Rectangle(-70, -50, 140, 100);

        // Act
        let actual = collisions.IsPointInsideRectangle(point, rectangle);

        // Assert
        expect(actual).toEqual(false);
    });

    it("IsPointInsideRectangle_PointIsOutsideOnTheRight_ReturnsFalse", () =>
    {
        // Arrange
        let collisions = new Collisions();
        let point = new Point(90, -10);
        let rectangle = new Rectangle(-70, -50, 140, 100);

        // Act
        let actual = collisions.IsPointInsideRectangle(point, rectangle);

        // Assert
        expect(actual).toEqual(false);
    });

    it("IsPointInsideRectangle_PointIsOutsideOnTheBottom_ReturnsFalse", () =>
    {
        // Arrange
        let collisions = new Collisions();
        let point = new Point(20, -51);
        let rectangle = new Rectangle(-70, -50, 140, 100);

        // Act
        let actual = collisions.IsPointInsideRectangle(point, rectangle);

        // Assert
        expect(actual).toEqual(false);
    });
});

 

Collisions.ts

Spoiler


import { Point } from "./Point";
import { Rectangle } from "./Rectangle";

export class Collisions
{
    public IsPointInsideRectangle(point: Point, r: Rectangle): boolean
    {
        return (r.X < point.X && point.X < r.X + r.Width &&
            r.Y < point.Y && point.Y < r.Y + r.Height);
    }
}

Point.ts

Spoiler


import { Shape } from "./Shape";

export class Point extends Shape
{
    public constructor(x: number, y: number)
    {
        super(x, y);
    }
}

Rectangle.ts

Spoiler


import { Shape } from "./Shape";

export class Rectangle extends Shape
{
    private _width: number;
    public get Width(): number
    {
        return this._width;
    }
    public set Width(v: number)
    {
        this._width = v;
    }

    private _height : number;
    public get Height() : number {
        return this._height;
    }
    public set Height(v : number) {
        this._height = v;
    }
    
    public constructor(x: number, y: number, width: number, height: number)
    {
        super(x, y);
        this.Width = width;
        this.Height = height;
    }
}

Shape.ts

Spoiler



export abstract class Shape
{
    private _x: number;
    public get X(): number
    {
        return this._x;
    }
    public set X(v: number)
    {
        this._x = v;
    }

    private _y: number;
    public get Y(): number
    {
        return this._y;
    }
    public set Y(v: number)
    {
        this._y = v;
    }

    public constructor(x: number, y: number)
    {
        this.X = x;
        this.Y = y;
    }
}

 

This topic is closed to new replies.

Advertisement