Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

AI controller using physics

Discussion in 'Scripting' started by Serinx, Nov 18, 2019.

  1. Serinx


    Mar 31, 2014
    Sorry if this doesn't really belong here.

    I'm trying to think at a conceptual level how a physical entity that's controlled by physics, could be controlled using AI.

    Imagine a top down 2d game where you have a spaceship.
    It moves and turns by using thrusters facing different directions and placed at different points around the ship.
    Lets say the ship needs to automatically turn to face an enemy ship to its right.
    How would the AI know which thrusters to enable in order to rotate in the correct direction?

    My thoughts were this:
    1. When initialising the ship, find it's center of mass
    2. loop through all the thrusters on the ship, depending on their rotation and position relative to the center of mass, assign them a flag that says if they're a "left turner". "right turner", "forward thruster" or "back thruster".
    I suppose in some cases, they could be a turner and a forward/back thruster, or even a "left/right strafer"
    3. When the AI detected a nearby ship, if it's on it's right, it will activate all the "right turner" thrusters until it's facing the ship.

    I wonder if anyone else has designed something similar to this and has any ideas.

    It's going to be some fairly complex coding, so I'd rather get some input from you guys before diving into it!

  2. Antypodish


    Apr 29, 2014
    Can you not simply make center of mass, at center of model, and simply add rotation left/right?

    What is the purpose of controlling thrusters in your case?
    You know smoke and mirrors ... :)

    Even tho, your case is trivial to resolve.
    Providing spaceship default position is facing up:
    - Turning left, you fire top right and bottom left thrusters.
    - Turning right, you fire top left and bottom right thrusters.
    All operates on local coordinate space of ship.
    You don't need to calculate, where thrusters are.

    Thats it, simple as that.
  3. Antistone


    Feb 22, 2014
    The generalized version of that question is "given that I only have access to the following controls, how do I use them to accomplish my objective?" and that's one of the top-level central problems in AI. Other examples of this problem include "how do I win this game, given that I can only make the following legal moves?" and "how do I climb a staircase, given the following sensors and actuators?"

    Given a fairly restricted domain (like a video game) and a fairly limited budget (like a hobby project), you probably want to do something that relies on the programmer's expert knowledge of the special rules of the domain in question--i.e. you know generally how this game works and you can invent some pretty good rules-of-thumb that work for your game even though they wouldn't work for other games.

    Generalized approaches to this problem that don't rely on expert knowledge of the domain generally involve things like reinforcement learning or similar trial-and-error techniques. I remember some years ago watching a program about a competition where small robot dogs were programmed to play soccer (and different researchers' teams were pitted against each other), and one team talked about how they had trouble teaching their robot dogs to run quickly on different surfaces, so they created a program where the robots would just try a bunch of different gaits and measure how fast they were; it managed to come up with better solutions than the human programmers could construct by hand.
  4. Serinx


    Mar 31, 2014
    Thanks for your input, but not quite sorry, I should have given more information. I want to use the physics engine to control the turning using forces, the center of mass will be different depending on how the ship is constructed so it needs to be dynamic.

    Well, you do need to calculate where the thrusters are to determine if they are "top right", "bottom left" etc.
    I suppose you're thinking of this as controlling one static spaceship and assigning each of the thrusters into different categories, but I want a more dynamic system that doesn't need to be told which thrusters do what - you just throw a thruster anywhere on the ship and the AI will use it to turn or reverse or melt enemy faces.

    Thanks for your input. I totally get what you're saying, but I believe that this could be applied to many games and I'm sure it has been done already.

    Imagine this example, you've got a truck that's controlled by AI. It's got anywhere from 4-16 wheels.
    It drives down a narrow street and it needs to turn around using a 3 point turn without hitting the edges.
    Assuming all wheels can be turned.
    The AI decision tree might look something like this:
    1. decide whether I have more room on my left or right side
    2. turn my front wheels toward the larger side, and by back wheels the other way
    3. drive forward as far as possible without a collision
    4. turn all wheels in the opposite direction
    5. reverse until facing the right direction

    My question is more about how does the AI decide which wheels to turn (and which thrusters to fire).

    Given a physics objects of any type, the logic should be pretty much the same. I'll add a picture that might help clarify things:

    Imagine that the arrows are thrusters that apply the same amount of force in the direction they're facing.
    Each square is a rigidbody with some mass.
    The black square is much heavier than the others, making it the center of mass.

    How does the AI work out which thrusters it can use to make the object spin clockwise?

    We can see at the moment that activating the red, green and yellow may do this, but it would be different if we shifted the center of mass.

    I'll have a play around as you suggested through trial and error, but there must have been plenty of solutions to this problem already. I'm just trying to not reinvent the wheel (no pun intended).
    Last edited: Nov 19, 2019
  5. Antypodish


    Apr 29, 2014
    Your problem still is not that complex, as it seems.
    You can calculate thrusters position, in respect to the CoM (Center of Mass).
    That providing you want to rotate along CoM.
    Knowing position, you just look into orientation.
    Now you save configuration, knowing which thruster can react to what command. Like forward, back, strafe, turn left / right (using either, or both forward/reverse and side faced thrusters)

    See this vid from my signature below.
    [DOTS] Hovercrafts Sandbox Prototype

    For 3DoF system, problem is much more complex however.
  6. palex-nx


    Jul 23, 2018
    You need something like integral controller. Let's say your AI have some variables to output and those var are trusters power for each truster. If you increase or decrease one of them ship vector changes. If ship vector changed in desired direction, then continue to increase power onto this thruster, else decrease it. Do it with every truster, calculate error, apply change to thruster power.
  7. Serinx


    Mar 31, 2014
    Alright, I just decided to go with my theory and it works great, although I don't like having massive conditional statements for assigning the thrusters to groups, it does make it an easy interface for accessing those groups of thrusters for AI so I guess it's a double edged sword. Performance doesn't matter either because I just cache it all up front.

    Here's a gif of the AI controlled ship chasing after some target :)

    palex-nx and Antypodish like this.
  8. Antypodish


    Apr 29, 2014
    Well done.