scripting enemy movements

Posts: 61
Joined: 2007.01
Post: #1

I'm working on a space shooter (galaxian / space invaders style) and I'm a bit stuck on the enemy movements.

See how the enemies move in this video: Demon Attack

I'm guessing these are scripted / hardcoded movement patterns. I've been trying to do something similar with an array of (x,y) coordinates that the enemies iterate through during their life time. Each element in the array is a pair of values to add to the x and y coordinates of the enemy.

It doesn't look that great (quite jittery) and I end up with a huge array of coordinates which feels inefficient.

I've also tried using sine and cosine wave based movements - which doesn't look too bad but doesn't give the variety of movement I'm after.

I'd be really grateful for some advice / tips / links!


Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
IDK how it's traditionally done, but I'd probably use bezier curves for something like galaga. They're straightforward to code and easy to create/edit, even adjusting the coords by hand if you don't have time to hook up a graphical editor.
Quote this message in a reply
Posts: 3
Joined: 2011.09
Post: #3
Looks like there are at least three components to the enemy movements in the video. First, an overall downward movement, towards the player. Second, a rough circling motion. And third, some responsiveness to the player's x position.

One way to model that could be to add together:
a y component that is gradually increasing,
a sin-based component, that creates a circular motion (maybe an oval with a rotating axis), sometimes randomly reversing to keep things interesting,
and an x component that will increment a fixed amount to the left or right every second or so, based on whether the player is to the left or to the right of the enemy.

...that might be an oversimplified description of the motion though. Try watching the slow-motion part many times and seeing if you can make an exact description of the movements. If you can describe it, you can model it!
Quote this message in a reply
Posts: 61
Joined: 2007.01
Post: #4
Cheers for the replies guys!

What I ended up doing was changing the way I was doing the coordinates array. I've replaced the coordinates with a direction. I'm working in JavaScript so it's kind of like

function MovementData(dir) {
   this.dir = dir; // 0 = up, 1 = down, 2 = left, 3 = right;

And then in my alien's update function, I have a switch statement that moves the alien in the direction of the current move.

Alien.prototype.update = function() {
  switch (moves[currentMove]) {
       case 0:
       // etc etc.

It's not high tech but it's working well. I find I have more control over the movements and have been able to create 3 distinctive movement patterns quite easily.

It also means that I can add an action to the MovementData(), for example a "shoot" action, fairly easily.
Quote this message in a reply
Post Reply