Trajectory of a tennis ball, when been hit

protek

"Takai desu ne." -"Jinsei da."
Joined
Feb 17, 2008
Posts
4,001
Country
Finland
Region
Oulu, Pohjois-Pohjanmaa
Here's one for you physics and maths wizs.

I'm programming the old Nintendo Game&Watch Snoopy Tennis on XNA. Instead of moving the balls in steps, I've found a formula that gives a natural looking smooth trajectory. The problem however is that I can't get the ball behave correctly, when Snoopy hits it.

These two lines define the initial trajectory, when Charlie passes.
Code:
MovingPosition.Y = (float)(StartPosY + Velocity * Math.Sin(Angle[Trajectory]) * time - Acceleration * time * time / 2);
            
            MovingPosition.X = (float)(StartPosX + Velocity * Math.Cos(Angle[Trajectory]) * time);

The lines are run for each ball instance's Update method. MovingPosition is the ball's current position. StartPosY and X are the starting positions. Velocity is a double constant of 88. Angle is a single dimension table and it is randomly selected from three first entries in the table, giving three different trajectories. Acceleration is a float set to -9.8 to represent gravity.

So Charlie basically passes all balls nicely to the three points, where Snoopy is supposed to hit them. I already have collision detection implemented, so the hit is detected. Problem is I don't know how to make it bounce from the racket.

If I just simply change the trajectory value or startpos values, the ball seems to teleport somewhere else, when it's been hit. I basically needed to get the ball bounce from the racket and head towards the platform over Charlie, where Lucy might hit it back.

Any ideas?

Below is a pic of the Nintendo version:
GameWatchSnoopyTennis.jpg
 
I really wish I could help but I don't know how to do what you're asking. :( I love all the old Game & Watch games.

Heather
 
You've got your ball moving with an x component and a y component, and you want to basically bounce it off the racquet. You're modelling an elastic collision between a point (1d) and a line (2d)

To do this, calculate the normal to the racquet face, then you'll have to sum the vectors of the racquet face and the ball to get the direction that you'll bounce off in...

That might be utterly ridiculous, but it's where I'd start.

You probably want to reset time, and also play with some scaling factor for power.
 
Another option might be to use the FlatRedBall engine. It's not bad at simplifying this kind of task as it has prebuilt classes and methods for handling collision rebound, and for working with paths and gravity.

And, actually, that might be one way to work around it. These types of games only have a finite number of paths through which an object will travel. So if you can implement a path system, it will solve your problem.

It may be that you are overcomplicating the solution by simplifying it.;)
 
Thanks for your answers, guys! :)

@chiark: Had to go to Wikipedia and freshen up on vectors and normals. Having had a look I remembered, why I flunked vectors in school. :D

The racket doesn't actually have a velocity as the hitting is just an animation, which, opposed to the ball, is true to the original with three racket positions. Anyway, creating an imaginary X and Y vector for the racket and summing that up with the X and Y vector of the ball just might work.

@TickledPink: Thanks for the tip about the FlatRedBall engine! It seems to have its own IDE but I wonder if they could be used as class libraries in XNA. I also have to look into the path system in case I flunk with maths again. :p
 
@protek
From a physics point of view, it's more or less what chiark said. Then you can use the dot product of the velocity vector and the normal to the racket to calculate the bounce-off angle (and thus the new x-axis and y-axis projections for the new velocity vector) and initiate a new trajectory using the same formulas (straight steady motion for the x-axis and smoothly accelerating motion for the y-axis) slightly modified for right-to-left movement.

But as already commented, this is overkill considering you won't implement any raquet direction or force or anything physics-like. So all you need is to save the x and y coordinates at the time of collision and use 3 new predefined angles, one for each bouncing point, which you will calibrate until you get the desired bounce-trajectory for each bounce-point. Thus you reuse your original trajectory equations:

time must be reset to 0
StartPosX = the saved x coordinate
StartPosY = the saved y coordinate
+ becomes a - in the MovingPosition.X function for right-to-left movement (implement a sign-change variable in the function itself).

This way the game will work in its intended predefined-path way and the only use of these physics equations is to provide for smooth ball movement.
 
Wish I could help but it's out of my league.

I do remember having the 2 screen orange Donkey Kong Game & Watch when it first came out. I drove my parents crazy with the beeps on our vacations :)

If only I had known what an important part of portable gaming they were :(
 
@BLTCON0:
Thanks! I'll look into it. I'll also try to get a screenshot up.

@BritishGeek:
You know, if you have a DS, you can get Donkey Kong, Oil Panic and Green House on Nintendo Game&Watch Collection cart. These mimic the original games exactly.
 
You know, if you have a DS, you can get Donkey Kong, Oil Panic and Green House on Nintendo Game&Watch Collection cart. These mimic the original games exactly.
I totally agree. I've got this cart and it is excellent. (y)

Heather
 
Thanks for your answers, guys! :)
@TickledPink: Thanks for the tip about the FlatRedBall engine! It seems to have its own IDE but I wonder if they could be used as class libraries in XNA. I also have to look into the path system in case I flunk with maths again. :p
The IDE (Glue) is a fairly recent development. Personally, I never use it. I find that, although it's handy for the initial setting up of screens and entities, it just gets in the way for everything else.

So yes, you can use FRB without it. Just use the ProjectBuilder application that comes with it, which will set up your Visual Studio project files for you. Then you can just code away. It is pretty easy to get into as well.

You have to bear in mind that it's a 2.5D engine and work your head around that (coordinate system is different from normal 2D) but one major advantage is that it will adjust image sizes automatically whenever you change resolution. There is no per-pixel collision, though, and no easy way to implement it but for your game, its bounding box collision should be more than adequate.
 
Just do a calculation for each path from one side, store them in a table and reverse the index for the return. It IS a 'prerendered' game and watch after all :)


Sent from my iPhone using Tapatalk
 
time must be reset to 0
StartPosX = the saved x coordinate
StartPosY = the saved y coordinate
+ becomes a - in the MovingPosition.X function for right-to-left movement (implement a sign-change variable in the function itself).

Thanks! That did the trick. (y)

I had to remove the time multiplier in MovingPosition.X function to prevent the rebound balls from actually picking up speed.

It's really nice to see the balls behave the way I want them to after such a long time. Next thing is to determine suitable rebound trajectories based on Snoopy's position.
 
if you got the space go for predetermined behaver other wise its calculate as you go but then I spose you know that, you know the cia tried ban home micros in the class room cos it made finding firing solutions to easy ie that terectrorie
 
Since you all have been so helpful in solving the trajectory issue, I thought it would be only fair to share a WIP picture of the game. Enjoy!
 

Attachments

  • snoopytennis.png
    snoopytennis.png
    15.9 KB · Views: 1
Back
Top Bottom