A Flash Developer Resource Site

1. ## Gravity & vectors

Hey all,

I'm trying to make a 2D side-scrolling homage to Zarch, one of the best games EVAR to grace the Acorn Archimedes. In turn, it owed more than a little to Lander, in that you had a mouse-controlled craft whose rotation and thrust were under your control.

It's my first experiment in using vectors to calculate movement, and it seems to be working pretty well so far. However, I've run into a problem with gravity. Basically, I'm calculating a vector for the craft's position each frame, and subtracting a value representing gravity from it's y co-ordinate. However, I'm forced to use a constant for gravity, as I'm not explicitly storing the x and y speed of the craft. Should I be? Or is there a better way of doing this?

Code for the craft movie clip follows.

Code:
```onClipEvent(enterFrame){
//steering
mDY = Stage.height/2 - _root._ymouse;
this._rotation = mDY;

if(_root.mDown && thrust < mSpeed){
thrust = thrust * accel;
} else if(!_root.mDown){
thrust = thrust / decel;
}
//rad =Math.sqrt(_root.square(speed.x) + _root.square(speed.y)) + thrust;

this._x += loc.x;
this._y += loc.y;

}```
accel, decel and _root.grav are constants stored outside of the enterFrame stuff:

Code:
```accel = 2;
decel = 1.2;
_root. grav=2;```

2. Hmmm.

On thinking about it a bit, I can see where I'm going wrong, and why I DO need to store speed. That'll teach me to post without working things through properly

3. if you want to do it properly with vectors you should store the x and y velocities and just add them to the x and y co-ordinates each frame. gravity becomes easy then as you just add a constant to your y velocity each frame

4. That's what I've ended up doing. Where I was having trouble was getting my head around the relationship between thrust and velocity, but I think it's sorted now. I'll post my WIP when it gets to a playable state if anyone's interested.

5. thrust relates directly to the acceleration on the object

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap