Detecting if Sprites are in Rect? - sprite-kit

i have a tower and a monster. The tower has a SkShapeNode around it, acting as its range. And i want to check if the monster is still inside the range of the tower( i.e inside the skshape node) every frame.
i have this method which i think checks if there is anything still inside the towers' range:
-(void)update
{
[self.currentScene.physicsWorld enumerateBodiesInRect:self.towerRangeNode.frame usingBlock:^(SKPhysicsBody *body, BOOL *stop) {
if (body == nil) {
[self lostSightOfMonster];
}
}];
}
however, the [self lostSightOfMonster] method never runs.
for additional understanding, i call this method in the update method of the scene:
[self enumerateChildNodesWithName:#"Tower" usingBlock:^(SKNode *node, BOOL *stop) {
Tower *tower = (Tower *) node;
[tower update];
if (tower.hasChosenEnemy) {
[tower updateRotation];
}
}];
}
i suspect its something to do with the enumerating bodies in rect method, however i am not sure.
thanks in advance

I would suggest a different approach. Rather than checking if it is in contact with the sprite I would check its distance to the origin of the tower using Pythagorean theorem. I think the code would be more simple and there would be the added bonus of not having the range sprite to be visible at all times.
here is some concept code to show what I mean:
double distance;
distance = pow(enemySprite.position.x-towerSprite.position.x, 2);
distance += pow(enemySprite.position.y-towerSprite.position.y, 2);
distance = sqrt(distance);
if (distance < towerRange) {
//shoot at ememy
}
You could hypothetically put this in a for loop to check for multiple enemies as well.

Use this code to check for frame intersection of 2 nodes:
if(CGRectIntersectsRect(towerNode.frame, enemyNode.frame))
{
// do your thing
}
If you have more than one tower and one enemy you would use this code:
for(SKNode *towerNode in towerNodeArray)
{
for(SKNode *enemyNode in enemyNodeArray)
{
if(CGRectIntersectsRect(towerNode.frame, enemyNode.frame))
{
// do your thing
}
}
}
The above assumes you are using the physics body of the tower to check for range. Alternately you can also use some simple x and y coordinate checking like this:
if(((towerNode.position.y <= enemyNode.position.y+100) && (towerNode.position.y >= enemyNode.position.y-100)) &&
((towerNode.position.x <= enemyNode.position.x+100) && (towerNode.position.x > enemyNode.position.x+100)))
{
// do your thing
}

Related

How can I make a simple automatic shoot function for a gun in Unity?

I am trying to create a procedural gun controller, but I can't find why my gun behaves so weird when I change the fire mod to automatic. To understand this better, I will give an example: when I try to shoot by pressing the "Mouse0" key, on semi auto mod it works fine, it behaves like I want it to (the bolt kicks back when I shoot, the front barrel kicks back - I animate them with code instead of frame animations from Unity or a 3rd party soft like Blender), but..., when I change to automatic, the problem is not that I get errors, The animations of the moving pieces don't work as they should and are not triggered correctly.
I tried to use different methods for shooting(using the WaitForSeconds() and WaitForSecondsRealtime() while using coroutines). It didn't work. I tried using the time function with scaled and unscaled time in the update function. I still got the same results.
What should I do to?
This is the shoot function untill now:
void GunFireManagement()
{
if (fireType == fireMethod.single)
{
foreach (BlowBack anim in animations)
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
gunSoundClone = Instantiate(gunShootSound, this.transform.position, Quaternion.identity) as GameObject;
anim.piece.transform.position = anim.kickbackState.transform.position;
}
if (anim.piece.transform.position != anim.initialPosition.transform.position)
{
anim.piece.transform.position = Vector3.Lerp(anim.piece.transform.position, anim.initialPosition.transform.position, anim.speed);
}
Destroy(gunSoundClone, 0.5f);
}
}
if (fireType == fireMethod.auto)
{
foreach (BlowBack anim in animations)
{
if (Input.GetKey(KeyCode.Mouse0) && Time.time - lastFired > 1f/autoFireRate)
{
lastFired = Time.time;
gunSoundClone = Instantiate(gunShootSound, this.transform.position, Quaternion.identity) as GameObject;
anim.piece.transform.position = anim.kickbackState.transform.position;
}
if (anim.piece.transform.position != anim.initialPosition.transform.position)
{
anim.piece.transform.position = Vector3.Lerp(anim.piece.transform.position, anim.initialPosition.transform.position, anim.speed);
}
Destroy(gunSoundClone, 0.5f);
}
}
}
The issue is how you are using Vector3.Lerp. The first two arguments you pass to the method are supposed to be the start and end positions of the animation, and the third one, t, is supposed to be the progress of the animation from the start towards the end, as a value between 0 and 1.
You can calculate the value of t by dividing the time since the shot started with the duration of the animation. For example if the length of the animation is 2 seconds, and the short started 1 second ago, then t should be 0.5.
if(isFiring)
{
float timeSinceShotStart = Time.deltatime - lastFired;
// t = s / v
float animationDuration = 1f / anim.speed;
UpdateBlockBackAnimationState(timeSinceShotStart / animationDuration);
}
}
private void SetBlowBackAnimationState(float progress01)
{
foreach(BlowBack anim in animations)
{
Vector3 initialPosition = anim.initialPosition.transform.position;
Vector3 finalPosition = anim.finalPosition.transform.position;
anim.piece.transform.position = Vector3.Lerp(initialPosition, finalPosition, progress01);
}
}
I recommend you try to split up your code into smaller methods that are easier to understand. You are trying to do so many things in one generic "GunFireManagement" method that it becomes pretty difficult to keep track of all the different pieces :)
I also recommend considering using a tweening framework such as DOTween to make it easier to animate things over time.

