Simple physics needed!!!
Quote:Original post by fguihen
not to dampen your spirits or anything, but what your going to do, if youve never attempted it before will be very very difficult. i have just started playing with game programming, and collision detection was a nightmare, and im still not sure at all with it. you'l need a solid foundation in maths ( which im afraid im lacking) and without it, the going will be very very slow and difficult. on saying that, i wish you the best of luck. if your determined enough youl be just fine. best of luck..
Collision can be as complicated as you want it. BUt most games I would
say don't need it to be. I would say use very simplie collision so
you can get a feel for your game and how much accuracy you need. It will also
tell you if the game is worth improving collision. Start out with
rectangular solid around object. Then go to bounding spheres. I
believe directx helper functions will create a bounding sphere for you and maybe even detect if two spheres collide. What I need is an equation
based on time x,y,z = F(t) that simulates motion, gravity. This
equation has been developed many times but I can't find it. ANy help
would be appreciatted.
Movement
Ok, to move the ball through 3D space, you should have two sets of vectors (X, Y, Z) - one for the ball's speed in each dimension, and one for the ball's current position in the room.
Basically, every time you move the ball, you add the velocity vector to the speed vector. For example:
Position Vector: X = 10
Y = 10
Z = 10
Velocity Vector: X = 5
Y = -1
Z = 3
When you add the velocity vector to the position vector you get a new position vector of X = 15, Y = 9 and Z = 13. This is the new position of the ball. You then do this every time you move the ball in your game loop.
Reflection Off Walls
This is very simple, you simply times the velocity vector for the component that causes it to hit the wall by -1. For example, if the ball is hurtling towards a wall (for this example, the wall lies across the Z axis), when it strikes, the ball keeps its speeds in the X and Y axies the same, but in the Z axis it become negative, so, when its velocity vector is X = 5, Y = 10 and Z = 3, after it hits the wall it would become X = 5, Y = 10 and Z = -3.
A more complicated version of this takes into account the coefficient of restitution between the ball and the wall to reduce the magnitude of the velocity in the Z axis, but this is beyond me knowledge for a couple more weeks (before we cover it in Applied Maths).
Friction
For a kids game friction from the air does not need to have an effect.
Gravity
Gravity is incredibly easy to implement, put simply, on Earth objects in freefall acclerate at 9.8 ms^2. To simulate this in the game, decrease the ball's Y velocity by 9.8 every second, this will cause it to slow down as it goes higher and to accelerate to the floor.
Good luck!
Read my article on billiard physics. All the code for a ball bouncing around is there. Just allow the ball to move more in 3D than the 2D example. Collision of one ball against an object or wall is pretty easy.
Good luck.
-Jeff
www.darwin3d.com/gdm1999.htm#gdm0999
Code is GL but math works on anything :)
for the physics itself, a basic euler integration (force->acceleration->velocity->position) will be sufficient.
this is pretty basic stuff to the point of sounding awfully pedantic, and would probably take 2 hour to code, but a few more hours to explain [grin].
You do need a bit of vector maths knowledge to make things work simply and efficiently. Nothing fancy but vector operators and dot products.
Check for distance between two ball centers and if its less than the sum of their radii , you've got a collision.
Now, You do the same thing for each ball so lets just look at what happens to ball A:
find a vector from the center of A to center of B :
Vec = PositionB - PositionA
Normalize it :
Vec = Vec / Magnitude(Vec).
Do the dot product between Velocity vector of A and your new normalized vector
Scale = DotProduct(Vec,VelocityA);
Multiply the normalized vector vec with your dotproduct result
Vec=Vec * Scale
Substract Vec from Velocity of A to get new velocity.
NewVelocityA = VelocityA - Vec
Do the same thing for Ball B and voila, you have basic sphere sphere perfect elastic collision.
aonther demo... I simplified it a bit to strip out the stuff that is not so useful.
basically, a bunch of balls and cubes bouncing around.
methods... Solid walls are very large unmovable cubes.
One of the cube is controlled with the keys w, s, a, d. Hold mouse button 1 to look around and move the ontrolled cube.
technology :
------------
- simple euler integrator (acceleration -> velocity -> position).
- basic overlap detection between cubes and spheres. calculate the MTD (minimal translational distance) or either contact points as a tool for the collision response.
- use the MTD vector (or the vector betweeen two contact points, which is the same) to reflect the object's velocity as an approximation of the collision impact.
- supports variable amounts of friction and elasticity.
- support arbitrary mass, so the lighter object gets more of the collision response (it does still obey the conservation of momentum laws).
all at the top of gamecode.cpp
Those rubber dodgeballs deform a lot on contact. How about mass-preserving deformable soft bodies instead of simple spheres? Highly connected mass spring system will get you close or a finite element mesh if you want to go for extra credit.
With that you can do some ductile deformation for free a la the "Incredibles" stickyball thing. Now that would be fun.
all this but I like the first reply with simple equations. For all
you folks looking for an idea for a game you should try dodgeball.
I find 1st person shooter games get a little boring but if you
have to dodge 3 different balls coming from different angles and
speed you have a choice to make (go left, go right, jump, duck,
lean right, lean left. I will put a challenge to all you physics
guru-s to come up with the best game.