Dieser Code erstellt ein Polygon basierend auf dem Umriss eines Image. Er stammt aus GMPhysics by Chris Giles.
Hat jemand von Euch Lust und Zeit (ich nämlich gerade nicht), in Java umzusetzen?
Spoiler anzeigen
Alles anzeigen
Hat jemand von Euch Lust und Zeit (ich nämlich gerade nicht), in Java umzusetzen?
GML-Quellcode
- //creates an instance of a polygon with verticies automatically added
- //arg0 - sprite name
- //arg1 - detail (lower = better)
- //arg2 - angle threshold in degrees (will remove points with angle differences below this level; 15 is a good value)
- // making this larger will make the body faster but less accurate;
- //arg3 - filename (optional); if specefied, a file with the vertex infromation will be created
- //returns new polygon ID
- d = argument1;
- spr = argument0;
- w = sprite_get_width(spr);
- h = sprite_get_height(spr);
- xoff = sprite_get_xoffset(spr);
- yoff = sprite_get_yoffset(spr);
- sprite_set_offset(spr,0,0);
- s = surface_create(w,h);
- surface_set_target(s);
- draw_set_blend_mode(bm_normal);
- draw_set_color(make_color_rgb(128,0,128));
- draw_set_alpha(1);
- draw_rectangle(0,0,w,h,false);
- draw_set_color(c_white);
- draw_sprite(spr,0,0,0);
- poly = create_polygon();
- trans = make_color_rgb(128,0,128);
- numPoints = 0
- vertex_x[0] = 0;
- vertex_y[0] = 0;
- vertex_k[0] = 1;
- tx = 0;
- ty = 0;
- fy = -1;
- for (tx=0;tx<w;tx+=d)
- {
- for (ty=0;ty<h;ty+=1)
- {
- if (surface_getpixel(s,tx,ty) != trans)
- {
- vertex_x[numPoints] = tx-xoff;
- vertex_y[numPoints] = h-ty-yoff;
- vertex_k[numPoints] = 1;
- numPoints += 1;
- if (fy < 0)
- fy = ty;
- lx = tx;
- ly = ty;
- break;
- }
- }
- }
- for (ty=0;ty<h;ty+=d)
- {
- for (tx=w-1;tx>=0;tx-=1)
- {
- if (surface_getpixel(s,tx,ty) != trans && ty > ly)
- {
- vertex_x[numPoints] = tx-xoff;
- vertex_y[numPoints] = h-ty-yoff;
- vertex_k[numPoints] = 1;
- numPoints += 1;
- lx = tx;
- ly = ty;
- break;
- }
- }
- }
- for (tx=w-1;tx>=0;tx-=d)
- {
- for (ty=h-1;ty>=0;ty-=1)
- {
- if (surface_getpixel(s,tx,ty) != trans && tx < lx)
- {
- vertex_x[numPoints] = tx-xoff;
- vertex_y[numPoints] = h-ty-yoff;
- vertex_k[numPoints] = 1;
- numPoints += 1;
- lx = tx;
- ly = ty;
- break;
- }
- }
- }
- for (ty=h-1;ty>=0;ty-=d)
- {
- for (tx=0;tx<w;tx+=1)
- {
- if (surface_getpixel(s,tx,ty) != trans && ty < ly && ty > fy)
- {
- vertex_x[numPoints] = tx-xoff;
- vertex_y[numPoints] = h-ty-yoff;
- vertex_k[numPoints] = 1;
- numPoints += 1;
- lx = tx;
- ly = ty;
- break;
- }
- }
- }
- for (i=0;i<numPoints-2;i+=1)
- {
- ang1 = point_direction(vertex_x[i],vertex_y[i], vertex_x[i+1],vertex_y[i+1]);
- ang2 = point_direction(vertex_x[i+1],vertex_y[i+1], vertex_x[i+2],vertex_y[i+2]);
- if (abs(ang1-ang2) <= argument2)
- vertex_k[i+1] = 0;
- }
- ang1 = point_direction(vertex_x[numPoints-2],vertex_y[numPoints-2], vertex_x[numPoints-1],vertex_y[numPoints-1]);
- ang2 = point_direction(vertex_x[numPoints-1],vertex_y[numPoints-1], vertex_x[0],vertex_y[0]);
- if (abs(ang1-ang2) <= argument2)
- vertex_k[numPoints-1] = 0;
- ang1 = point_direction(vertex_x[numPoints-1],vertex_y[numPoints-1], vertex_x[0],vertex_y[0]);
- ang2 = point_direction(vertex_x[0],vertex_y[0], vertex_x[1],vertex_y[1]);
- if (abs(ang1-ang2) <= argument2)
- vertex_k[0] = 0;
- fl = -1;
- if (is_string(argument3))
- {
- fl = file_text_open_write(argument3);
- file_text_write_string(fl, "poly = create_polygon();");
- file_text_writeln(fl);
- }
- for (i=0;i<numPoints;i+=1)
- if (vertex_k[i] == 1)
- {
- if (fl >= 0)
- {
- file_text_write_string(fl, "add_vertex( poly, " + string(vertex_x[i]) + ", " + string(vertex_y[i]) + " );");
- file_text_writeln(fl);
- }
- add_vertex(poly, vertex_x[i], vertex_y[i]);
- }
- if (fl >= 0)
- file_text_close(fl);
- sprite_set_offset(spr,xoff,yoff);
- surface_reset_target();
- surface_free(s);
- return poly;