dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Gravity & vectors

  1. #1
    Junior Member
    Join Date
    Oct 2004
    Posts
    4

    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;
    	rad =thrust
    	loc = {x: Math.cos(_root.degs2rads(this._rotation-90))*rad, y: (Math.sin(_root.degs2rads(this._rotation-90))*rad) + _root.grav};
    		
    	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;
    Last edited by prehensile; 10-11-2004 at 12:18 PM.

  2. #2
    Junior Member
    Join Date
    Oct 2004
    Posts
    4
    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. #3
    Senior Member
    Join Date
    Jul 2000
    Location
    Sydney, Australia
    Posts
    486
    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. #4
    Junior Member
    Join Date
    Oct 2004
    Posts
    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. #5
    Senior Member
    Join Date
    Jul 2000
    Location
    Sydney, Australia
    Posts
    486
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center