My son and I are writing some graphics code, eventually for a game.
The code draws a parabolic path (100 line segments), based on the position of the mouse, which is truncated when it hits the ground (nine line segments). Before drawing each line segment of the parabola, we check whether it intersects each line segment of the ground, truncating the path at the ground and stopping if so. All of this happens in a warp.
It's quite slow. It's a lot speedier if the parabola is reduced to 15 (edit:25) line segments (set dt=.4), but doesn't look as good.
Admittedly, doing ~1000 intersection checks every refresh is a lot of work, but I'm not convinced Snap can't handle it.
I think we're following all of the advice in the FAQ, but we're far from experts. Is there any easily addressed coding issue that would help our performance? My son wants to speed things up and I'd rather avoid getting (more) into the weeds optimizing the code.
Well, first of all, the project runs plenty fast for me!
But instead of doing all that math at each step, you could use one of the TOUCHING blocks to detect hitting the ground. The most straightforward thing would be TOUCHING COLOR [black], but since that's a little slower than the test for touching another sprite, maybe after drawing the background, you should have another sprite do
and then the first sprite can TOUCHING (Sprite (2)) or whatever it's called.
Thanks for your reply. These are great ideas; we'll try them. I should have said that it's quite slow, according to my son. I grew up in another decade and it did not seem slow to me.
I agree. I think it's the number of segments of the parabola it draws. If I move the mouse below the "cannon" (where it would be if we drew one), the refresh is quite snappy. If I move it above that point, it slows to maybe 2 refreshes per second.
Drawing the segments isn't the issue. There's a loose chunk of code in the sprite that draws a zig-zag of 100 line segments, starting at the origin and ending at the mouse cursor. If attached to WHEN GREEN FLAG CLICKED, it runs smoothly.
Thanks all! Making the ground a sprite costume and using the TOUCHING block worked for us. We haven't experimented with pen trails yet; We avoided paths that tunnel through the top of the mountain by increasing the resolution of the parabola.
We're new at this, and did not think to make a static copy of the code to post for this thread. Apologies for any confusion to later readers.
@loucheman Your solution was along the lines of what we did, but I could only mark one post as the solution.