Simulating electric circuits?

Hello fellow programmers! I need to somehow simulate electrical circuits in the sense of having a source, then a line of cylindrical objects and then the output. This sounds pretty simple, but what if I want the line to split to two parts and have two outputs? And what if I want to incorporate logic gates? I am looking for something like Minecraft’s redstone, but in 2D. Any help appreciated! Thanks in advance! :slight_smile:

You should take a look at snap circuits. You can duplicate that system in Unity. Here are some kits that could give you some ideas…

Snap Circuits Extreme SC-750

Snap Circuits SC-750R Student Training Program

Was that sarcastic? I hope not because you probably didn’t understand what I am asking, or the most probable of all, I didn’t express myself right. English is not my primary language, and I run into trouble sometimes. Let me rephrase my question. How can I convey signal (on or off) from an object to neighbour objects? I know how circuits work, I just don’t know how to replicate them.

I wasn’t being sarcastic. I misunderstood you. Sorry, about that.

Sending bools from each other?!

Good question, I hope it gets answered :smile:

Cool, but how can I see which objects are next to the current object? I’d prefer an array of GameObjects so I could access their components and change their variables.

Never mind :slight_smile: Thanks for your answer anyway!

Create a script that you add to each object with a function to receive a broadcast from other objects. Pass the bool to any object using broadcast.

Maybe you could raycast on all sides then add the hit objects to a array of items to edit? I know it would be bad performance wise but you only have to do this when you place a new part of the circuit so it shouldn’t be to bad… there is probably a better way.

Cool stuff, but it still seems too “messy” to do it that way? It’s more of a chain reaction effect. It’d be a lot easier if I had a grid, and checked the positions adjacent to the object on the grid, but I can’t figure out how to do that :frowning:

What’s a broadcast? Is it a Unity function? Could you provide some sample code? I just realized “Reply” doesn’t quote the post. LOL This is a reply to arkon.

Minecraft’s redstone is simplified from a real circuit because of the time it takes for the signal to travel down the paths. Real circuits work much faster, and so simulating them is hard.

If you want to do it MC’s way, then it’s as simple as mapping out the circuits in data, and then making a display that shows them. You could make 2D blocks a la MC, I guess, but I think it’d be a lot easier to handle everything on the backend and then just show the result.

Thanks, this gave me an idea which I"ll try once I get home. How do you think I can “map” the circuit in memory? Like having a “grid” of places in a multidimensional array and checking for the neighbour tiles to contain something? I’d still have to have a grid like implementation for the graphics as well, right?

A very simple way to approach it is probably to have a “circuit block” base class that contains a Value field that represents the state of the block, and has some methods that allow the block to execute an “on” state action and an and “off” state action. That will fulfill the basic requirements of most digital circuits. It might look something like this…

I’ve written this code off the cuff and is untested and may contain errors.

public class CircuitBlock : Monobehavior {
    public float Value; // Set the initial value in the inspector
    public List<CircuitBlock> InputBlocks; // Set the inputs in the inspector
   
    private void Start() {
        Init();
    }
    private void Update() {
        if (IsSwitchedOn())
            OnAction();
        else
            OffAction();
    }
   
    public virtual void Init() { }
    public virtual bool IsSwitchedOn( return false; }
    public virtual void OnAction() { }
    public virtual void OffAction() { }
}

Then you can code more specific blocks, such as light switches, timers, wires, resistors, capacitors, etc. Here’s how a simple lightbulb block might be coded…

public class LightbulbBlock : CircuitBlock {
    // set these values in the inspector
    public GameObject LightOn; // the object that represents the lightbulb turned on
    public GameObject LightOff; // the object that represents the lightbulb turned off

    public override void Init() {
        // the light will initially be turned off
        LightOn.renderer.enabled = false;
        LightOff.renderer.enabled = true;
        Value = 0;
    }

    public override bool IsSwichedOn() {
        // the light will only turn on if all inputs are greater than 0
        foreach(CircuitBlock input in InputBlocks)
            if (input.Value <= 0)
                return false;

         return true;
    }

    public override void OnAction() {
        if (LightOn.renderer.enabled == false) {
            LightOn.renderer.enabled = true;
            LightOff.renderer.enabled = false;
            Value = 1;
         }
    }
    
    public override void OffAction() {
        if (LightOff.renderer.enabled == false) {
            LightOn.renderer.enabled = false;
            LightOff.renderer.enabled = true;
            Value = 0;
        }
    }
}

This should work somewhat like Minecraft’s system, as best as I understand it from videos I’ve seen (I’ve never played Minecraft). Of course, this isn’t how a real electrical circuit works, but it should work well enough to simulate very simple digital circuits.

That good, but I think the question is how to handle the current.

I would have a reference for each connection to the item. A lightbulb would have 2 connections. And depending on the lightbulb, maybe it only works when current comes from 1 direction and leaves the other.

MC way: Power is turned on. Send a signal to the first node/block/whatever. On each cycle, each node sends a signal to the next node about whether or not there’s power flowing. The signals continue to flow each cycle even after the power is cut, until they all reach the end.

More realistic way: Power is turned on. A signal is sent to the first node, which immediately passes it to the next node, and so on until it reaches the end. Every cycle sees the power flow through the whole system. It stops functioning immediately when the power is cut.

Edit: Oddly enough, I actually plan to write a game that relies on current, so I’ve spent a little time thinking about this already, but haven’t actually implemented anything yet.

You don’t need to handle current, only voltage and resistance. Current comes along for the ride. Recall Ohm’s law:
V = IR

To simulate a digital circuit you don’t actually need any electrical quantities. All you really need an ambiguous “signal”. You can use that signal any way that you want.

That’s what my example above basically does. In fact it isn’t even simulating a circuit in the strictest sense. For one, there’s no loop requirement. Secondly, there’s no requirement to drop from high potential to low potential. All it does is propagate an ambiguous signal from block to block in one direction.

That’s very helpful, though I still don’t have a way of conveying the signal across from the input, through a set of wires that may or may not split, to the output.

Guys I probably exaggerated a bit when I said “simulation”. I don’t need to handle voltage or resistance. Just convey electricity though wires, from the input to the output. No actual “simulation”. Any way to do that?

EDIT: Here’s a sketch:

IN----OUT

IN is the input and is a GameObject

----- are wire parts, all GameObjects

OUT is the output, a GameObject as well

I want to have electricity going through the wire parts, starting from the input and reaching the output.
But I’d like to be able to do this as well:
^^^^|------OUT
IN–|
^^^^|------OUT

But I want the electricity to go through both routes. How can I achieve this? Thanks in advance and thank you all for your kindness! :slight_smile:

P.S. Ignore the ^'s I just put them there because my text got messed up otherwise…

Outputs are not actually necessary, the only thing you need is the input list. If you want two blocks A and B to reference each other such that A is essentially the output for B and B is essnetially the output for A, then simply add A to B’s input list and add B to A’s input list…

A.InputBlocks.Add(B);
B.InputBlocks.Add(A);

That creates a circuit…
A —> B —> A

And the nice thing with Unity is that you don’t even need to write this code. Just use the inspector panel in the Unity editor. Drag and drop the game objects that include a CircuitBlock component into the desired InputBlock list in the inspector panel.

        |------OUT (B)
(A) IN--|
        |------OUT (C)

B.InputBlocks.Add(A);
C.InputBlocks.Add(A);

Or set up the references in the inspector. Simple as that.