Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Circle pack-push in GameMaker by oliver @pixelchipcode
- // obj_packer: Create Event
- circles = [];
- path_points = [];
- is_drawing = false;
- max_radius = 20;
- min_radius = 2;
- spawn_interval = 10;
- last_spawn_x = 0;
- last_spawn_y = 0;
- // Physics constants - tweaked for smoother motion with arrays
- spring_strength = .4;
- damping = 0.8;
- min_push_distance = 0.1;
- // obj_packer: Step Event
- // Handle mouse input and drawing
- if (mouse_check_button_pressed(mb_left)) {
- is_drawing = true;
- circles = []; // Clear arrays
- path_points = [];
- array_push(path_points, [mouse_x, mouse_y]);
- last_spawn_x = mouse_x;
- last_spawn_y = mouse_y;
- }
- if (mouse_check_button(mb_left) && is_drawing) {
- // Add new mouse position if moved
- var path_len = array_length(path_points);
- var last_point = path_points[path_len - 1];
- if (point_distance(mouse_x, mouse_y, last_point[0], last_point[1]) > 5) {
- array_push(path_points, [mouse_x, mouse_y]);
- }
- // Spawn new circles along the line
- var dist = point_distance(last_spawn_x, last_spawn_y, mouse_x, mouse_y);
- if (dist >= spawn_interval) {
- var dir = point_direction(last_spawn_x, last_spawn_y, mouse_x, mouse_y);
- var steps = floor(dist / spawn_interval);
- for (var i = 0; i < steps; i++) {
- var spawn_x = last_spawn_x + lengthdir_x(spawn_interval * (i + 1), dir);
- var spawn_y = last_spawn_y + lengthdir_y(spawn_interval * (i + 1), dir);
- // [x, y, vx, vy, radius, target_radius, growing, hue]
- array_push(circles, [
- spawn_x, // x
- spawn_y, // y
- 0, // vx
- 0, // vy
- 1, // radius
- random_range(min_radius, max_radius), // target_radius
- 1, // growing (1 = true, 0 = false)
- (point_distance(spawn_x, spawn_y, room_width/2, room_height/2) /
- point_distance(0, 0, room_width/2, room_height/2)) * 255 // hue
- ]);
- }
- last_spawn_x = spawn_x;
- last_spawn_y = spawn_y;
- }
- }
- if (mouse_check_button_released(mb_left)) {
- is_drawing = false;
- }
- // Update circles with physics
- var circle_count = array_length(circles);
- for (var i = 0; i < circle_count; i++) {
- var circle = circles[i];
- // Grow circles
- if (circle[6] && circle[4] < circle[5]) { // if growing and radius < target_radius
- circle[4] += 0.5; // increase radius
- if (circle[4] >= circle[5]) {
- circle[4] = circle[5];
- circle[6] = 0; // stop growing
- }
- }
- // Calculate spring forces between circles
- var total_force_x = 0;
- var total_force_y = 0;
- for (var j = 0; j < circle_count; j++) {
- if (i == j) continue;
- var o = circles[j];
- var dist = point_distance(circle[0], circle[1], o[0], o[1]);
- var min_dist = circle[4] + o[4]; // sum of radii
- if (dist < min_dist && dist > 0) {
- var overlap = min_dist - dist;
- var angle = point_direction(o[0], o[1], circle[0], circle[1]);
- // Calculate spring force
- var force = overlap * spring_strength;
- total_force_x += lengthdir_x(force, angle);
- total_force_y += lengthdir_y(force, angle);
- }
- }
- // Apply forces and update velocity
- circle[2] += total_force_x; // vx
- circle[3] += total_force_y; // vy
- // Apply damping
- circle[2] *= damping;
- circle[3] *= damping;
- // Update position if movement is significant
- if (abs(circle[2]) > min_push_distance || abs(circle[3]) > min_push_distance) {
- circle[0] += circle[2]; // x += vx
- circle[1] += circle[3]; // y += vy
- }
- }
- // Draw circles with gradient effect
- var circle_count = array_length(circles);
- for (var i = 0; i < circle_count; i++) {
- var circle = circles[i];
- if(circle[4] < 5){
- draw_circle(circle[0], circle[1], circle[4],false)
- } else{
- draw_circle(circle[0], circle[1], circle[4],true)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment