miniStrike part 2

How I made tentacles.

Yesterday, I implemented the boss for the second area in miniStrike and I designed this guy to have tentacles that moving slowly to the player and shooting bullets to the player when the tip pointing toward the player. It took me hours to implementing the tentacles.

I think I should write a post about how I made it for anyone who looking for a starter idea for implementing their own tentacle code. I can’t say that It’s the best solution but It did the job.

Logic

In my design, The tentacle consists of tail, body and head.

  • the tail part always sticks to the boss body. ( 1 node )
  • the body part link between head and tail. It consists of many nodes connecting each other with limited length. ( an array of nodes )
  • the head can move freely but constraint by the length of a body part. ( 1 node )
  • The head can’t move far from the tail more than the sum of length between nodes

These are steps that require todo every update() before drawing to the screen.

  • Update the target position of the head. I make it move closer to the player’s position.
  • Update the target position for all nodes in the body starts from head to tail. This will pull the node toward the head. (body[1], head), (body[2],body[1]), … , (body[last], tail)
  • Update position for all nodes includes head and tail.
  • Update the target position for all nodes in the body starts from tail to head. This will pull node back toward the tail. (body[last], tail), (body[last-1], body[last]), … , (body[1], head)
  • Clamping the target position of the head by the sum of maximum length between nodes.
  • Update position for all nodes include head and tail

After these steps. It will have tentacle that movement feels good enough. It will push or pull neighbour nodes to make the distance.

Variables and Functions

Constant values.

  • maximum_length: maximum length between 2 nodes.

The data structure of a node.

node = { x, y, target_x, target_y }

x, y is the current position of the node.

target_x, target_y is the target position that node needs to move closer.

Functions

  • add_node() – The function will create and add new node into the body. It always adds to the end of the array. A new node will appear close to the tail.
  • adjust_target_position( adjust_node, reference_node) – The function will find an angle between 2 nodes and It will update the target position from the current position of adjust_node using the angle and maximum_length variable.  ( target_position = current_position + cos/sin( angle ) * maximum_length )
  • update_node_position( node ) – I use lerp() in this function for update current position toward target position.
  • clamping_current_position( adjust_node, reference_node, radius) – Function will clamp the current position of adjust_node if the distance between reference_node and adjust_node exceed the radius.