Results 1 to 1 of 1
Thread: velocity issue
-
October 7th, 2010, 07:40 PM #1869Registered User
postsvelocity issue
The following code moves a ball down a slope with appropriate gravity based on the angle. What i am trying to do is apply the velocity vector to a new slope once encountered but i cant figure it out.
Code:var slope:Number=.2; var g:Number=9.81*40;//40 pixels per meter var ball:Shape; var dt:Number=1/30;//delta time var v:Point = new Point();//velocity QuickTests(); function QuickTests() { stage.align=StageAlign.TOP_LEFT; stage.scaleMode=StageScaleMode.NO_SCALE; addEventListener( Event.ENTER_FRAME, onEnterFrame ); ball = new Shape(); ball.graphics.beginFill( 0, 0.5 ); ball.graphics.drawCircle( 0, 0, 10 ); addChild( ball ); ball.x=ball.y=100; graphics.clear(); //draw a line segment to show the platform graphics.lineStyle( 1, 0xff00000 ); graphics.drawCircle( 100, 100, 3 ); graphics.lineStyle( 1 ); graphics.moveTo( 100, 100 ); graphics.lineTo( 100 + Math.cos( slope ) * 500, 100 + Math.sin( slope ) * 500 ); } function onEnterFrame( event:Event ):void { var normal:Point=new Point(Math.sin(slope),- Math.cos(slope)); var reaction:Number=Math.sin(Math.PI/2-slope)*g; v.x += ( normal.x * reaction ) * dt; v.y += ( g + normal.y * reaction ) * dt; ball.x+=v.x*dt; ball.y+=v.y*dt; }
if i were to say the ball encountered an opposite slope at mid screen like so:
i want to apply the current velocity to that slope so it rolls up it until eventually rolling back down again. If someone would please help or point me in the right direction that would be great.Code:if (ball.x>=stage.stageWidth/2) { slope=-.2; }else{ slope=.2; }
thanks
to give proper credit, the code is from:
http://www.actionscript.org/forums/s....php3?t=231243

Reply With Quote

Bookmarks