GameMaker. Write that if the object collides with an obstacle, it moves to another point…
- User
GameMaker. Write that if the object collides with an obstacle, it moves to another point nearby that does not collide with the obstacle.
- Assistant
GameMaker. Write that if the object collides with an obstacle, it moves to another point nearby that does not collide with the obstacle.
Certainly! In GameMaker, you can handle collisions by using collision events or by checking collisions within the Step event using GML (GameMaker Language). Below is an example of how you can achieve the desired behavior:
Objective:
When an object (obj_Player, for example) collides with an obstacle (obj_Obstacle), it should move to a nearby point that does not collide with the obstacle.
Create Your Objects:
obj_Player): The object that will move and potentially collide.obj_Obstacle): The stationary object that obj_Player should avoid colliding with.Setup Collision Handling in obj_Player:
We'll use the Step event to continuously check for collisions and respond accordingly.
1. Add a Step Event to obj_Player:
// obj_Player - Step Event
// Define movement variables (example using arrow keys)
var move_x = 0;
var move_y = 0;
if (keyboard_check(vk_left)) move_x = -4;
if (keyboard_check(vk_right)) move_x = 4;
if (keyboard_check(vk_up)) move_y = -4;
if (keyboard_check(vk_down)) move_y = 4;
// Attempt to move the player
if (move_x != 0 || move_y != 0) {
// Check potential new position for collision
var new_x = x + move_x;
var new_y = y + move_y;
if (!place_meeting(new_x, new_y, obj_Obstacle)) {
// No collision, move normally
x = new_x;
y = new_y;
} else {
// Collision detected, find nearby non-colliding position
// Define search radius (in pixels)
var search_radius = 32;
// Number of attempts to find a non-colliding position
var attempts = 8;
var angle_step = 360 / attempts;
var found = false;
for (var i = 0; i < attempts && !found; i++) {
var angle = i * angle_step;
var radians = degtorad(angle);
var offset_x = cos(radians) * search_radius;
var offset_y = sin(radians) * search_radius;
var test_x = x + offset_x;
var test_y = y + offset_y;
// Check if the new position is free of obstacles
if (!place_meeting(test_x, test_y, obj_Obstacle)) {
// Move to this new position
x = test_x;
y = test_y;
found = true;
}
}
if (!found) {
// Optional: Handle case where no position was found within search radius
// For example, stop movement or implement alternative logic
show_debug_message("No available position found to move.");
}
}
}
2. Explanation of the Code:
Movement Input:
move_x and move_y accordingly to determine movement direction.Collision Check Before Movement:
new_x, new_y) would result in a collision with obj_Obstacle using place_meeting().Handling Collision:
search_radius within which to look for alternative positions.3. Customization:
Search Radius (search_radius):
Number of Attempts (attempts):
Movement Speed:
4 pixels per step. Adjust move_x and move_y to change the speed as needed.4. Additional Considerations:
Smooth Movement:
Handling Edge Cases:
Optimizations:
Alternatively, you can use the Collision event in obj_Player with obj_Obstacle to handle collisions specifically when they occur. Here's an example:
1. Add a Collision Event to obj_Player with obj_Obstacle:
// obj_Player - Collision Event with obj_Obstacle
// Define search parameters
var search_radius = 32;
var attempts = 8;
var angle_step = 360 / attempts;
var found = false;
// Iterate through multiple directions to find a free spot
for (var i = 0; i < attempts && !found; i++) {
var angle = i * angle_step;
var radians = degtorad(angle);
var offset_x = cos(radians) * search_radius;
var offset_y = sin(radians) * search_radius;
var test_x = x + offset_x;
var test_y = y + offset_y;
// Check if the new position is free of obstacles
if (!place_meeting(test_x, test_y, obj_Obstacle)) {
// Move to this new position
x = test_x;
y = test_y;
found = true;
}
}
if (!found) {
// Optional: Handle case where no position was found within search radius
show_debug_message("No available position found to move after collision.");
}
2. Explanation:
obj_Player collides with obj_Obstacle.The provided code examples demonstrate how to handle collisions in GameMaker by detecting when an object collides with an obstacle and then moving it to a nearby position that does not result in another collision. You can customize the search parameters and movement logic to fit the specific needs of your game.
Feel free to ask if you need further customization or have additional questions!
Not yet included in a published release.