4
\$\begingroup\$

I have three UIImageViews. enemy1, enemy1AggroBox and mainSprite. What I want to do is when mainSprite and enemy1AggroBox interect, I want enemy1 to start moving towards mainSprite. Basically creating aggro for a game.

if(CGRectIntersectsRect(mainSprite.frame, enemy1AggroBox.frame)){
//Code here//
}

My plan would be to call this method in viewDidLoad. I'm not using any sort of framework like cocos2d or OpenGLES. If you need to see any more code just ask.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Are the rectangles always axis-aligned, or can they rotate? \$\endgroup\$
    – Nic Foster
    Feb 17, 2012 at 15:04
  • \$\begingroup\$ They don't rotate \$\endgroup\$
    – Will
    Feb 17, 2012 at 15:41
  • \$\begingroup\$ Are you checking for intersections frequently in a game loop or are you checking after some specific event? \$\endgroup\$
    – willc2
    Feb 17, 2012 at 21:27
  • \$\begingroup\$ I'm checking frequently \$\endgroup\$
    – Will
    Feb 18, 2012 at 2:20
  • \$\begingroup\$ If you check frequently, you have to calculate the small position change for each time step closer to your target position then move the center point of enemy1 to that new position every frame. \$\endgroup\$
    – willc2
    Feb 18, 2012 at 6:52

2 Answers 2

1
\$\begingroup\$

I'm not familiar with the particular platform you're working on, but I would imagine that a simple rectangle intersection function would solve this.

if ( intersects(mainSprite.frame, enemy1AggroBox.frame ) {
    MoveTowardPlayer(Player1, Enemy1);
}

bool intersects(const Rect& first, const Rect& second) const {
    if (first.Max.X < second.Min.X )
        return false; 

    if (first.Max.Y < second.Min.Y )
        return false;

    if (second.Max.X < first.Min.X )
        return false;

    return (second.Max.Y >= first.Min.Y )
}

Something like this would work provided your rectangles are Axis-Aligned (which you've indicated they are in your comment (below your question).

And to make one move towards the other (using pseudo-code, I don't know what engine you're using so I can't write exactly what you'll need to compile):

void MoveTowardPlayer(const Unit& player, const Unit& enemy) {
    // Get the difference in positions between player and enemy
    Vector2 direction = player.position - enemy.position;

    // Normalize the difference to get a direction vector
    direction.Normalize(); // Also sometimes called Unitize

    // Move the enemy in the direction of the player, based of the amount of
    // time has elapsed, and the enemy's movement speed, in units per second.
    enemy.position += (direction * enemy.movementSpeed * timeDelta);
}

I'm just giving a generic example on how to move your enemy toward a player. That question alone is pretty broad.

\$\endgroup\$
3
  • \$\begingroup\$ CGRectIntersectsRect() is a built-in function that does what your code does. \$\endgroup\$
    – willc2
    Feb 17, 2012 at 21:20
  • \$\begingroup\$ anyway where is the code that makes the UIIMageview start going towards the other? \$\endgroup\$
    – Will
    Feb 17, 2012 at 21:31
  • \$\begingroup\$ Added that just now, the question on how to move one object toward another should be posted as a separate question, because it's fairly broad. I'm sure the answer to that exists on GameDev.StackExchange already anyhow. \$\endgroup\$
    – Nic Foster
    Feb 17, 2012 at 21:45
0
\$\begingroup\$

To make a UIView (or any subclass like UIImageView) move to a given point, in a given amount of time, do this.

NSTimeInterval totalDuration = 3.0; // three seconds  

// if you change any view property inside this block
// it will be smoothly interpolated over a duration     
[UIView beginAnimations:nil context:nil];  
[UIView setAnimationDuration:totalDuration];  

enemy1.center = mainSprite.center;  

[UIView commitAnimations];  
\$\endgroup\$
4
  • \$\begingroup\$ If you read the Programming Guide for Core Animation, you'll find Apple has done virtually all the heavy lifting already. \$\endgroup\$
    – willc2
    Feb 17, 2012 at 23:08
  • \$\begingroup\$ This didn't seem to work. When they intersect the enemy1 image just immediately goes to the mainSprite.center. If it's easier maybe link me the part of the Apple Programming guide that shows how to accomplish what I'm trying? \$\endgroup\$
    – Will
    Feb 18, 2012 at 1:43
  • \$\begingroup\$ That method is for infrequent use and lets CA calculate how much a view needs to move each frame, automatically. -viewDidLoad is normally only called once, right after the UIView associated with some view controller has been fully set up. \$\endgroup\$
    – willc2
    Feb 18, 2012 at 6:34
  • \$\begingroup\$ Either google for "Core Animation Programming Guide" or try this link developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/… Try their sample code and don't imagine you can skip doing a fair amount of experimentation before you get a handle on it. There are also lots of YouTube videos and blog posts. \$\endgroup\$
    – willc2
    Feb 18, 2012 at 6:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .