Public Group

# Need clarification on movement in a 2D plane

This topic is 3773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Currently I'm trying to work on movement AI for enemy Spaceships. Simple stuff, just left, right, up, down movement. I've implemented player movement by doing the simple move() function like this: (Java)
public void move(Direction dir) {
switch(dir){
case NORTH:
if((coordinate.getY() - y_speed) >= 0){
// Set the sprite to the appropriate frame
sprite = SpriteStore.getStore().getSprite(SHIP_PLAYER_UP);
// Move the ship
coordinate.moveY(-y_speed);
}
break;
case SOUTH:
if((coordinate.getY() + y_speed + sprite.getHeight()) <= BACKGROUND_HEIGHT){
// Set the sprite to the appropriate frame
sprite = SpriteStore.getStore().getSprite(SHIP_PLAYER);
// Move the ship
coordinate.moveY(y_speed);
}
break;
case EAST:
if((coordinate.getX() + x_speed + sprite.getWidth()) <= BACKGROUND_WIDTH)
coordinate.moveX(x_speed);
break;
case WEST:
if((coordinate.getX() - x_speed) >= 0)
coordinate.moveX(-x_speed);
break;
}
}



##### Share on other sites
Ugh...hate to bump my own topic.

##### Share on other sites
I do a lot of that sort of thing in my games.

Here are the basics that I almost always seem to need:
- Current Position (Current X & Y)
- Current Linear Movement State ... Heading Angle, Current Linear Speed
- Max Linear Speed
- Acceleration Rate

(Note: I only store Mass if I plan to use it in some sort of collision/reaction math, or have other outside forces, like gravity, acting on everything.)

Each frame you would want to compare your target position to your current position ... If they are different then you will want to update your heading angle and linear speed to move towards your target.

I usually do this by determining the Target Heading and getting X and Y components of that angle multiplied by the Acceleration Rate (AX and AY). Also, get your X and Y velocity components by using your current Heading Angle and Current Linear Speed (VX and VY).

Then, VX += AX and VY += AY. Use the updated VX and VY to determine your new current Heading Angle and Linear Speed. Cap your Linear Speed at the Max. And use these Heading and Speed values to change your Position.

If you want to get tricky, you can also apply the math to slow down as you approach your target position. However, if your enemy ships are simply trying to ram into the player ship, then that shouldn't matter too much, eh? ;-)

I hope that was helpful ... let me know if you need more details on anything.

-Matt

##### Share on other sites
Quote:
 Original post by MattWordenI hope that was helpful ... let me know if you need more details on anything.

Yes it was, thank you for your response. What do you mean by Current Linear Movement State? What is the point of origin for your Heading Angle? What if you don't care about rotating your Ships, just moving them left/right/up/down?

I'm curious as to how 2D Vectors work in a situation like this.

##### Share on other sites
I'm using an angle + magnitude vector to describe the current linear movement state ... heading and speed. Heading is the direction in which you are moving and the speed is how fast you are moving in that direction. So, the origin would be your current position.

If you are only going to move in the 4 cardinal directions, then your "heading" would only hold 1 of 4 possibilities: 0, 90, 180, 270 ... if you're working in degrees. But, by using a heading angle, you would be able to handle the math needed to move in any direction in the 2D plane.

Rotating your ships would depend on a separate "Facing" angle ... which may be related to your heading in some way (spaceships in video games tend to face in the same direction as their heading ... but not always). So, you could do something in the game to determine your Facing, and then use Facing to determine how you graphically show your ship.

A good, long term example of Heading an Facing being separate is the game Asteroids ... the player can rotate to change the ship's Facing and then thrust to change the ship's Heading.

But, yes ... being able to switch between angle+magnitude vectors and their X/Y components is very handy in 2D movement and physics for games.

-Matt

##### Share on other sites
Maybe you could find Steering Behaviours interesting. They allow for pretty much that kind of movement, and they can be combined in any way so that your AI pilot can turn them on/off whether he wants to intercept a target, flee from it, fly in formation along other ships, or all of it at the same time.

You'll have to play with the tweakable constants to get a convincing movement for the kind of vehicles you're using though, but I think it's worth the effort.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 27
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631407
• Total Posts
2999914
×