Download

Cable Component Plugin for UE4

Lead programmer James Golding is back to share a cable component plugin he created for UE4. Read on below or check out the original post on our blog at unrealengine.com!
[HR][/HR]
Here at Epic, we get one day a month nominated as ‘Epic Friday’, on which we can work on pretty much anything we like. A couple of months ago I worked on a really simple ‘cable component’ using rope physics for hanging up in levels, to add a bit of life. It’s an effect that has been seen in other games for years, but I wanted to see how easy it would be to add as a plugin to UE4! Here is what I ended up with:

https://www.youtube.com/watch?v=WdrtNUSJZkY

Simulation

To do the actual cable simulation, I used a technique I’ve played with a couple of times called ‘Verlet Integration’. It is very well known in game development; you can find all kinds of articles about it, the first one I remember is from Thomas Jakobsen (Gamasutra - Features - "Advanced Character Physics" printer friendly](Gamasutra - Features - "Advanced Character Physics" printer friendly)).

The idea is to represent the cable as a series of particles, with ‘distance constraints’ between them. The particles at the ends are ‘fixed’ and move with whatever they are attached to. The ones in the middle are ‘free’ and fall under gravity. Each step, you update the velocity and position of each particle, and then move them to satisfy the constraints. The ‘stiffness’ of the cable is controlled by the number of times we iterate to enforce the constraints, each step.

https://forums.unrealengine.com/attachment.php?attachmentid=485

I won’t go into the maths here, but if you have the UE4 source code it’s only about 60 lines of code in the [FONT=Courier New]UCableComponent class, inside the functions [FONT=Courier New]VerletIntegrate and [FONT=Courier New]SolveDistanceConstraint.

Rendering

Now that I had a nice chain of particles bouncing around, I needed to render it. This was a pretty good test case for creating a new type of geometry in UE4, and it turned out to be quite easy. I had to create a new [FONT=Courier New]FCableSceneProxy class, which is the renderer representation of the cable. Each frame I pass the new particle positions from the simulation (which is done on the main thread inside [FONT=Courier New]TickComponent) to this proxy, via the [FONT=Courier New]SendRenderDynamicData_Concurrent function. On the render thread I lock and update the index and vertex buffers to make a ‘tube’ mesh. For each vertex I need to calculate a position, a texture UV, and three tangent basis vectors. X points along the cable, Z points straight out from the cable (the normal), and Y is perpendicular to X and Z.

https://forums.unrealengine.com/attachment.php?attachmentid=484

I expose properties on the component for number of sides, radius of the tube, as well as how many times to tile UVs along the cable.

Attachment

Now that I could see the cable and play with it, I wanted to attach it to things. Unreal Engine 4 already allows any SceneComponent to be attached to any other, so I just added an option to attach the ‘end’ particle to a different component, with an offset. The final details for the cable look like this:

https://forums.unrealengine.com/attachment.php?attachmentid=487

And that was it! You can try this out by adding a Cable Actor to your level (or a Cable Component to a Blueprint). It is certainly a fun addition to UE4, but there is a lot more that I would like to see added:

• Add collision with the world.
• Response to forces, explosions etc.
• Multithread the simulation instead of doing it on the main thread.
• Stop rebuilding the index buffer every frame as it doesn’t change.

