Tuesday, December 6, 2022
HomeGame Developmentcollision detection - How you can cease participant once they hit an...

collision detection – How you can cease participant once they hit an axis aligned bounding field?


I am within the course of of making a recreation utilizing electron and HTML5. It’ll be a reasonably primary story-based platformer, however I am programming the sport engine myself. Up to now it is going nicely, however I encountered some problem when making an attempt to implement strong objects, issues the participant cannot undergo.

Within the first revision of my engine, the algorithm is as follows:

//when the participant strikes left
var moveby = -player.velocity; //participant.x - participant.velocity => participant strikes left
//for every strong object
for (var i = 0; i < Sport.currentmap.solids.size; i++) {
  var strong = Sport.currentmap.solids[i];
  //if participant is not going to the touch the strong object when it strikes, proceed loop
  if (!Sport.check_collision(x - velocity, y - velocity, w + (velocity * 2), h + (velocity * 2), strong.x, strong.y, strong.w, strong.h)) {
    proceed;
  }
  //a hitbox on the aspect of the strong object
  var hitbox = {
    x: ogbox[0] + ogbox[2],
    y: ogbox[1],
    w: velocity,
    h: ogbox[3]
  }
  if (Sport.check_collision(x, y, w, h, hitbox.x, hitbox.y, hitbox.w, hitbox.h)) {
    //transfer participant left till they hit it, then break loop
    moveby = hitbox.x - x;
    break;
  }
}
return moveby;

Basically, this code is copied for every path the participant can transfer (up, down, left, and proper). The x, y, w, and h variables are the x, y, width, and top of the participant’s hitbox. The sport will test if the participant will collide with the strong object when it strikes. If it will not, the loop continues. If it would, the perform will calculate how far the participant strikes earlier than it would hit the strong object, then strikes the participant that far.

Clearly, this is not very environment friendly, and the code seems horrible. However I am unable to consider a greater method to go about this. May anyone please give me a greater advice?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments