Sunday, February 10, 2008

X+Y+Z = 3D

I’ve come up with a way of making the items that are thrown into the pond stop when they hit the surface of the water. The big problem with this project is that I’m trying to do a 3D physic in a 2D environment… let me TRY to explain…

Clicking the mouse activates the old lady to throw an item across the screen, this involves the X axis (horizontal, length) and the Y axis (vertical, height)… pretty simple… until I came to the stage where I wanted to land that thrown item onto a flat surface, the Z axis… X+Y+Z = 3D… my game is 2D with a side on view - DOH!

I could have gone back and re-designed the whole perspective of the game, maybe make it isometric. Instead I decided to put tiered platforms into the area where the thrown item travels through. I then applied a hitTest variable to these platforms so the thrown item can land on any platform it connects with.

Man I found that hard to explain, hope it made some sense. I’ve printed a screen shot to try and further explain my solution, please see below. Obviously the platforms won’t be visible in the game.


*** Addition *** as feared it seems my explanation wasn’t as crystal as I’d hoped. Adam’s comment prompted me to add the additional image below to expand on my explanation, hopefully it shows what I mean by the action that I was trying to achieve was a 3D one. No worries now though, platforms to the rescue!

9 comments:

Adam said...

Don't know why you thought it was 3D because of a platform being there lol

Hit test is so easy in actionscript 3.. guess there's one thing good about it.

No wait, two, you can do pixel-level hit tests in actionscript 3, which is awesome!

Take a look at this when you get a chance (by the way this guys site has amazing tutorials on making games in AS3)

http://www.8bitrocket.com/newsdisplay.aspx?newspage=6421

Gav Cooper said...

Adam, I knew someone wouldn’t get it, oh well… I’ve added an additional image to this post to try and illustrate what the 3D / 2D issues are (were).

I’ve solved the issue though, just had to take a different route to getting the job done! Been testing, the platforms work pretty well…

Adam said...

I think you're misunderstanding it, the water is not parallel to the Z axis, it's tangential to it; the water runs across both the x and y axis only as there is no third dimension in your game.

The platforms are rectangles which are also two dimensional objects - there is no third dimension, unless you rotate the whole scene so that the Z axis is NOT at a tangent to the screen (at the moment it is - and by definition is 2D)

Hope that helps clear it up!

Adam said...

That is, unless your platforms are meant to be at different depths, thereby creating a Z axis - are the platforms at the bottom meant to be closer to the screen and the ones at the top meant to be further away?

If so - why? There is no reason to have it that way as it will not change the appearance of your game at all!

Jools said...

think the additional diagram really useful. Looking forward to trying the game

Gav Cooper said...

Adam I am not trying to make a 3D game, it’s a 2D game and there lays my restriction…

yes Adam, your right the water does run across both the x and y axis, that’s the problem… as illustrated I needed the water to be parallel with the Z axis so it is a flat surface (as shown in the added diagram) and therefore can have items land upon it… but yeah your correct its 2D…

The physic that I was trying to achieve was a 3D one… throwing something… and for that thrown item to land it needs a FLAT surface for it to fall upon, the Z axis.

enter the platforms… these platforms aid the game so that whatever is thrown, now has a selection of surfaces to land on. The platforms enable me to achieve this EFFECT in a 2D world.

I really do HOPE that explains where I’m coming from because the lack of understanding is making me feel like I am going insane... or maybe it’s just the flu I got (AGAIN!)

Anonymous said...

Lol, I was thinking you were getting confused and thinking it was a 3d world!

For the effect you need, you pretty much just need to draw a path in actionscript and tell the object to follow the path - that way all you're doing in code is drawing a curved line each time that starts at her hand and finishes at the exact [x,y] spot you want it to. There is no need for the platforms at all!

I'll have a look for info on drawing a bezier curve in actionscript and get back to you with some (hopefully helpful) code/links.

It's far simpler than you're making it out to be, be careful not to over-think what you're doing as you'll unneccessarily make it more difficult for yourself!

Anonymous said...

Here's some code for drawing a curve.

package {
import flash.display.Sprite;
import flash.display.Shape;

public class Graphics_curveToExample1 extends Sprite
{
public function Graphics_curveToExample1():void
{
var pathForBread:Shape = new Shape();

// This line sets where the curve should start (set to the womans hand x,y)
pathForBread.graphics.moveTo(X, Y);

// This line draws a curve to the point targetX, targetY (set them to no's)
// and it curves through the point curvePointX/Y which will be the high
// point of your curve
pathForBread.graphics.curveTo(targetX, targetY, curvePointX, curvePointY);

// This line adds the curve as a child of the stage (which makes it display)
addChild(pathForBread);
}
}
}

obviously you may well not need the package/class stuff!

Then you just need some code to animate the bread object along the path, which should be easy to find on the net. Otherwise, ask me :)

Gav Cooper said...

indeed… i see what you mean about over complicating things.

the platforms aren’t suitable, I was thinking I could arrange them so they wouldn't hinder the gameplay. i did some testing of various positions for them and… BAH! back to the drawing board.