Actionscript 2 - hitTesting multiple instances - actionscript

I'm making a platformer using Actionscript 2. I got the jumping and gravity and everything going, but I'm limited to one "ground". As one might imagine, platformers require multiple "grounds" for jumping onto and falling off of. I tried editing the "ground" movie clip so that it was several blocks with nothing between them, but when I try this, my character will jump, land at the peak of its jump, and repeat the process until it lands on the top of the ground movie clip, then behave regularly, jumping and landing on top of the ground movieclip, even if there's no part of the ground movieclip to land on.
Here's my ground-gravity code.
if(this.hitTest(_root.ground))
{
this._y -= gravity;
gravity = 0;
jumping = false;
}

When you use hitTest between two MovieClips, it doesn't matter if there's nothing inside the movieclip, as long as the hittesting movieclip is INSIDE the bounding box (the blue outer rectangle of a movieclip when you select it) of the other movieclip.
To solve your problem, but still have many grounds in the same MovieClip, you have to use the second usage of hitTest, hitTest(x, y, shapeFlag) -- this will not check if two movieclips are hittesting each other, but rather if a MovieClip is touching a POINT (just an x and a y coordinate). But the real advantage of using this is the third parameter, shapeFlag, which is a Boolean value (either true or false) -- if it's false, then the hitTest will take the whole movieclip into account, meaning that even if there are empty spaces, those will be included in the hitTest, WHILE, if it's true, then only the actual shape of the MovieClip will be considered during the hitTest.
First of all, you have to get inside your Character's MovieClip and move him so that the registration point of the movieclip (you know that small plus sign), is at the BOTTOM of your character. Then, change your code to this:
if(_root.ground.hitTest(this._x, this._y, true))
{
this._y -= gravity;
gravity = 0;
jumping = false;
}
This will check if the SHAPE of the ground MovieClip (meaning only the platforms, NOT the empty spaces between them) are touching the registration point of your MovieClip, which is located at the bottom, at the feet, of your character. So, whenever the feet of your character are touching any of the platforms inside the ground movieclip, the code will be executed and your character wil stop.
Hope this helps :)

Related

GKGoal Avoid Boundary Obstacles