Strange If/Else Behavior

Context
I am making a mobile game in which the player is required to touch objects in a specified order. The correct order is determined in a List called clickOrder. To determine the current object the player is supposed to click, currClickIndex is used.
Problem
When touching a correct object, the debug text will display "Correct" for a split second, and will then immediately change to "Wrong." What I am unsure about is why both the if and else blocks are executed when only touching a single object.
Code
void Update()
{
if (Input.touchCount == 1)
{
if (this.enabled)
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
if (hit != null && hit.collider != null)
{
// check if the touched object is the correct one
if (hit.collider.gameObject == clickOrder[MyData.currClickIndex])
{
debug.text = "Correct";
MyData.currClickIndex++;
}
else
{
debug.text = "Wrong";
}
}
}
}
}
As soon as the correct object is being touched, you do this:
MyData.currClickIndex++;
which moves you forward in the ordered sequence, and from then on, the previously correct object is not correct anymore. But you're still touching it.
If you want to avoid this, you need to move forward in the sequence after you've touched the correct object.
if (there are touches and the correct object is being touched)
{
set a flag;
}
else if (a flag has been set)
{
MyData.currClickIndex++;
reset the flag;
}

How to detect a touch inside a box2d fixture created by PhyicsEditor in Cocos2d-x?

My testing environment:
Xcode 4.6, New cocos2d-x+box2d project
cocos2d-2.1beta3-x-2.1.1
PhysicsEditor 1.0.10
I modified a bit in HelloWorldScene of PhysicsEditor cocos2dx demo to make it simpler, Here are some of my code:
initPhysics
gravity.Set(0.0f, 0.0f);
So that the sprite will not move.
Replace source code inside ccTouchesEnded to:
CCTouch* pTouch = (CCTouch *)touches->anyObject();
CCPoint location = pTouch->locationInView(pTouch->view());
CCPoint convLoc = CCDirector::sharedDirector()->convertToGL(location);
b2Vec2 v = b2Vec2(convLoc.x/PTM_RATIO, convLoc.y/PTM_RATIO);
for (b2Body *b = world->GetBodyList(); b; b = b->GetNext())
{
b2Fixture *f = b->GetFixtureList(); // get the first fixture
CCSprite *sprite =(CCSprite *) b->GetUserData();
if(sprite != NULL)
{
if(f -> TestPoint(v))
{
CCLog("You touched a body %d",sprite->getTag());
}
}
}
The problem is that TestPoint only return true in a very small area (not for whole shape area).
Here is the screenshot:
Can anybody suggest how I debug this problem? Thanks
Updated: showing the generated data from PhysicsEditor
The problem is that you only probe for the first fixture. But complex bodies are made from several. The reason is that
Box2d only allows 8 vertexes per fixture
Box2d can only work with convex shapes
This is why complex shapes are decomposed into a list of polygons.
Iterate over the fixture list instead.
b2Fixture *f = body->GetFixtureList();
while(f)
{
if(f -> TestPoint(v))
{
CCLog("You touched a body %d",sprite->getTag());
}
f = f->GetNext();
}

How can i use void ContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) method in our application?

