Ball bouncing

Blitz3D Forums/Blitz3D Programming/Ball bouncing

jad(Posted 2007) [#1]
I need to make a ball bounce accurately off a wall that is inclined at some angle. If anyone knows of an equation that would work for this, that would be awesome.


bytecode77(Posted 2007) [#2]
you need to get the vector normals of the point, the ball hits the wall. then combine this vector with the vector of the ball and flip it. the result will be the "bounce-vector".

ps: are you the guy who asked this in the devil-engines-forum?


t3K|Mac(Posted 2007) [#3]
i'd say use a physics engine. no need to fiddle around with mathematics ;)


bytecode77(Posted 2007) [#4]
you mean like that one?


Stevie G(Posted 2007) [#5]

you need to get the vector normals of the point, the ball hits the wall. then combine this vector with the vector of the ball and flip it. the result will be the "bounce-vector".



This is not physically correct. You need to use get the reflection vector. It's quite straightforward.

Const Restitution# = 1.0 ;elastic collision

;Nx, Ny & Nz are the normals of the surface collided with
;Vx, Vy & Vz is the balls current velocity vector

; Project velocity vector onto the collision normal vector
VdotN# = Vx * Nx + Vy * Ny + Vz * Nz
	
; Calculate the normal force. 
NFx# = -( 1.0 + Restitution ) * Nx * VdotN# 
NFy# = -( 1.0 + Restitution ) * Ny * VdotN# 
NFz# = -( 1.0 + Restitution ) * Nz * VdotN# 
	
; Add the normal force to the motion vector to get the new velocity. 
Vx = Vx + Nfx
Vy = Vy + Nfy
Vz = Vz + Nfz


Stevie


t3K|Mac(Posted 2007) [#6]
i'd still go for a physics engine - you'll have more features for your game (gravity, friction, ...)


caff_(Posted 2007) [#7]
i'd still go for a physics engine - you'll have more features for your game (gravity, friction, ...)

And sometimes unpredictable effects. And for some engines you don't need gravity, as I found out when I recently coded a 3D breakout game.


Stevie G(Posted 2007) [#8]
Gravity and friction are easy to model yourself - if you just have a ball which moves about a level and doesn't do anything fancy, using a complete physics engine is overkill.

Stevie


t3K|Mac(Posted 2007) [#9]
"Gravity and friction are easy to model yourself" - if you know the mathematics involved. some guys don't know this (me included).


IPete2(Posted 2007) [#10]
Stevie,

thanks for the code... I'm now laughing my socks off!!!

IPete2.


Stevie G(Posted 2007) [#11]

thanks for the code... I'm now laughing my socks off!!!



NP. Laughing your socks off?


IPete2(Posted 2007) [#12]
Stevie,

I am currently working on a medium sized, commercial project, I need to make my character to bounce around like a pinball, to that end I have been trying to decide on which system to use, ODE, Newton, Aegis Physx, it has been difficult to get any control at the level I need it with physics libraries, so when I came across your code a few weeks ago and saw the comment about physics engines being overkill, I stored the code and came back to it today, deciding to give your code a try.

Ha ha ha - fantastic! I had it working in a short time and couldn't believe how few lines of code I would need to achieve what I want.

So, yes, now I'm laughing my socks off, at my character bouncing around how I want it, and now I can continue to develop my project with a little smile on my face. It looks so impressive yet the code is so few liens.

Thanks a lot Stevie G.

IPete2.


Eviltoes(Posted 2007) [#13]
I think making your own physics would be more fun than using an engine, more thinking involved, I think its fun to figure these things out on your own.