views:

249

answers:

3

I am creating a ping pong game. And I want to create the ability to control the direction of the ball based on the impact on the paddle. If the ball is coming down at vy = 4; vx = 4;

and the paddle is moving to the left at vx = -5; I want the ball to slightly change its course depending on how fast the paddle is moving. It would probably reduce the balls vx speed on collision, therefore making the ball move more straight (close to the Y axis) when it is moving back up. But before I go on a crazy trial and error journey, I was wanting to know if anyone knew the answer or probably have any sources.

I figure the solution for probably doing this would be to measure how fast the paddle is going. My problem is the paddle is controlled by the mouse and has no certain speed. I am trying to figure out how I can measure the speed of my mouse traveling on the x axis.

I am probably going to create a timer that fires every few seconds to determine where the mouse was and where it is at. figure the difference and that will be the speed

If anyone has any answers, that would be great. thanks

+1  A: 

The easiest way to measure the speed of the paddle is to keep a cache of the position of the paddle in the previous frame, thus by having x - xPrev you have the delta of the paddle movement for this frame, or the relative speed.

From that speed you can add it as a modifier(scaled down probably), into the x velocity of the ball's reflected vector.

Now this sounds like a simple game, and framerate should not be a problem. For reference however, if you want to keep track of the velocity, simple Physics calculations could be made by having access to the total time of the previous frame. allowing for consistent behavior time-wise, independent of the framerate of the simulation.

Ramon Zarazua
If that happens to be too twitchy, you can record the x values for several previous frames and average/filter them to give a smoother effect.
CookieOfFortune
I will have to look into that. yea I figure I would set a Timer that would fire an event so many x miliseconds to record the distance. I curios to know if there is any catchy formulas out there. if not, I will probably take the speed of my mouse and just substract a little how the vx of the ball.
numerical25
To expand on what CookieOfFortune said, a digital filter may be the simplest way to smooth it. Here's a simple low-pass filter: v = f * input + (1 - f) * v. f is between 0 and 1. The greater the value of f, the less filtering.
Wayne Conrad
A: 

K, I figure the solution out. Still need to work on my collision detection though. isnt the greatest. my ball sometimes get stuck in the paddle. I am using the flash object hit test. but anyhow. this is the code I used to get it to somewhat do I want it to do. It seems as though it supports the direction it suppose to go sometimes and sometimes it doesnt.

It works but the problem is I knock my speed in the negative so sometimes the directions are off. since the direction the ball moves is determined by a -1 or a 1. if I knock my speed in the negative. it can cause a number to be the opposite of what its suppose to be.

Below is in the paddle class

public var cspeed:Number;
        private var timer:Timer;
        public var vx:Number = 0;
        public var vy:Number = 0;
        private var prevx:Number = 0;
        private var prevy:Number = 0;

        public function Paddle():void
        {
            timer = new Timer(60);
            timer.start();
            timer.addEventListener(TimerEvent.TIMER,checkSpeed);
        }


        private function checkSpeed(e:TimerEvent):void
        {
            if(prevx == 0)
            {
                prevx = x;
            }
            else
            {
                cspeed = x - prevx;
                prevx = 0;
            }
        }

I check for collision within my ball class. since it is the one hitting everything. below is the code

the following is in my ball class. in the check walls class it checks to see if it hits walls or the paddle. I passed reference of my paddle to the ball class.

private function checkWalls():void
        {

            if(y > sRef.stageHeight || y < 0)
            {
                yDir = yDir * -1;
            }
            else if ( x > sRef.stageWidth || x < 0)
            {
                xDir = xDir * -1;
            }
             //trace(dist);
            /*var dx:Number = paddle.x - x;
            var dy:Number = paddle.y - y;
            var dist:Number = Math.sqrt(dx * dx + dy * dy);*/


             if(hitTestObject(paddle))
             {
                 xspeed = (paddle.cspeed/100) + xspeed;
                 yDir = yDir * -1;
             }
            //trace(paddle.cspeed);
        }

        private function moveBall():void
        {

            x += xspeed * xDir;
            y += yspeed * yDir;
        }

I still needs to be twinked ALOT. but if I work on it. i think i can get it to do what i want.

numerical25
A simple solution is, when detecting a hit, revert back to the previous position (when it wasn't hitting yet) after changing the properties you need (velocities, direction, etc...), and then move the ball.
Cay
I somewhat get what you are saying. can you put that in psuedo ??
numerical25
A: 

I was able to some what resolve the issue. I found on the internet to do the following and the physics look some what better. ball still gets stuck within the paddle from time to time

Changed the following from

xspeed = xspeed + (paddle.cspeed/10);

to

xspeed = xspeed + ( paddle.cspeed * .4 );
numerical25