I dont know how to use this metnod in my application
void MyContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
b2PointState state1[2], state2[2];
b2GetPointStates(state1, state2, oldManifold, contact->GetManifold());
//NSLog(#"Presolving");
if (state2[0] == b2_addState)
{
const b2Body* bodyA = contact->GetFixtureA()->GetBody();
const b2Body* bodyB = contact->GetFixtureB()->GetBody();
b2Vec2 point = worldManifold.points[0];
b2Vec2 vA = bodyA->GetLinearVelocityFromWorldPoint(point);
b2Vec2 vB = bodyB->GetLinearVelocityFromWorldPoint(point);
b2Vec2 rV = vB - vA;
float32 approachVelocity = b2Dot(rV, worldManifold.normal);
if (-1.5f < approachVelocity && approachVelocity < 0.0f)
{
//MyPlayCollisionSound();
NSLog(#"Not Playing Sound");
}
else
{
NSLog(#"playing the sound");
}
}
}
How can I use this code in my HelloWorldLayer.mm Please help me...
I have a problem here, I have a scene where some bodies falls and hit a static body, edges alike, I implemented the b2ContactListener, and in my tick method Im checking for contacts and play the sound, the problem with this approach is that when a body is constantly in contact with the static body, the sound plays indefinitely overlaying the previous one, so at the end I have huge noise..
What can I do to avoid this situation?
Please help me thanks......
You just used following method into your update method and declare its object before schedule update method call.
Like in HelloWorldLayer.h
MyContactListener *contactListener;
And in HelloworldLayer.mm
Before
[self scheduleupdate];
contactListener=new MyContactListener();
world->setContactListener(contactListener);
Than this type of error not occur.
When two bodies collide, the b2ContactListener methods are called in the following sequence:
BeginContact
PreSolve
PostSolve
PreSolve
PostSolve
...etc
EndContact
So if you want to detect collision between the bodies once for each collision, use BeginContact or EndContact instead. These methods only take a single b2Contact parameter though so you might need to do away with using the oldManifold value in your calculation.

Make randomised sprites in a specific co-ordinate do something

I was wondering if anyone could help me with my program,
I have randomised my sprites into a specific set of co-ordinates.
I want one of the sprites that is at that specific co-ordinate, to be able to make them do something when they are at this random co-ordinate. The problem i am having is that i have to make a long list of if statements saying if this sprite is here do this if another sprite is here do the exact same thing.
if (red1.position.y>=0 && red1.position.y<=63) {
id r1animation = [CCMoveTo actionWithDuration:0.2 position:ccp(red1.position.x,33)];
[red1 runAction:r1animation];
}
if (red2.position.y>=0 && red2.position.y<=63) {
id r2animation = [CCMoveTo actionWithDuration:0.2 position:ccp(red2.position.x,33)];
[red2 runAction:r2animation];
}
i want to be able to say if any of the sprites are at that exact co-ordinate then move them to a point, in a short amount of code as possible. so basically grouping the sprites or something i'm not sure.
Thanks
i want to be able to say if any of the sprites are at that exact co-ordinate then move them to a point
Firstly, specify the 'hotspot' programatically:
CGPoint hotspot = ccp(32,32); // convenience macro,
//creates a CGPoint with x = 32, y = 32
You should store a reference to all your sprites in an array when you create them (you can use cocos2d's 'tagging' also, but I usually like to use an array for simplicity)
-(void)init {
//.. misc
// creating sprite returns a reference so keep it in an array
CCSprite* curSprite = [CCSprite spriteWithFile: //...etc]
[self.spriteArray addObject: curSprite];
// add all sprite references to your array
}
Now you can iterate over this array to see if any of the sprite's frames overlap the hotspot:
-(BOOL) checkAllSpritesForCollision
{
for (CCSprite *sp in self.spriteArray)
{
CGRect spriteRect = sp.frame;
if (CGRectContainsPoint(spriteRect,hotspot))
{
// run your action on sp...
}
}
// you might like to return YES if a collision happened?
}
This is a brute force method of checking whether every sprites frame contains a given point. There are many ways to skin this cat of course, but hopefully this will set you on a better path.
What you can do is to calculate the distance:
float pointX = thePoint.position.x;
float pointY = thePoint.position.y;
float pointDeltax = sprite.position.x-pointX;
float pointDeltay = sprite.position.y-pointY;
float pointDist = sqrt(pointDeltax*pointDeltax+pointDeltay*pointDeltay);
But maybe davbryns solution suits your purpose better.