Slice a Block like a lightsaber

After I saw a game titled “Saber Simulator” on the top of the popular section, I thought it would have cool light saber mechanics, so I was excited. Then I played it of course, and it didn’t. What I want is to be able to slice a block, in many ways, on the client, smoothly, and without noticeable lag. I also only want it to work with parts as blocks because i feel it would be easier. I also realize I’m not the first to think of this.

I’ve tried using SubtractAsync, but it quickly becomes a terrible choice for many reasons. Mainly, to reduce lag, I only need to run a slice every time the saber travels a certain distance, the sabers line path changes a certain amount with enough speed, or enough time passes. And of course only when its touched a block.
So, I ask if this is possible on Roblox, or if it should be done?
What are your thoughts?

24 Likes

I think this is a GREAT idea :upside_down_face:

With a little bit of scripting and some building, I’m pretty sure this can come true. (Although I’m unsure Roblox Studios supports this YET)

If I were you, I would try scripting a block that, when touched by the saber, would be replaced by two blocks half the size of the original block, which would simulate the block being cut in half.

I’m not very good at scripting and building though, so whatever I just said is just an idea on how to do it. :smile:

6 Likes

I’d recommend doing the exact opposite. Imagine having many breakable blocks, and one script per block… that would cause lag, and a bunch of unwanted / avoidable memory issues. Instead, every frame perform a raycast either from the tip of the lightsaber to the bottom of the saber, or from the bottom of the lightsaber to the top. Then if the raycast hits the brick, you could of course cut the brick in half. The only hard part would be getting the brick to actually split in two. Obviously you could duplicate the brick, and make both bricks half their original size, but that sadly that would get unrealistic real fast… Imagine a player cutting a triangle out of 1 brick. It would be really hard to make the brick cut in half properly, and get which section of the brick you cut off.

6 Likes

True true. I’m not the best scripted, so I’m not that good with this kind of stuff. :sweat_smile:

Thanks for giving helping me out.

4 Likes

You can store an already “cut block template”. Call it when necessary such as a saber/part touching the cube, once it does, clone the template, change it to fit the needs slightly such as orienting it randomly (Or just having multiple random templates though I don’t know how efficient that’ll be) then summoning it relative to the position as such. If you want to show the splitting effect, you can animate it and then play the animation on the template or tween it :woman_shrugging:

8 Likes

It is possible, however, I don’t think it’s easy nor performance friendly. I’d wait until vertex deformation eventually becomes a thing.

8 Likes

Method 1: If you’re only using straight line, and aren’t doing anything fancy with the parts simply calculating wedge sizes might do? Saber “draws” line on block, spawn two wedges to fit block’s old position and also follow that linear path?

Or a combo of wedges and parts (along with some welds) to fit with the line the lightsaber “drew”

Method 2: Requires a little more processing, but having a big part that consists of many smaller parts, as the light saber touches those parts they are despawned/destroyed with fancy melting effects.

Might by laggy though with all the small parts, and would require some annoying building practices…

Of course, fancy optimization could be used to limit the amount of parts in the vicinity and also render them as singular parts for the client


I do however remember someone making a slicer for parts.
Edit: Found something close https://twitter.com/Windows8point1/status/981225808164397056?s=20

1 Like

I’d say go for a semi-realistic approach:
1 brick turns into 2 bricks with 1 wedge each like this:


with the special case of hitting the edge, in which case one side only gets a wedge:
image

Sure it isnt even 50% accurate, and it doesn’t work with big blocks, but it’s a simple idea that would look cool ingame :slight_smile:

6 Likes

CAUTION: Math and Trigonometric Manipulation content up ahead, turn back now before it’s too late.

Have no fear, for I think I have found what you’ve been looking for:

EgoMoose had a challenge on ScriptingHelpers that allowed people to find methods and ways to accomplish what he did in those gifs, Snack Break Problem #28 - Scripting Helpers

Luckily for us he provided the solution/source code, so you can take a look at it and see how you can implement it with your Lightsaber code.

https://www.roblox.com/games/2387944110/Snack-break-28

Goodluck!

48 Likes

From what I’ve read, many have overlooked realtime CSG. In theory, it’d be easy to just create your saber mechanics using Roblox’s in-built functionality.

5 Likes

Was surprised nobody mentioned this earlier. The best way to implement this is to start with the real time CSG and use EgoMoose’s solution as a backup as CSG often fails with a large number of manipulations.

In case an operation fails, you can check and destroy if the part is very small, or just ignore if it is a union.

1 Like

Hello there @vijet1

You have a really magnificent idea. And it is really possible to do too!

Your idea can be made with in-game solid modeling. Before I try to explain it own my own. You can take a look at it right here: https://developer.roblox.com/en-us/articles/in-game-solid-modeling

Union Operations With In-Game Solid Modeling


First of all we can add our part as a variable. After that you can take all the other parts that can union with the part.

local part = workspace.Part1
local otherParts = {workspace.Part2, workspace.Part3, workspace.Part4}

And finally we can make our Union Operation.

local newUnion = part:UnionAsync(otherParts)

Basic Demonstration With In-Game Modeling


First we will do the first step with unions. and then we will subtract our part instead of unioning it.

local part = workspace.Part1 --//Our main part.
local otherParts = {workspace.Part2, workspace.Part3, workspace.Part4} --//Other parts

local newUnion = part:SubtractAsync(otherParts) --//Negating the part

What I explained here is the same as the one in the wiki page. Roblox made a open sourced example with this api in this game. If you want you can take a look at it here: https://www.roblox.com/games/2309627316/Rotating-Windows

1 Like

Totally possible, as shown here:

https://old.reddit.com/r/roblox/comments/aqo82c/i_made_a_realtime_csg_module_to_cut_parts_and/

and playable here:

https://www.roblox.com/games/2650630470/Who-ate-my-spaghet

2 Likes

I was hoping someone would reply with a way to do vertex deformation, thanks.

Thanks for replying Syharaa. This fits great, I will just have to make it run more than once. After looking over the code, I have to say you were right, it is complex. I don’t know how it is performance wise, but since it works, I will mark you as a solution.

@ElliottLMz @nooneisback @skwoginc
After playing the game provided by skwoginc, I realized that real-time CSG provided by Roblox might not be such a bad idea. I know “Who ate my spaghet” uses it after reading the replies to the provided Reddit post. Although, I did see that it does not work well with spheres, I can work around this as i did say I am only looking to manipulate block parts in my original post. Thank you for replying!

2 Likes

The issue with speheres is that they have a high triangle count, quickly making the operation too complicated. It is better to make some kind of a custom “spheroid” by combining 3 normal parts, 8 wedges and 8 corners.

3 Likes

so like an octahedron but with the tips more flattened and the square edges also flattened?


untitled

Almost. 12 wedges actually. Forgot about the ones on the sides.

1 Like

Ik exactly what you mean

5 Likes