Collision Detection with ActionScript 3.0 - actionscript-3

Hey I am beginner Flash Action Script 3 developer.
I am using hitTestPoint() to detect collision between a car and a stage drawing. Car is moving in the stage so I am using hitTestPoint().
There is a problem, Lets say.
Car is a square, it is actually a perfect square right now.
I am doing this:
heightHalf = car.height / 2;
widthHalf = car.width / 2;
if(level.hitTestPoint(car.x + widthHalf, car.y + heightHalf,true)){
trace( "Right Collision" );
}
It should work as, car.x + the half of its with should return the point on x-axis which is colliding and same with the y-axis. But its not working.
When my car collides with the right walls it doesn't produce error or trace, but If I move my car further out of stage(as car can go through walls) just before it can completely move out, it produces trace error just when left side is colliding with walls.
These pics should help:
Right Collision with no error: http://i.minus.com/ibqvrbNHuLTTIX.png
Error but with wrong side: http://i.minus.com/iGRNRVmCwwY4x.png
Inverting the + - signs isn't helping either.

Take notice to where the anchor point is on the car object. since you are using Flash IDE, The anchor point could be in the middle, right, or left corner.
Also you will need multiple points do to this type of hitTesting. at least one for each side of car.
If your registration point is top left, then you are hitTesting the middle point of your car. So it will only register collision when half of the car goes over the wall.
Check your registration point. When you create a new movieClip or Sprite you can select the registration point by clicking one of the 9 box on the square that comes up under the name of the Object.

Related

cocos2d-x Actor does not move to touch location

I am trying to make an actor follow the player's finger (long touch). I'm positive I have the math right, but the actor fails to move exactly to where the player touched.
Here is an illustration of my problem:
When the touch is near the top, the actor goes beyond the visible scene at the top.
When the touch is near the bottom, the actor goes out of the visible scene at the bottom.
Same goes for the left and right.
When the touch is performed in the middle of the scene the actor moves perfectly to the touch. In short, the further the touch is away from the middle the more pronounced the distance between the actor and the touch is. In other words; the closer the touch is to the middle, the closer the actor moves towards the touch.
Please note that when the touch was near the bottom or the top the distance between the touch and the actor was more pronounced then when the touch was on the right or the left; as the top/bottom are further from the mid point.
Here is the code used to follow the actor towards the touch:
Lang: Lua
Lib: Cocosd2-x 3.1
local velocity = 1.4
local x, y = self.sprite:getPosition()
-- self.dest[X/Y] are cached coordinates to where the actor should move next.
local angle = math.atan2(touch.y - y, touch.x - x)
local deltaX = velocity * math.cos(angle)
local deltaY = velocity * math.sin(angle)
local newX = x + deltaX
local newY = y + deltaY
self.sprite:setPositionX(newX)
self.sprite:setPositionY(newY)
Things I've tried:
Changed the scale of background layer and sprites. No change
Changed the algorithm used to compute the angle. No change.
Created a red dot and set its position to the exact touch x/y to determine if there was some weird transformation issue when determining the actor's point. The red dot was always perfectly under the touch.
Discovered the issue. When I created the Actor sprite I set its z-index to 100. When I uncommented out the call that set the z-index, everything worked perfectly. In my situation, this particular sprite must always be above all other sprites. What I did to fix the issue is set the z-index much lower than what I had originally set it to; which ended up being 15.
sprite:setPositionZ(15)
From my observation it appears that the sprite is having some type of scale applied to its position the larger the z-index is of the sprite.
Update 1
Using :setPositionZ(int) will unnecessarily scale your sprite bigger in some cases. I now use :setGlobalZOrder(int) with much better success:
sprite:setGlobalZOrder(15)

Does the registartion point change when I rotate movie clip AS3

I have a movie clip as you can see from the following link : http://prntscr.com/2pta2j .The registration point of it is in A, i.e. + symbol. I want to know where will be the registration point after I rotate it 90 degree . Final view of it will be like : http://prntscr.com/2ptans
The registration point stays next to A.
EDIT
One exception being, if the registration point next to A is the registration point of a container that the square is in and you rotate the square within the container, not the container.

I am trying to make 2 circles around a point and fill between these points and listen for a click between the circles

i'm trying to make something for a game i'm making. When someone clicks on the movieclip i want it to draw an inner circle and an outer circle. I'd like to fill between the circles with a opaque colour (purple in the image) so people can see basically a large thick circle around the movieclip but not touching the movieclip. I then need to check if the mouseclick happens between the two circles only.
The image below shows what i mean. The thing is the thickness of the purple bit has to be adjustable (not in game as such), if you click on 1 movieclip the thickness of the purple bit may be 10pixels, a different clip may be 50. Obviously checking for a click greater than inner circle x and less than outer circle only works on a straight line across from the clip, once you move up or down this doesn't work so well. Any help is much appreciated as i can't seem to work this out. I've tried drawing 2 circles and also tried using 2 movieclip circles but can't get it to work.
Seems i cant upload pictures on here. Easiest way is to think of a no entry sign without the / line going through the middle. The centre is the movieclip, the inside part of the red circle the inner circle and the outer is the outer circle but at no point does the red touch the movieclip
I would measure the distance from the center of the circles to the mouse click point. Then you just need to check is that distance greater than the inner circle radius and less than the outer circle radius.
Something along these lines:
var clickPoint:Point = new Point(mouseX, mouseY);
var centerPoint:Point = new Point(circleMC.x circleMC.y);
var dist:Number = Point.distance(clickPoint, centerPoint);
if(dist > innerRadius && dist < outerRadius){
trace("the click happened between circles
}

How to Create Boundaries in AS3

I am new to AS3 and I wanted to know how to make a frame Boundary in flash, I am making a platformer game. My code Logic is like :
Whenever I move with ArrowKeys Move Background invert, so if I click Left key the background will move Left. so this is not really what I want as my Player (moving avatar) will always be stick to the middle of the stage. How to make a New boundary so whenever Player go near Right end of Stage ... Background should go left as much x moved outside boundary of player.
more illustration :
http://i.imgur.com/HOw6vHI.png
if(player.x > rightBound){
map.x += rightBound - player.x;
player.x = rightBound;
}
Repeat for the other 3 sides.

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/