How do I make my hitTestObject() more precise? - actionscript-3

I'm doing some collision detection with a circle and a square and whenever the circle comes in contact with the square it bounces away changing its X coordinate by *-1. However, the Hit Area of the circle is a square, so even when it collides with the white area around the circle , the affect still occurs.
My question is, is there a way to modify the hit area to closer resemble my circle?
Thanks

Bitmap hit testing is pixel based (instead of boundary-based, like Sprite-based hit testing), so it is inherently more precise.
Here are the Adobe docs on it.
Here is a nice tutorial on it.
And here is a nice code snippet on it:
if (firstObjectBitmapData.hitTest(new Point(firstObject.x, firstObject.y), 255, secondObjectBitmapData, new Point(secondObject.x, secondObject.y), 255))
{
trace("hit!");
}

Related

How to implement rotating rectangle around circle in libGDX Box2D?

I want to implement rotating rectangle around cicrle in such way, that circle has no rotation, and rectangle has. All object's are Box2D Body objects. Here is picture, what I want to have:
In my case rectangle touches circle, but I think it doesn't matter.
At first I tried to do it with two Fictures for same Body, but there was a problem with rotation: I couldn't have one ficture with rotation and another without.
I think, it should be somehow connected with joints, but I don't know what exactly Joint I should use. Maybe are there another solutions?
I think DistanceJointDef will do the tricks
you could put the radius if the circle as the distance with a little margin if you want
you also have to reduce the friction of bodies so the rectangle can move smoothly
DistanceJointDef djd = new DistanceJointDef();
djd.bodyA = bodyRactangle;
djd.bodyB = bodyCirlce;
djd.length = radius + margin;
world.createJoint(djd);
bodyRactangle is a dynamic body
bodyCirlce is a static body
try that for a start, hope it is helpful
Good luck !!

how to make walls in actionscript 3.0?

i've been making a twist on the labyrinth game and i've got my ball to move with physics but im struggling with getting it to hit the walls around it. its currently a movie clip with black walls, and ive used this code to try and stop it:
if (character.hitTestObject(walls)){
character.x = //something
character.y = //something
}
all this does is when it hits any part of the movie clip, (even the blank spaces) it moves my character,
is there any sort of code i can use to maybe detect hitting a certain colour?
One way you could do this, is to use hitTestPoint() method to test if any of the corners have hit your wall.
hitTestPoint() tests only a single location to see if that point collides with an object. This is how you could test the top left corner of your character to see if it's touching the wall :
// I am assuming that x,y is the top left corner of your character
if (wall.hitPointTest(character.x, character.y, true))
{
// top left collided with wall
{
So you could do the same for all corners, or if you want, you can determine any collision points you want to check for the character.
Depending on your level of precision, this method might work just fine for your needs. But if you want pixel perfect collision, you can check out this link :
http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/

As3 sprite rotationX and rotationY

I have a bit problem with rotationX and rotationY.
It's cool if i just do a roationX and rotaionY below
_eventParent.rotationY =_differentX;
_eventParent.rotationX =_differentY;
However once i have assign a mouse move to the _eventParent. The roationX and roationY change perspectively while the mouse is moving. so instead the item remain the same size. it increase and decrease size prospectively. any idea why is it doing this? is there a possibility to stop this behavior?
Thanks
Please find the image below.
Perspective allows part of your shape to look closer to you than other parts. The problem is that perspective has a center, or "vanishing point" and by default, it is fixed. As you move your shape farther away from the vanishing point, the perspective changes, causing your shape to widen or narrow.
You can fix this by updating the vanishing point so that it is always at the same coordinates as your shape. Since the shape will always be at the vanishing point, the perspective shouldn't change.
To do this, create a perspectiveProjection for your shape:
_eventParent.transform.perspectiveProjection = new PerspectiveProjection();
PerspectiveProjection is located in the flash.geom package, so don't forget to import it.
Then whenever you update your shape's position, update it's vanishing point:
_eventParent.transform.perspectiveProjection.projectionCenter =
new Point(_eventParent.x, _eventParent.y);
You might need to offset the vanishing point by a set number of pixels to get the perspective looking the way you want it to.
Correct me if I misunderstood your question. Your question is that if you apply rotation to the movieClip object, then why does the size appear to be changing?
For simplification, Let's not apply rotation on both X and Y axis. Let's take a rectangular movie clip and onMouseMove we do ++myMovieClip.rotationX;
Now, this statement is going to apply rotation on the object about the X-axis and one would get a perspective of the movie clip flipping across X -axis and this flipping will show as change in size of the object.
The same applies to rotating across y-axis.

Masking in ActionScript3

I am trying to understand masks in actionscript..Everything seems to make sense to me but one part of the code
function mouseM(event:MouseEvent):void {
if (mouseclick == 1) {
mask_mc.graphics.beginFill(0x000000);
mask_mc.graphics.drawEllipse(mouseX, mouseY, 70, 60);
mask_mc.graphics.endFill();
}
}
I am not sure how to exactly ask this question but here it goes. why does the mask have "begin fill" with a black color? wouldn't that paint the the image in black (I know it doesn't, it just reveals it)? what is the exact function of beginfill (besides revealing the image lool)? like how does it exactly work? sorry if it sounds ridiculously off.. but that part of the code was really screwing me up in understanding masks
What you are doing is drawing a shape to be used as a mask. In this case, a circle.
It doesn't matter what colour it is as Flash is only interested in the shape of the mask, not the colour.
Once the circle is drawn, Flash checks what part of the circle overlap the object you're masking so that every pixel the circle is not covering will be invisible. I guess it should really be called an anti-mask as the circle dictates which parts of your image wont be masked but it's just become the general convention to call the circle (or whatever shape you use) the mask.
Again, you're just creating a shape to be used as a mask. Setting the colour is just so the object can essentially exist.. because you can't exactly have a transparent circle.
Feel free to change the colour to anything and you'll see it makes no difference, the shape is all that matters.

How can I created a curved health bar?

I'm making a game with a health bar and I am trying to make a health bar that is curved.
Currently I have a lineBar that has 20 segments that looks like this at the bottom left of the screen.
What I'd like to do is write a function that goes through and modifies the scaleY of each to get a curved bar.
I can easily scale them down in a straight line. So that it looks triangle ish.
I want exponential decay.
In normal math terms it might be something like y = Pa^x.
I developed a game with a curved health bar a while back, this is how I achieved it:
Step 1:
Create your curved bar. I suggest the Oval Primitive tool:
Draw your bar. I suggest creating a guide layer to demonstrate a whole-circle visual of your curved segment. Copy the bar onto another layer and make it a mask, this will be what reveals your healthbar. The mask and the segment should be MovieClips:
Step 2:
Set the registration point of your mask to the centre of your guide circle. Your mask will rotate around this point to reveal your actual bar. Rotate your mask so that it is to the left of your actual bar graphic:
Step 3:
Create a tween of your mask rotating clockwise across 100 frames (add more frames for finer progression). You can even apply a tween to your bar graphics where the colour changes from red to green as it fills, etc.
Step 4:
Use gotoAndStop() on this element to determine which frame you should stop on throughout the animation. The formula I use here is generally:
gotoAndStop( Math.round( currentHealth / maxHealth * x ) );
Where x is the amount of frames you created.
Hope this helps.