Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

the code for throw a ball physics - HELP!

New Here ,
Mar 14, 2010 Mar 14, 2010

Does anyone know where I can find some tutorials or explanations of the code for throw a ball physics? the code similiar to beer pong game or tossing a paper at something with a collision.

I would be very appreciate oif anyone could help me with a code about similiar physics, please

M




TOPICS
ActionScript
1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 14, 2010 Mar 14, 2010

You can use a Tween with easing to give the effect of a ball soaring high till a slow stop and then falling down with a bounce effect. You just have to know the fact that the X component of the tween will move at a constant speed since the horizontal motion is not affected by gravity. The Y component on the other hand has to be broken down into 2 tweens. One moving upward with an easing of easeIn and the 2nd one moving downward with an easing of easeOut. Try this example. Create a movieclip with instance "mc" and put this in frame 1.

import fl.transitions.Tween;

import fl.transitions.easing.*;

import fl.transitions.TweenEvent;

var nDuration:Number = 2;

var nStartX:Number = 100;

var nEndX:Number = 500;

var nLowY:Number = 350;

var nHighY:Number = 50;

var cTwX:Tween = new Tween(mc, "x", null, nStartX, nEndX, nDuration, true);

var cTwY:Tween = new Tween(mc, "y", Regular.easeOut, nLowY, nHighY, nDuration*0.5, true);

cTwY.addEventListener(TweenEvent.MOTION_FINISH, onUpwardMotionDone);

function onUpwardMotionDone(oEvent:TweenEvent):void

{

    cTwY.removeEventListener(TweenEvent.MOTION_FINISH, onUpwardMotionDone);

    cTwY = new Tween(mc, "y", Bounce.easeOut, nHighY, nLowY, nDuration, true);

    cTwY.addEventListener(TweenEvent.MOTION_FINISH, onDownwardMotionDone);

}

function onDownwardMotionDone(oEvent:TweenEvent):void

{

   cTwY.removeEventListener(TweenEvent.MOTION_FINISH, onDownwardMotionDone);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2010 Mar 14, 2010

I don't understand why you would ever make it that complicated. It's pretty easy to make it move in a parabolic way...

Make an enter frame event. Give the ball a variable for xVelocity and yVelocity.

In your enter frame event function, move the ball forward by xVelocity and up by yVelocity (which will at some point be negative). Then, subtract something from yVelocity - this basically makes it accelerate downward.

If you want it to bounce, just multiply yVelocity by -.8 or something whenever it hits the ground. Or multiply it by -1 and subtract a bit. Or something.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 14, 2010 Mar 14, 2010

It is just an example and i though it would be easier to understand that way. Ofcourse it can be made more dynamic and efficient if you take a more mathematical approach. Maybe not. Feel free to give your code examples if you think you can do it better than others.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 14, 2010 Mar 14, 2010

I don't think it's necessary to label one solution better than another--the purpose of these forums is to share ideas and solutions.  It'd be interesting  for folks to see how the two compare in terms of code and performance.    Harry's solution looks like an efficient use of code and it plays out nicely.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2010 Mar 15, 2010

Yeah, fair enough - maybe my solution seems simpler to me because I've actually studied a fair bit of physics, and I've never heard the words "motion tween" outside of Flash. I should probably learn more about tweening, though.

Meh, comes down to personal preference, I guess.

I do like including rotation, though... Although, I suppose a beer pong ball looks the same at all angles?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2010 Mar 15, 2010
LATEST

Plenty of folks around here likely studied a fair bit of physics, and going that route might seem an obvious choice for some.  But the OP's concern is in getting help with finding a solution, and physics may be the last thing on the list he/she has for new things to learn. So showing an example of what you would use may be more helpful than describing it in general terms.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 14, 2010 Mar 14, 2010

We may also do it this way if that is easier and understandable:

var nVelocity:Number = 40; //units/sec

var nPrjectionAngle:Number = 45;

var nAngle:Number = nPrjectionAngle*Math.PI/180; //in radians

var nGravity:Number = 9.8; //units/sec/sec

var nTime:Number = 30; //in frames perspective

var nX:Number = 250;

var nY:Number = 0;

mc.addEventListener(Event.ENTER_FRAME, onMotion);

function onMotion(oEvent:Event):void

{

   mc.x = nX - nVelocity*Math.cos(nAngle)*nTime;

   mc.y = nY - nVelocity*Math.sin(nAngle)*nTime + 0.5*nGravity*nTime*nTime;

   nTime--;

}

Just modify it to suit your needs... Please share your ideas forumers.. Sorry about the previous thread i did not mean to imply that it's about a better solution. Just share your ideas.

-----------

Edit: mc.x assignment does not look right... anyway just get the idea using physics equation d = V(init)*t - 0.5gt(squared)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines