Skip to content

Commit

Permalink
reduced collision hitbox size
Browse files Browse the repository at this point in the history
  • Loading branch information
jellinki committed Oct 23, 2023
1 parent 821d0a8 commit cf3cfe9
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions _posts/2023-10-12-teamwork.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ courses: { compsci: {week: 7} }
let isMovingRight = false;
let isIdle = true;

const gravity = 2.5; // Increased gravity for even faster falling
const jumpStrength = -20; // Increased jump strength
const gravity = 2.5;
const jumpStrength = -20;
const moveSpeed = 20;

// Define a narrower collision hitbox
const collisionWidth = 20;
const collisionHeight = 20;

function updateSpriteAnimation() {
if (frameX < maxFrame) {
frameX++;
Expand Down Expand Up @@ -123,11 +127,9 @@ courses: { compsci: {week: 7} }
var platforms = [];

function generateRandomPlatform() {
// Calculate the maximum height for the platform, considering a minimum jump height
const minHeight = spriteY - jumpStrength;
const maxHeight = spriteY - 2 * jumpStrength;

// Define the platform properties
var platform = {
width: 150,
height: 20,
Expand Down Expand Up @@ -155,11 +157,17 @@ courses: { compsci: {week: 7} }
platforms.forEach(function (platform) {
const spriteBottom = spriteY + spriteHeight;
const platformTop = platform.y;
const spriteRight = spriteX + spriteWidth;
const spriteCenterX = spriteX + spriteWidth / 2;
const platformLeft = platform.x;
const platformRight = platform.x + platform.width;

if (spriteBottom >= platformTop && spriteY < platformTop && spriteX < platformRight && spriteRight > platformLeft) {
if (
spriteBottom >= platformTop &&
spriteY < platformTop &&
spriteCenterX > platformLeft &&
spriteCenterX < platformRight
) {
// Use a narrower hitbox for standing on platforms
spriteY = platformTop - spriteHeight;
isJumping = false;
spriteVelocityY = 0;
Expand Down Expand Up @@ -241,4 +249,3 @@ courses: { compsci: {week: 7} }
</script>
</body>
</html>

0 comments on commit cf3cfe9

Please sign in to comment.