I am seeming to have difficulty keeping my sprite nodes inside a map boundary that I have set up in the following way:
I have an SKNode *enemy that moves around an SKScene by goals and behaviors courtesy of GameplayKit from iOS 9. Currently the node wanders, and avoids obstacles that are defined GKPolygonObstacle objects. I have my bitmask set up so that any obstacle is deemd category wall, which the node is told to collide with (AKA disallow passing through).
In my didBeginContact:(SKPhysicsContact *)contact I am handling these collisions. All is working exactly as planned and there are no issues when manually moving this enemy.
However, the problem begins when I have the enemy wander around the scene through an SKGoal *wanderGoal set up as
// Low --> 0.5 Lowest --> 0.25
enemy.wanderGoal = [GKGoal goalToWander:low];
[enemy.agent.behavior setWeight:lowest forGoal:enemy.wanderGoal];
// Add obstacles to avoid for each of the inner map nodes
// that act as impassible areas for the enemy to pass through.
NSArray *obstacles = [SKNode obstaclesFromNodePhysicsBodies:innerMapArray];
/** This goal does not change --> enemies will ALWAYS
avoid the obstacles in the level map. This should always
be set to the highest priority of the enemy goals
*/
enemy.avoidGoal = [GKGoal goalToAvoidObstacles:obstacles maxPredictionTime:10];
// Highest --> 250
[enemy.agent.behavior setWeight:highest forGoal:enemy.avoidGoal];
The enemy moves randomly as expected, and seems to avoid the obstacles most of the time...yet on the occasion the enemy does in fact pass through the map barrier. Let me expand on this:
The map barrier is essentially a rectangle, and I have set up 2 nodes that act as a shell that represent the left half and the right half of this shape. These nodes have a thickness of 100 points so they are not simply CGPath refs but rather shapes. They act as these two brackets act: [] (my halves are flush touching at the middle).
My question is such:
How can I prevent the enemy from passing through these obstacles with using either physics with the bitmask categories, or by the goal behavior? (Or both together)
I understand that while a GKGoal is a suggested behavior it does not guarantee this goal to be reached (but it can be ~95% sure it wont happen as Apple's AgentsCatalog shows). But, what I don't understand is why the enemy does not simply "slide" along the edge of the boundary as the goal tells it to (try to) "pass" through such boundary.
This "sliding" behavior does happen when I manually move the enemy around the scene, and as I attempt to move it passed a boundary deemed an obstacle, it slides towards the direction that I am having it move (just as any video game does that has map boundaries).
SO
If someone can help me prevent this from happening / explain how to approach preventing the enemy from ever passing through these (supposedly) "impassible" areas, I would be most appreciative.
FYI: If you are unclear with what I am describing / how I have created any objects, please ask and I will provide more info to help.

SceneKit: PhysicsWorld relative to body

I have a ship that everything is centered around and everything moves relative to it. It is a first person shooter. Right now when I fire, and the ship speeds up, it catches up to the bullets. I would like the physics world to move relative to the ship's speed so that the bullet is essentially unaffected by ship speed.
The physics engine is doing the right thing, but not the right thing for my game.
I have other elements that move relative to the ship as it moves, which move correctly now, so don't want make ship stationary and move everything else in world around it. I don't see a direct way to do this maybe there is an indirect way? Perhaps I can manually take over the positioning of the bullets. I would like to use the other parts of the physics engine for doing collisions etc so don't want to completely manually do it, but will if that is the only option.
Anyone else have any suggestions?
It sounds like your bullets are receiving air friction. This is controlled by the physics bodies "damping" property. A value of 1.0 will make it static without movement. A value of 0 will allow it to move continuously without ever stoping. The default value is 0.1 as per the Apple documents. Assign your node like so to remove the damping(air friction)...
yourNode.physicsBody.damping = 0

Needing advice about my game logic

I am creating a game where you are given a few letters (they are images, each letter is an image)
and you you have empty slots (an image of a black box).
When the user touches the image of the letter, it clones the letter, and starts moving that clone where the user is touching, and if he put it into one of the slots (one of the black boxes) then it just drops there, where if he didn't move it to the black box it just resets (the clone disappears, like it was dropped).
Now I am using storyboard here, and I have my level1.lua file ready, with the scene background and all.
What logic should I use here? I tried googling tutorials for drag and drop in corona but couldn't find any.
Can anyone recommend a good logic to do this within storyboard messed up file?
You can use this methods in your logic
Physics Collision
The letters and slots have physics bodies, you can drag the letter and when the letter collides on a slot body you can now get the collision data and you can now able to drop the letter.
Rectangle Approach
This is straight forward. You have to get all of the slots' x, y, width and height and compare it to the letter's x and y when you dragged it. Letter's x and y must be between slot's (x to x+width) and (y to y+height) so that you can now drop the letter to the specified slot.
It is up to you how you will code this. This is just my idea about drag and drop.

XNA Falling through tiles after jumping

I discovered a lot of new info since I posted this question so I have completely rewritten it:
I have run into some problems while implementing gravity and jumping in my tile-based game.
Sometimes when my character lands between tiles after a jump, the characters falls through them.
The problem certainly is not that my speed is too high since my max speed is the same as my tile-size. Also, the problem does not occur after every time the character jumps. I can often make a jump, get to max velocity, and still land well. But sometimes the character just falls through.
My level exists of layers of tiles. Each layer has it's own collision-type. So every tile in the same layer, follows the same rules for collision.
In my current set-up all layers use per-pixel collision.
This is how I update the player coordinates
http://pastebin.com/qVc6gv6T
This is the class where I calculate my collissions.
http://pastebin.com/7GqrFih6
In the update I just do:
controls.Update(player);
physicsEngine.HandleCollissions(levelManager, player);
I imagine that the problem could be because the player gets moved to another tile after collission. Collides with the other tile. But that doesn't count since the other tile already has been checked for collision?
Or could it be because I use foreach instead of for-loops?
You say the error occurs after jumping. So, your character falls toward a tile (has some positive velocity.Y) and hits it. The collision code executes the // the player comes FROM ABOVE and collides with the tile block. You run a loop that sets player velocity to zero and moves the player out of collision. This loops calls a function using a term tile as an argument. I assume you loop through a set of tiles in order to collide with more than a single square. Am I right?
If I am, then the problem occurs after that first collision and correction. Now your player has a velocity.Y of 0, but they are colliding with a new tile. So in spite of your comment about the meaning of your else block, you may have more position changes happening:
if (player.Velocity.Y > 0)
else // if (moveVector.Y < 0) // what you commented
else // if (moveVector.Y <= 0) // what your else really means
Now sometimes (only when you have slightly mis-alligned per-pixel collisions) you have course corrections happening the the wrong direction. That would produce a fall-through effect. You could try setting a break point in the // the player comes FROM UNDER and collides with the tile block, and then running the program in a scenario that shouldn't cause player.WorldPositionY += 1; to happen. If it happens when it wasn't supposed to, then you have your culprit.
I am doing a lot of speculation here. I think you should post more code so that we can know for sure. But perhaps a paste bin would be appropriate place for it.

Box2D shapes falling through world

I have this very odd problem with Box2D (under iOS). I have a body that is resting on the bottom of the screen. If I set the body from b2_dynamicBody to b2_staticBody using body->SetType(b2_staticBody), then later set it back to b2_dynamicBody, it falls through the bottom of the screen for a quick moment (several frames as I can see it moving down) before being pushed back up through the floor and coming to a rest correctly.
This behavior seems to only happen on the bottom of the screen. Doing the same thing with other bodies sitting on each other does not product this behavior.
I am defining the screen edges as follows (screenRect as already been adjust to world space):
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body* groundBody = globalWorld->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(screenRect.p1.x,screenRect.p1.y), b2Vec2(screenRect.p2.x, screenRect.p1.y));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(screenRect.p1.x,screenRect.p1.y), b2Vec2(screenRect.p1.x, screenRect.p2.y));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(screenRect.p1.x,screenRect.p2.y), b2Vec2(screenRect.p2.x, screenRect.p2.y));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(screenRect.p2.x,screenRect.p2.y), b2Vec2(screenRect.p2.x, screenRect.p1.y));
groundBody->CreateFixture(&boxShapeDef);
The oddest part of this is the more I switch the body from dynamic to static, the farther and farther it falls through the floor before being pushed back up.
I have not changing anything else in the body except from dynamic to static. If I move the body and drop it back onto the floor, the start switching it from dynamic to static, it resets how much it's drop into the floor, but it build up again the more I switch.
I can't figure out why is is falling through the floor like this unless I'm creating it incorrectly. Everything else in the game works perfectly.
I had this problem too. Here is my theory: when the object is static, it stops generating contacts with other static stuff like the ground. So, it seems that when set to dynamic again, it fails to update this contact checking.
My solution was this: since changing the position of an object correctly update these contacts, I just move the object to its current place. Unfortunately, it's detected as a zero change. So, I need to move it elsewhere, then move it again to its previous position.
b2Vec2 pos = body.getPosition();
float ang = body.getAngle();
b2Vec2 off_map(-100,-100);
body.setPositionAndAngle(off_map, 0);
body.setPositionAndAngle(pos, ang);
If you don't have an off-map area, you can try deactivating the object during the first move.

Resources