Giter VIP home page Giter VIP logo

ikd's People

Contributors

danboid avatar weber21w avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

weber21w

ikd's Issues

'Proper' bouncy bullets

I have been wondering exactly how I would implement 'proper' bouncy bullets (not the ping-pong cop-out currently in place) for some time. I thought I'd ask ChatGPT today and it sounds like it has devised a workable solution. I never thought when I started this project that at least one of its features would get devised by an AI! Amazing times.

My question to ChatGPT was:

How might I improve processBullets() so that the bullets will deflect off multiple surfaces and not just ping-pong between 2 walls?

It's reply:

To make the bullets deflect off multiple surfaces, you will need to update the processBullets() function to check for collisions with all walls in the level, not just the two adjacent walls.

One way to do this would be to use a 2D array to represent the level layout, with each element in the array representing a tile on the screen. You can then use the bullet's position and velocity to determine which tiles it will pass through during each frame. For each tile that the bullet will pass through, you can check if that tile is a wall or not, and if it is, you can calculate the new velocity for the bullet based on the angle of the wall and the direction the bullet was coming from.

Here is some pseudocode that demonstrates this approach:

void processBullets() {
  for each bullet {
    int startX = bullet.x / 8; // convert from pixels to tile units
    int startY = bullet.y / 8;
    int endX = (bullet.x + bullet.vX) / 8;
    int endY = (bullet.y + bullet.vY) / 8;

    // Check if the bullet is moving horizontally or vertically
    if (startX == endX) {
      // Bullet is moving vertically
      int direction = (bullet.vY > 0) ? 1 : -1;
      for (int y = startY; y != endY; y += direction) {
        if (level[y][startX] == WALL) {
          // Collision with vertical wall, reverse Y velocity
          bullet.vY = -bullet.vY;
          break;
        }
      }
    } else {
      // Bullet is moving horizontally
      int direction = (bullet.vX > 0) ? 1 : -1;
      for (int x = startX; x != endX; x += direction) {
        if (level[startY][x] == WALL) {
          // Collision with horizontal wall, reverse X velocity
          bullet.vX = -bullet.vX;
          break;
        }
      }
    }

    // Update bullet position based on new velocity
    bullet.x += bullet.vX;
    bullet.y += bullet.vY;
  }
}

This approach will allow the bullets to deflect off multiple walls and surfaces in the level.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.