Need help optimizing raycasting in project

Hello there,

I've recently started remaking my favorite scratch projects on Snap just to see what extra capabilities Snap has, and I've run into my project being significantly slower on Snap than it was on Scratch. I assume this is due to my inexperience with the warp block.

Link to my project because I got a popup saying I can't embed a picture in a post. If someone knows how I can post my code in the forum directly, that'd be appreciated.

You just need to read and post of the forum for a bit, then you'll get a notification saying you got trust level 1, and then you can post images. And if you don't get a notification, then you can just look at your name above posts. If it's black (or white) then you can post images, if it's grey, then you still can't.

very cool. Thanks for the info

I can't really help too much, because I'm currently on mobile, but one thing that could significantly speed up your project, is by using this block.

1000399145

It reports pretty much the same thing you would get, just by moving forward 1 step until you reach the wall, except that it's much faster, and way more accurate (in most cases). Now, it only checks the current direction, so you still have to change the direction of the sprite. You should probably experiment with that.

ray length to thing
Oh yeah, that optimized it quite a bit. Not 100% on the solution I came up with, but it works haha. I'll see what I can do to make it look nicer while still keeping it optimized.

Cheers

Whoops, I quoted too much. Just meant to quote the

ray length to thing

code issues can significantly slow down a project, but snap is also significantly slower than scratch because of the rendering. for example, every time a script glows in the editor, it takes a significant amount of time drawing the glow, and many things related to costumes changes the icon displayed in the sprite panel, which does very slow image operations.

in your case, your project is mainly slow because of all the pen being drawn. snap struggles to just fill the screen with pen, and you'll get much better performance if you can find a way to draw as few lines as possible, or maybe it's as few pixels as possible, i'm not really sure, but keep it way down.

i would suggest you draw the scene once, then afterwards only draw the new light or shadows, and don't draw anything that's the same. if you add any camera movement functionality later, maybe move the whole pen layer at once using the PEN TRAILS block. you can probably also significantly reduce the amount of rays you need to cast by only casting where the edges of objects should be. you can get the positions of sprites, and you can store the previous positions of sprites and places the rays hit, so that you only need to cast rays where things should change.

also, it probably won't make a difference to performance, but you can pretty much always directly use an (x, y) list for any position. for example, you can drop a list into the GO TO [random position] block, you can do math operations on entire lists, and you can replace an entire list at once instead of just replacing each item.

Oh, that's a fantastic idea. Thanks!

how would you go about doing this? I noticed the block, but I couldn't figure out how it works. Also is it possible to use pen trail block and change the pen trail, or is it only usable as the exact same as it was?

I was looking into this, but couldn't figure out how to do it. My end goal is to allow the shapes to be movable, too, so that is also probably another reason why I couldn't figure out how to make it. If I just hard coded coords, it would probably be significantly easier.

I don't know why they suggested using the pen trails costume for 3d, because with 3d, there's different camera angles, which you wouldn't be able to get with just drawing it once, and then applying some transformation on the image.

Unfortunately it is just a static costume of the current pen trails, you cannot modify the pen trails on the stage with that block (or at least without stamping).

the pen trails block would work for your current top down perspective with the shadows if you added scrolling, or really any situation where you want scrolling pen. it's just an example of something you could do, it might not be useful for whatever you end up creating. the pen trails block just gives everything currently drawn with a pen as a costume, so a sprite can switch to that costume, move over a bit, clear the pen, then stamp. this way you can do any sprite transformation to the pen somewhat fast.

if your sprite is a circle and you know the radius of that circle, the edge must be a radius distance from the center, and in fact you wouldn't even need to cast any rays to figure out where to draw the shadow. if it's a square, the edge must be at one of the outer corners of the square.
if you have some complex shape that's kinda close to a circle (some kind of blob or square or whatever), you can look at the costume, find the closest and farthest points from the center on the edge of the costume, and only cast rays that would go that distance from the center towards that sprite.

essentially, since you can very quickly find where sprites are without casting rays, and you're likely making the costumes beforehand, you can take advantage of that to find the large regions where you KNOW sprites will or won't be without casting any rays.

if your sprite is a circle...
if it's a square...

ah, those make a lot of sense :sweat_smile:. Thanks!

Good point. My current inexperience, it's probably best if I don't add the ability to drag around the objects anyways.