Papa Doge
Member
Hello,
I'm working on a game that has a throwing projectile that the player tosses and sticks into walls, then the player can call the projectile back and throw it again.
I had the basic code setup and working fine, but my projectile sprite was going too far into the walls, so I added some collision detection to check if the projectile is about to hit a wall based on its movement speed using a while loop, and if it is, I decided to move the projectile 1 pixel closer to the wall it's about to hit until it is hitting the wall, then stop its movement and fix the projectile's image_angle to match the wall it's hitting.
The problem I'm having is that my player sprite is 32x32 and my projectile speed is 16. Every time the player is on the floor or climbing a wall and tries to throw a projectile, the projectile sticks in the surface of whatever they are currently touching because the speed of the projectile is half the height of the player sprite and the collision detection kicks in automatically.
The solution I've been hunting for is some way to only check the collision detection after the projectile has moved at least 1 pixel which would solve my immediate problem, but I'm sure there are more elegant ways to resolve the issue I'm seeing.
I'm working on a game that has a throwing projectile that the player tosses and sticks into walls, then the player can call the projectile back and throw it again.
I had the basic code setup and working fine, but my projectile sprite was going too far into the walls, so I added some collision detection to check if the projectile is about to hit a wall based on its movement speed using a while loop, and if it is, I decided to move the projectile 1 pixel closer to the wall it's about to hit until it is hitting the wall, then stop its movement and fix the projectile's image_angle to match the wall it's hitting.
The problem I'm having is that my player sprite is 32x32 and my projectile speed is 16. Every time the player is on the floor or climbing a wall and tries to throw a projectile, the projectile sticks in the surface of whatever they are currently touching because the speed of the projectile is half the height of the player sprite and the collision detection kicks in automatically.
The solution I've been hunting for is some way to only check the collision detection after the projectile has moved at least 1 pixel which would solve my immediate problem, but I'm sure there are more elegant ways to resolve the issue I'm seeing.
Code:
// COLLISION VARIABLES
onWall = place_meeting(x+projectileSpeed*2,y,par_obj_collisions) - place_meeting(x-projectileSpeed*2,y,par_obj_collisions);
onFloor = place_meeting(x,y+projectileSpeed*2,par_obj_collisions) - place_meeting(x,y-projectileSpeed*2,par_obj_collisions);
onSpike = place_meeting(x,y,obj_death);
onDoor = place_meeting(x,y,obj_door);
/* -------------------- */
// COLLISION CHECKS
// if the projectile is not touching a wall or a floor...
if (onWall == 0) && (onFloor == 0) {
// set speed to default...
speed = projectileSpeed;
} else {
speed = 0;
if (returning == false) {
if (onWall == 1) {
// make it stick into the tile...
image_angle = 0;
while (!place_meeting(x,y,par_obj_collisions)) {
x += 1;
}
}
if (onWall == -1) {
// make it stick into the tile...
image_angle = 180;
while (!place_meeting(x,y,par_obj_collisions)) {
x -= 1;
}
}
if (onFloor == 1) {
// make it stick into the tile...
image_angle = 270;
while (!place_meeting(x,y,par_obj_collisions)) {
y += 1;
}
}
if (onFloor == -1) {
// make it stick into the tile...
image_angle = 90;
while (!place_meeting(x,y,par_obj_collisions)) {
y -= 1;
}
}
}
}
Last edited: