ID:267020
 
i would like to have it so when an object bumps something i i want to make it bounce off and change angle depending on its original angle and its velocity. how will i go about doing this?
Soori-99 wrote:
i would like to have it so when an object bumps something i i want to make it bounce off and change angle depending on its original angle and its velocity. how will i go about doing this?

This was one of my early programming questions...one I never ended up researching properly.

There's a Java-based solution on this page that you could adopt:

http://www.cs.washington.edu/homes/csk/IntroJava/javanotes/ source/MovingBall.java

Another way to do it which is less precise (and which the upcoming L&D level happens to use) is to simply have balls moving in one of 8 directions (N, W, S, E, NW, NE, SW, SE), and when a ball hits something while moving in that direction, hardcode the direction to bounce in:

BounceDirection()
switch(dir)
if (NORTH) return SOUTH
if (NORTHEAST) return NORTHWEST
if (SOUTHWEST) return SOUTHEAST


Etc.

You could also introduce some randomness if you wanted:

BounceDirection()
switch(dir)
if (NORTH) return pick(NORTHWEST, NORTHEAST, SOUTH)


This less-precise approach wouldn't be what you'd want for a true pixel-based game, but for the movement granularity of BYOND, you might find that it works fine.
Soori-99 wrote:
i would like to have it so when an object bumps something i i want to make it bounce off and change angle depending on its original angle and its velocity. how will i go about doing this?

Well, I can tell you from experience that it's gonna be easier to do this with a ball than with an ellipse.

Collision algorithms tend to be quite difficult to implement, especially if you look out for the tough cases like when multiple objects collide, or what happens when one ends up on the inside of a wall. The math itself isn't so bad, though; the algorithm is what's the killer.

If you have only circles or spheres with no rotation, then the math turns out to be pretty simple and the collision calculations aren't so bad.

Lummox JR