Flash 3d Ball Moving

Started by
2 comments, last by driftingSpaceMan 12 years, 6 months ago
[font="Comic Sans MS"]Hello,[/font]

[font="Comic Sans MS"]Say, I have:[/font]
  1. [font="Comic Sans MS"]A 3d ball and[/font]
  2. [font="Comic Sans MS"]a 3d level.[/font]
[font="Comic Sans MS"]
My problem is: How to make my ball know when the level has an uphill so as for the ball to respond accordingly?[/font]
[font="Comic Sans MS"]Is it just a matter of collision detection between the ball and the level's ground?[/font]


[font="Comic Sans MS"]Thank you for your answers.[/font]
Advertisement
Well, this is assuming you don't want to use a physics library... (which you might consider if accurate physics is really important here, or relevant to more than a rolling ball)

But without a physics library:
If your ground is a height map, this should be pretty straight forward.

For these purposes, lets say X and Y are the ground plane, and Z is height (I prefer to do it that way)

You should be able to compare the planar X and Y coordinates of the ball to your height map (which is an image) and get the grey-scale value. Say, between 0 and 255. That directly converts to height based on whatever scaling you're using.

Then you just set the ball's height to the height of the height map to get it to follow the ground visually (with the necessary Z offset for wherever the model is relative to the file- at least center the ball so you don't offset the ball's X and Y, but if you can't, you need to offset the coordinates you use for the look-up instead of the ball).

For the physics aspect, I would recommend converting the height-map into a normal map. You can do that on the fly, but flash is kind of slow, so I'd say you should precompute it (somebody else may have a better way).

What that will do is give you a texture to look up for each respective world XY position that tells you the slope of the map at that point. Depending on the slope in each direction, you can then add to or subtract from the ball's X or Y velocity vectors.

With extreme slopes, this method will have the effect of creating a phantom acceleration that manifests along the vertical axis. You can compensate for this by reducing the magnitude of all velocity relative to the slope's normal by a factor scaling with the extreme degree of slope while the ball occupies that space... but, that probably will not be aesthetically necessary unless you have slopes of more than 20 or so degrees, and it should not affect the mechanics of your game.

Bear in mind, with this method, your ball will never catch air or bounce. If you need that, you'll have to use a more complicated model.

Note: I'm a science and art nerd, but I'm not very good at programming compared to the CS geniuses around here. I can tell you that should work, but I can't say what the performance will be- somebody else may tell you that this method will kill performance in flash due to all of the look-ups and that you should just use the graphics card to process slope on the spot from surrounding height data (being a gathering function).


Anyway, I hope that helps.
Cheers!
[font="Comic Sans MS"]Thanks a lot, driftingSpaceMan.

That will surely help. I had no idea how to do it. I will search for a physics library. [/font]
[font="Comic Sans MS"]However, if it becomes difficult to use one I will try with the height maps.

Once again, thank you very much.[/font]
No problem.

I would say, if you aren't familiar with integrating libraries, and *all* you need to do is have the rolling ball, then the heightmap method may be easier.

Let me know (PM) if you end up using this and if you have trouble correcting the phantom Z axis acceleration on steep inclines- I can give you some rough equations to start from.

Cheers!

This topic is closed to new replies.

Advertisement