Jump to content


Photo

How to not draw circles over other circles


  • This topic is locked This topic is locked
1 reply to this topic

#1 Andriko

Andriko

    GMC Member

  • New Member
  • 1 posts
  • Version:GM8.1

Posted 18 August 2012 - 01:04 AM

Hey, so I want to make a program that draws multiple circles on the screen in random locations with a random size, so here is my code minus the drawing part:

numofplanets = irandom_range(8, 20)
show_debug_message(string(numofplanets))
for (i=0; i<numofplanets; i+=1) {
    planetx[i] = 0
    planety[i] = 0
    planetr[i] = 0
}
for (i=0; i<numofplanets; i+=1) {
    do {
        collide = false
        planetx[i] = irandom_range(0, 640)
        planety[i] = irandom_range(0, 480)
        planetr[i] = random_range(0.125, 1)
        if planetx[i] + (planetr[i] * 60) >= 635 {
            planetx[i] = 635 - (planetr[i] * 60)
        }
        if planetx[i] - (planetr[i] * 60) <= 5 {
            planetx[i] = 5 + (planetr[i] * 60)
        }
        if planety[i] + (planetr[i] * 60) >= 475 {
            planety[i] = 475 - (planetr[i] * 60)
        }
        if planety[i] - (planetr[i] * 60) <= 5 {
            planety[i] = 5 + (planetr[i] * 60)
        }
        for (j=0; j<numofplanets; j+=1) {
            if i != j {
                if ((planetx[i] + (planetr[i] * 40)) >= (planetx[j] - (planetr[j] * 40))) && ((planetx[i] + (planetr[i] * 40)) <= (planetx[j] + (planetr[j] * 40))) {
                    if ((planety[i] + (planetr[i] * 40)) >= (planety[j] - (planetr[j] * 40))) && ((planety[i] + (planetr[i] * 40)) <= (planety[j] + (planetr[j] * 40))) {
                        collide = true
                    }
                }
            }
        }
    }
    until (collide = false)
}
in planetr[] the r is radius, the sprite which is drawn is 80x80, centered, and is drawn with draw_sprite_ext, scaled using planetr[]

For some reason the "planets" still overlap each other... can someone tell me why?

Edited by Andriko, 18 August 2012 - 01:07 AM.

  • 0

#2 ramses12

ramses12

    6

  • GMC Member
  • 5769 posts
  • Version:GM8.1

Posted 18 August 2012 - 07:59 AM

You know, using draw_sprite_stretched would make things much clearer, as you won't need to multiply the pseudo-radius with the sprite's radius each time you make a calculation.
Also, your second loop could be much shorter, as you only have to check collision with the planets generated until the present point. So, for(j=;j<i;j+=1) should be enough.
Your circle "collision checking" system is a bit over-complicated and actually inaccurate. You should do it using something more logical, like:
if(point_distance(planetx[i],planety[i],planetx[j],planety[j])<planetr[i]+planetr[j])collide=true;
By the way, to simplify your margin checking, you could
planetx=median(planetx,5+planetr,640-5-planetr);
planety=median(planety,5+planetr,480-5-planetr);
Of course, these codes imply that planetr is the actual radius. If you want to keep using the scale amount, you should use planetr*60 everywhere.

Edited by ramses12, 18 August 2012 - 08:00 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users