#๐Ÿ”’ How can I make a circle grow at a certain rate?

56 messages ยท Page 1 of 1 (latest)

rich stag
#

I'm working on my Modern Physics Final and am doing a python code for my project. I'm doing good for the most part, but I'm trying to create a light clock, which ticks at a specific rate depending on the velocity of the planet or how close it is to a black hole. If you're interested in seeing the whole code, I have it on github:
https://github.com/MaraMaraschino/M-Phys-I/blob/master/Pygame M Phys Final.ipynb

With the light clock, right now I'm just trying to get it to grow AT ALL, but with the code that I have it just draws a bunch of concentric rings at a distance that light would travel each time step. I have:

def light_clock(self, planets, is_1d):
        if not self.bh and not self.sun:
            if is_1d:
                center = scale * self.x + width/2, height/2
            
            else:
                center = (scale * self.x + (width/2), scale * self.y + (height/2))
            
            for _ in range(500):
                radius = scale * c * dt * (_ + 1)
                pygame.draw.circle(win, white, center, radius, 1)

The "is_1d" is there because I'm also trying to draw the system on it's edge as well as from above, so I need it to be able to work for both instances. The circles also stay tethered to the planet's center, but I want it to start drawing at the center and then stay at that location as it expands. I see why it's doing what it's doing, but I have no idea how to get it to do anything else.

Thank you for any help!

GitHub

Place to store my coding projects for school. Contribute to MaraMaraschino/M-Phys-I development by creating an account on GitHub.

hollow kiteBOT
#

@rich stag

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

nocturne crag
#

You want the ring to grow. But instead, it shows rings with multiple radii at the same time.

#

The solution is to draw a different radius in each frame?

rich stag
nocturne crag
#

I think somehow you need to make the radius depend on the time. Of course one idea is to have a radius attribute and update it in the update function. But you could also calculate a radius in the drawing function if you have access to the current time.

#

I'm not sure what makes sense and how you want it to look exactly.

#

Especially when the velocity or the distances change.

rich stag
#

I think I have it getting the size to increase now at least, but I'm not sure how to get it to store the location of the center of the circle each time one is drawn and to draw multiple every few time steps.

nocturne crag
#

Okay, so you don't want the planet to be the center. Maybe in that case each clock needs to have its own center.

#

Maybe it makes sense for the clocks to be separate list of objects.

#

Or each planet could have a specific number of clocks.

rich stag
#

