anglur bouncing for 2d phisics/collision

Started by
3 comments, last by onfu 21 years, 8 months ago
Greets. I need some help with any kind of method for detecting weather an angular surface was hit, what the angle of the surface is, and what x/y velocity the projectile/entity should receive accordingly. I''ve made my phisics/gravity engine in director, which is very similar to java - but it''s probably best if i just explain in pseudo how my system works. ball is on stage. phisics is applied to ball''s (y) velocity decreasing it incrementaly each frame until it reaches -20. (when it bounces this number becomes an appropriate posotive number and thus the (y) value sends it upwards and not downwards) collision checks: the ball is only allowed to move if the collision check returns false, and it bounces otherwise. the collisions are checked using a black and white bitmap image where black pixels are impassable. when applying the (x) motion if (x) = 1 (meaning it will move 1 pixel to the rigt each frame) it checks that ball(x + 1, y) <> black before it''s allowed. if that check does find a black pixel though the result is simply ball(x,y) = ball(-x,y). the problem is that my system only works properly for sqaured off surfaces. if a ball coming in at a 45 degree angle hits a 45 degree surface it should be sent in the exact reverse direction, but if a ball comes in from straight abouve and hits a 45 degree angle it should get sent sideways. so, can anyone suggest what methods/systems i can use to check the angle of the surface that was hit? it isn''t possibly to do it with vectors in any way that i can think of because of the collision system being based around a single raster mask. any help would very much appreciated.
Advertisement
I suggest trying to approximate the surface normal with the slope of the pixels at that point. You could look for pixels in the 5x5 grid around the impact point, and find a decent approximation.

Cédric
that may cause problems though if within that 5x5 grid there''s something more complex than a single surface, or if in the middle of the grid (impact point) is the peak of an uneven pyramid, the angle calculation would be wrong wouldn''t it?
*God* What was I thinking?

It''s past midnight over here, but I am pretty sure that the normal force applied on a sphere/circle is always directly aligned on the center, starting from the impact point.

So it''s even simpler than before. For most cases, what I would do is average the x and y coordinates of all the pixels which fall ''inside'' the ball. Ideally, you want as few pixels as possible, so you might want to subdivide your movement into smaller time intervals. Then, the normal of the surface is simply C - P, where P is the averaged point of impact and C is the center of the sphere. This won''t be perfect because of the discrete nature of the pixels, but it should look pretty good.

The bounce should always be applied in the direction of the normal. Ie.: finalspeed = initialspeed + k * normalvector

HTH,

Cédric
thanks for your help, i''ll give that a try...

all i meant before was that if it impacts at a complex/messy point that the calculated average would possibly be innacurate (as far as i knew, but i was asking to make sure)...

the movement system allready only moves an entity one pixel at a time, as the collision system wouldn''t be effective otherwise.

if the ball''s virticle motion is 5, the movement part of my code moves it one pixel downwards at each stage of a 5 part loop, checking for a collision at each stage of the loop.

anyway, thanks again...pretty sure i understand the theory. just need to try and implement it.

This topic is closed to new replies.

Advertisement