# Sprite movement options?

## Recommended Posts

Trying to setup different movement options for enemies in my Paddle game. Right now I have all 4 enemies initiating in a random y location and with random velocity.x/.y and set to bounce on the world sides from the right side of the screen to the left. I'd like to set up completely different movements for all 4 enemies but I've struck out on finding anything describing anything besides linear movement. Anyone know some good tutorials on how to do setup different movements?

##### Share on other sites

It's linear because those x/y values never change, if you change them periodically (not intermittently, periodically, use a function with a period) then you can get some interesting movement pattern, they'll still be repetitive but with the bouncing on the edges it might break up the repetition.

You could add timers i.e. change the x/y values, (even using 0,0 for some length of time) after a set amount of time. Very similar to using a periodic function (such as sine) to help alter movement.

You can add some really nice stuff if you change around how your entities actual move. This is slightly harder on the maths but not too much. Consider giving them a direction (or heading) and a velocity (a single velocity) and then each frame calculate the required x/y movement based on angle and velocity (you can get even smarter and use vectors for this to describe both direction and magnitude all in one go although I generally find a unit vector to represent direction coupled with a velocity more intuitive, it ends up the same anyway).

With a heading and a velocity you can change each value independently and stuff like chase/avoid behaviours become much easier to implement. When you introduce an angle you can calculate the relative positions of things and react accordingly i.e. you can give an entity a set speed but always have them turn towards the player (or the mouse or whatever).

You can go crazy with this an introduce influencers and repulsors. These objects sit somewhere in your world, when an entity enters their radius of influence they can either exert a pull or a push force on the entity, either directly influencing their position or affecting their direction/heading. They could be invisible or visible, fixed or moving, why not have a couple of moving invisible repulsors flying around? That would create the illusion of very random movement, but smooth.

The key to all this is understanding a bit of maths. Brush up vectors and how they can be added/subtracted or otherwise manipulated (cross, dot products etc) if you don't already know. Ditto for creating non-linear curve functions. Try feeding the current time into a function that passes the time value through a sine wave and adding that onto the lateral component of your movement i.e. if you entity is moving with x/y as 10,0 then just changing the y value by values from sine (i.e. `Math.sin(time) * 5`) would make the character wobble up and down as they generally move right across the screen.

##### Share on other sites

Thanks matt! Didn't think to have a function call on each update to change the motion.

##### Share on other sites

I think part of it is learning to think with time involved, after all, without time nothing changes, movement is the change in position over time and whilst in programming we sometimes fall into the trap of directly manipulating location, we should, at a high level, be controlling our entities by other means, not by direct positional manipulation i.e. change the heading, change the forces acting on the entity, change its mass, all these things should influence the position rather than just directly setting the position.

Programming emergent behaviours, I think, is one of the most exciting things to do because it is all about modelling systems declaratively and it is super interesting to find seemingly complex behaviours emerge from the interaction of very simple pieces.