Yeah, I'm trying
(idk if this'll work as a comment):

def light_clock(self, planets, is_1d = False):
        if not self.bh and not self.sun:
            if is_1d:
                center = scale * self.x + width/2, height/2
            
            else:
                center = (scale * self.x + (width/2), scale * self.y + (height/2))
            
            self.light_clock_circles = []
            if pygame.time.get_ticks() % 10 == 0:
                # Draw a circle with radius 0
                pygame.draw.circle(win, white, center, 0, 1)  
                # Store the center and the current time when the circle was drawn
                self.light_clock_circles.append((center, pygame.time.get_ticks()))
                
            radius = 0
            for circle_data in self.light_clock_circles[:]:
                circle_center, draw_time = circle_data
                # Calculate the elapsed time since the circle was drawn
                elapsed_time = pygame.time.get_ticks() - draw_time
                # Calculate the radius based on the growth rate
                radius += scale * c * dt * (elapsed_time)
                # Draw the updated circle
                pygame.draw.circle(win, white, circle_center, max(1, int(radius)), 1)
                # Remove the circle from the list if it's too large
                if radius > 500:
                    self.light_clock_circles.remove(circle_data)

I think ??? that it's storing the circles like this, but now it's not drawing them

nocturne crag
#

Yes, ai isn't good at writing this kind of code. Now you're assigning an empty list to self.light_clock_circles on each frame. And you only add one circle to it when the current milliseconds are a multiple of 10.

#

So you only draw circles with radius 1, if at all.

rich stag
#

Yeah I have gotten a little unlucky with a professor that encourages chatgpt orz
How could I fix that?

#

I haven't really been able to find analagous sims to look at the code of.

nocturne crag
#

I thought you wanted the rate to be dependent on the velocity of the planet and how close it is to a black hole.

rich stag
#

Yeah but that part seems like the easier part - if I can get it to draw every dt, I can get it to draw every dt * velocity_gamma * gravity_gamma

#

I mainly need it to keep circles in place every few time steps and to expand them at c

nocturne crag
#

Okay, at least you need to stop assigning an empty list to self.light_clock_circles in this drawing function.

#

That needs to go into the planet initialization.

rich stag
#

That one change like immediately fixed it lol tysm, I really appreciate it

#

Now the only problem is that each circle seems to be evolving at a different rate without the relative affects being added in

nocturne crag
#

Maybe something is wrong with how the radius is calculated.

rich stag
#

I think the problem is coming from *(elapsed_time/1000) - They're evolving faster the later they're drawn

nocturne crag
#

So how do you calculate the radius of a circle?

rich stag
#

radius += scale * c * dt * (elapsed_time/1000)

#
or circle_data in self.light_clock_circles[:]:  # Iterate over a copy of the list to allow removal
                circle_center, draw_time = circle_data
                # Calculate the elapsed time since the circle was drawn
                elapsed_time = pygame.time.get_ticks() - draw_time
#

for*

nocturne crag
#

Sounds weird.

rich stag
#

๐Ÿ˜…

nocturne crag
#

Why would you add them?

rich stag
#

Doesn't it mean that the radius gets bigger by those factors each time step?

nocturne crag
#

Yes, for one circle it makes sense. But you're adding everything so that the second circle grows twice as fast and the third circle grows three times as fast.

rich stag
#

Ok, that's evolving a lot more evenly - do you know where the elapsed_time/1000 comes from? If it's just elapsed_time, it grows way too fast no matter the size of the black hole, but if it's /1000, the planet moves faster than the circle's radius grows which shouldn't be possible

nocturne crag
#

elapsed_time is the elapsed time in milliseconds. what is dt?

rich stag
#

dt = bh_period/360
If I kept it as a static number, it would be way too slow for small black holes and way too short for bigger ones, so it's just to have it be dynamic based on how long it would take a black hole to orbit at a specific distance.

#

would take a planet to orbit the black hole*

nocturne crag
#

So the movement of the planets is different from the growth of the circles.

#

The planet velocity gets added once per frame to the planet position. The circle radius gets updated based on the elapsed ticks.

#

So what you want to do is to remove pygame.time.get_ticks() / draw_time from your code.

#

Instead you want to store center and radius for each circle, and update the radius once per frame.

#

Or just store a list of centers, and calculate the radius based on the list index.

rich stag
#

I'm not sure what to replace the .get_ticks() with. Also I thought that's what I was doing with

for circle_data in self.light_clock_circles[:]
   circle_center, draw_time = circle_data
#

Also, sorry for my really basic problems ๐Ÿ˜ญ I'm still pretty beginner at this - this is so far the longest code I've worked on at all and it's still like 75% from a youtube video and chatGPT

nocturne crag
#

I mean you can insert the centers at the front of the list like self.light_clock_circles.insert(0, center) so that the list index equals the number of frames that elapsed.
Then you can calculate the radius as py for i, center in enumerate(self.light_clock_circles): radius = scale * c * dt * i

rich stag
#

Should that be completely replacing

for circle_data in self.light_circles[:]

block? Or should I just add that before the radius part of that?

#

It runs when completely replacing the block, but it's not drawing the light clock so ig scratch the first half of the question out lol

#

I'm just not sure where to put that code or what to replace with that code

nocturne crag
#

yes, that only calculates the radius. you would still have to draw the circle.

rich stag
#

I think I'm going to have to head out for the night. I really appreciate all the help you've given me so far! I might try to come back to this post tomorrow after I've worked more on it if you'd still be willing to help out.

nocturne crag
#

Good night.

rich stag
#

Good night!

hollow kiteBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.