What about you guys? What would you like to see added? Find me over on twitter at @EpicJamesG](https://twitter.com/EpicJamesG) or let us hear about it below! Your feedback on our tools and these tutorials is important to us.

Neat!

Is there a relatively easy way to generate these at run time (i.e. during gameplay) rather than placing in the editor?

Awesome! Just what my project needs!

This can be an insanely useful(and fun :eek:) tool with world collision.

This is extremely cool. I plan to try this out sometime.

Can you have it grow / lengthen ?

Great feature!
Any chance of making a tutorial of it using blueprints?
Or at least more details on how to make it?

Thank you.

Love this! I was needing something just like this earlier today to prototype something for a video render in matinee (that has moving meshes that I wanted naturally moving cables dangling from) and this looks like it’ll do the trick! thanks :slight_smile:

EDIT > 10 minutes later, I can confirm this works like a charm. Got the exact thickness, length and stiffness of cable I needed going from a rotating machine arm to it’s sliding front cover (attached as child to arm, attached cable to sliding cover) run the animations and sure enough it moves and reacts like cable. I had no idea this was sitting there in UE4 until I saw this post!

Thanks again. Nice work. :slight_smile:

To the guy above, drag the cable actor into your level. Position it on point A (If point A is a mover/animating mesh then attach it as a child), in the cable actor properties you can set all the relevent stuff like length, segments, amount of polys for a smoother cable, stiffness, material etc and the ‘attach end to’ is simple (use the dropper to click on the mesh in the viewport) and it ‘just works’. I didn’t touch blueprint but my needs were simple - Attach a cable between two related moving meshes (that move at the same time and are simulating part of the same machine) the cable just gives it a bit of life vs the usual static mock cable tube.

One question to James if I may, at launch the cable instantly reacts as if it’s had some force (even though it’s attached meshes start out stationary) it’s like it pops into life rather than using it’s rendered resting state as it’s starting state, if that make sense? if so is there anyway to bypass it so when you hit play/sim the cable doesn’t bounce wildly as if it’s been hit? I looked through the parameters but couldn’t find anything. Thanks.

Thank you.

I saw this too but have been unable to track it down thus far :frowning: If anyone want’s to dig in and fix it and make a pull request on GitHub, that would be great!

Is it Possible To “shoot” the Cable out? Like I Line Trace to one Spot and than to another one and let an Cable Spawn with these specific Start and End Locations? I tried to create this but I just can’t make the End Location happen and can’t change it in my Blueprint. The Start Location is always also the End Location, I would like to see an “Set End Location” Function to control that value or is it possible to do otherwise?

My post seems to have vanished after I hit reply.

Thanks James for your reply, It’s certainly not the first time I’ve come across simulated physics that have to ‘warm up’ so to speak before being ready for prime time (particle systems in general tend to be that way? I’m not sure). It’s not a deal breaker because there are work arounds (camera delays if for matinee sequences, or if in game simply they would have stopped bouncing by the time the player sees them - typically).

I have noticed in the editor the cables can disappear (but are ok in game/play mode). This occurred after click drag copying a cable, and changing it to attach to another mesh. The cables then became erratic in display, based sometimes on small distance or angle changes (of the editor camera - in perspective/3D viewport). I’m only using a messy test level at the moment so it may well just need a rebuild and clean up but thought I’d put it on record in case anyone else notices.

I once did the same thing in UDK, but since it was meant as part of a destructible environment (thanks for the destructible meshes BTW) I created it rather differently: I created a cable mesh in 3DS Max, and used a number of bones along the mesh (very much like the free particles discussed here), and created bodies for them in PhAT. I would then attach the first and last bone using physics joints to other actors. This allowed for interaction with physics and also allowed me to “tear” the cables using the joints’ break force. Seeing this post got me wondering: which is the better solution in terms of performance?

Is there a way to download this plugin? Thanks.

It’s already in UE4 - in the classes browser (cable actor) :slight_smile:

Very cool tool, I love to see new and exciting real-time solutions like this. I do have a question about the system though. Would it be possible to have the particle positions drive a skeleton instead of the final thing deforming vertices? I would think (you will have to set this me straight on this) you would be able to “drive” much more complex meshes with a skeleton rather than deforming the entire mesh. I’m sure everyone has seen people use a similar set up in Maya where you drive joints with a dynamic hair system or ncloth and then those results are baked down. It would be awesome to see this be a dynamic process instead of a pre-baked solution.

Been working with cable component lately. Noticed that the bigger the number of segments the longer the cable is and also it behaves less like a rope in this case, but more like a rubber… cable exactly. Should not cable length be exactly CableLength?
Some test results:
CableLength = 50, NumSegments = 20 - Actual length - 60
CableLength = 50, NumSegments = 50 - Actual length - ~105
Edit:
So cable totally stretches between start and end particles. Even though constraint rest length is calculated from CableLength, which does not get changed due stretching, that’s something that confuses me…
Apart of it, implemented mass for particles and stiffness for constraints, results are pretty nice. But i also experience some weird bug, cable disappear after some game time and appears once again if i move it on X axis.
Edit #2:

Here’s the patch, sorry for not using github.
http://pastebin.com/jUg322ea

What was changed:

  • Particles have masses now
  • Constraints stiffness implemented
  • Each particle could be fixed(hard-attached to object) There are some problems though, read below
  • Each particle could have attachment(initial work)

Current issues:

  • Stretching problem mentioned in first edit
  • Cable also stretches once rotational movement was applied Normal behavior, to get rid of stretching if needed, it’s required to check position between particles to satisfy constraint before calculating new position, or any other method similar to this
  • Accessing FComponentReference of FCableParticle in editor panel leads to crash Unable to fix particle at object due to this (Update: Fixed this by adding particles in the loop instead of adding a bunch of uninitialized elements and modifying values then)

Update: after testing various parameters found out that the following parameters produce best results:
Stiffness - 1.9
Iterations - 8 to 10
While number of iterations of 2 and stiffness of 1.5 provide stiff enough cable with less computations.

Some cloth using updated code. Sorry for large pics.

No collisions so far.

Once collisions for generated meshes implemented(it’s possible to use complex as simple with generated meshes now, but it has limitations, AFAIK), it would be possible to detect mesh collision and then apply projection to particle closest to the contact point.
Self-collision is much harder topic. Implementing angular constraint would have some visual effect of self collision, but it won’t be enough of course.

Hello,

I’m looking for a cable solution like this in unreal but am no coder.
Can anybody help me with a working example and how to set this up?
The first part is just setting up static connections in the editor, but in the end I would like to be able to connect and disconnect the cable to see how an object attached reacts.
Also see if it’s possible to for example tension one rope, and see what happens.

I’m looking into using unreal engine to do proofs of concept and see if I can use unreal engine as a dynamics playground for pre-engineering solutions.

Any help would be very appreciated

Thanx

Peter

Hi.

There’s is no way to attach objects to the cable yet, only attach cable to object. Actually it’s not even one-way interaction. The tension you’re expecting to see also can’t be achieved currently.
Also, you would not be able to disconnect one end of the cable as it will be immediately snapped to the other end. I’ve been working on improving cable component, some of the results are in pastebin above, but it’s totally not ready to use.