Reputation: 10790
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
Upvotes: 0
Views: 996
Reputation: 10790
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 );
Upvotes: 0
Reputation: 7415
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.
Upvotes: 1
Reputation: 10790
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.
Upvotes: 0