• Create Account

Banner advertising on our site currently available from just \$5!

# making a bitmap follow a character

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1Analizer  Members   -  Reputation: 110

Like
0Likes
Like

Posted 23 April 2013 - 10:39 AM

Hi.

I have created a function for an enemy and I'm trying to make the bitmap follow my character and when they interact the character dies and the game ends.

Can someone give me heads up?

Here is the class code:

#ifndef _ENEMY_H_
#define _ENEMY_H_

#include <allegro.h>

class Enemy
{
public:
int x;
int y;
BITMAP *enemy;
Enemy();
~Enemy();
void blitenemy(BITMAP*buffer);
};

#endif



and here is the cpp code:

#include <allegro.h>
#include "Enemy.h"

Enemy::Enemy()
{
srand(time(NULL));
x = rand() % 400 + 1;
y = rand() % 300 + 1;
}

Enemy::~Enemy()
{
destroy_bitmap(enemy);
}

void Enemy::blitenemy(BITMAP*buffer)
{
}


### #2KnolanCross  Members   -  Reputation: 1503

Like
1Likes
Like

Posted 23 April 2013 - 12:00 PM

Didn't really got the question.

You want the enemy to get to the character? If that is the case, on an open world (in other words, a scenario with no obstacles) all you have to do is update the enemy position by this formula:

    angle = atan2f(playerY - monsterY, playerX - monsterX);
nextX = monsterX + (cos(angle) * moveSpeed * dt);
nextY = monsterY + (sin(angle)  * moveSpeed * dt);
setMonsterPosition(nextX, nextY);


moveSpeed is the monster speed and dt is the amount of time passed since the last frame (your library probably gives you that with some function).

If the world has obstacles, then you must search for pathfinding, particularly A*.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS