11 Replies Latest reply on Dec 2, 2010 8:18 AM by kglad

[as2] Finding the minimum y point in a complex shape in a given x coordinate

Hello.

I´d like to know if it´s possible to find a minimum y point (where the shape ends) of a shape, in a given x coordinate.

Example:

I have a complex shape like a big "S". Suppose the "S" minimum x coordinate is 0. How can I find the minimum y point of the "S" (again, where the shape ends) in the x coordinate of 14?

Here is the image:

I need to find the y coordinate (or point) of the blue point in the image.

Thanks

• 1. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

You could probably get the bounding box of the S. Then use the BitMapData class to take a 1 pixel wide copy of the item at the desired x. From there you can use the getColorBoundsRect() method to find the bounds of the filled pixels in that 1 pixel-wide slide.

It gets complicated if the shape is rotated and all--I think. But it should be doable.

There are a lot of questions that could make a difference in the way this is approached.

What kind of shape is it? does it have transparency? Are the colors just black and white/transparent? Are there lots of these things that you need to do?

What is the purpose? What are you actually trying to do?

• 2. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

It will be used in a game.

The player should walk in a irregular path.

I don´t want to make a lot of movieclips while the path.

The shape is a path where the player should walk through.

The path doesn´t rotate.

No right now it´s just a prototype.

But I think the path will be like a ground,

with lots of kind of greens.

There will be some platforms and a ground.

Just like super mario bros, but with irregular grounds

and platforms with irregular shapes for the player to

walk through.

• 3. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

if the shape is not rotated and has reg point 0,0 in its parent movieclip, you can use:

function f(s:MovieClip,x:Number):Number{
var bmpd:BitmapData = new BitmapData(s._width,s._height,true,0x00000000);
var clipRect:Rectangle = new Rectangle(x,0,1,s._height);
var mat:Matrix = s.transform.matrix;
mat.tx = 0;
mat.ty = 0;
bmpd.draw(s,mat,null,null,clipRect);
var r:Rectangle = bmpd.getColorBoundsRect(0xFF000000,0x00000000,false);
return r.y+s._y;
}

• 4. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

I think so far so good.

But, now... the player has registration point at top left, and he will climb a little.

So, his new x will be his _x minus (or plus denpending if it is left or right and depending of the ground) his speedX.

How could I do it?

The player initials coordinates isn´t 0,0, but, it´s the coordinate obtained when he hitted the ground, in a given (obtained) _x and _y coordinate.

So, the player, will climb a little the ground.

The shape you mean, is the ground right?

No, the ground doesn´t have 0,0 coordinates it was just draw in the stage. It can contain any _x and _y position.

And, of course, is a movieclip, named ground.

The "S", was just an example.

The thing is, it´s a player that sometimes need to climb the ground.

The ground is irregular, it can have any type of shape. The problem is climbing.

• 5. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

if the ground is the shape you're interested in, convert it to a movieclip and make sure it has top-left reg point.

• 6. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

Yes the "ground" movieclip is top-left registration point.

But, the code isn´t working.

The player movieclip disappear.

kglad, can I send the fla to you?

• 7. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

if ground_mc is the instance name of your ground movieclip and you wanted to know the least y-value corresponding to x=112, you would use:

var yVar:Number = f(ground_mc,112);

• 8. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

yes, but, the matrix values shouldn´t change?

• 9. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

you don't need to edit any code in f() as long as the ground is not rotated and has upper left reg point.

• 10. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

I´m calling the "f" function like this:

p._y = f(ground, p._x-=3)

where "p" is the player movieclip.

But, the player ("p") gets a higher y position, than the desired y position.

• 11. Re: [as2] Finding the minimum y point in a complex shape in a given x coordinate

use the trace() to debug.