Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to make an sprite go to correct direction  (Read 6296 times)

0 Members and 1 Guest are viewing this topic.

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
How to make an sprite go to correct direction
« on: December 11, 2010, 06:39:08 am »
Hi,

On my game i want that my shoot go to correct direction (enemy direction), but it doesnt occur.

You can see here:
http://www.prsolucoes.com/downloads/solodefender.zip

The code that im using is (on update):

Code: [Select]


void Bullet::update()
{
updateSpriteDirection();
checkCollision();
updatePosition();
}

void Bullet::updateSpriteDirection()
{
sprite.SetRotation( Functions::angle(sprite.GetPosition().x, sprite.GetPosition().y, targetX, targetY) );
}

void Bullet::checkCollision()
{
if (sprite.GetPosition().x + sprite.GetSize().x < 0)
{
remove();
}
else if (sprite.GetPosition().y + sprite.GetSize().y < 0)
{
remove();
}
else if (sprite.GetPosition().x > Constants::SCREEN_WIDTH)
{
remove();
}
else if (sprite.GetPosition().y > Constants::SCREEN_HEIGHT)
{
remove();
}
}


void Bullet::updatePosition()
{
sprite.SetPosition(sprite.GetPosition().x + speedX, sprite.GetPosition().y + speedY);
}

void Bullet::updateSpeed()
{
if (sprite.GetPosition().x < targetX)
{
speedX = Constants::BULLET_SPEED;
}

if (sprite.GetPosition().x > targetX)
{
speedX = (Constants::BULLET_SPEED * -1);
}

if (sprite.GetPosition().y < targetY)
{
speedY = Constants::BULLET_SPEED;
}

if (sprite.GetPosition().y > targetY)
{
speedY = (Constants::BULLET_SPEED * -1);
}
}



What is wrong?

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
How to make an sprite go to correct direction
« Reply #1 on: December 18, 2010, 01:18:01 am »
The easiest thing to do is to store the player's current direction somewhere, so you can then apply that to the bullet.

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
How to make an sprite go to correct direction
« Reply #2 on: December 18, 2010, 04:04:26 am »
Is it that i do.

TargetX and TargetY is the position of enemy (x and y).

The problem is that i need rotate the laser to point to the enemy, and make the bullet go to targetX and targetY on the correspondent angle.

Breakman79

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
How to make an sprite go to correct direction
« Reply #3 on: December 18, 2010, 07:09:37 am »
You need to use trigonometric functions to determine the trajectory of your shots.

If you take the sine(angle) it gives you the ratio of change in the y-axis.
If you take the cosine(angle) it gives you the ratio of change in the x-axis.

So, you can update the position with something like this (pseudocode):

sprite.x += cos((double)angle*3.14159/180.0)*speed
sprite.y += sin((double)angle*3.14159/180.0)*speed

I think that's basically about right.   I can't remember if the results need to be converted to match the screen coordinate system or not.

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
How to make an sprite go to correct direction
« Reply #4 on: December 22, 2010, 01:41:59 am »
Hey there, I'm a bit baffled about this too.
Not about the way of doing this by itself, but I stumbled across a bug which I can't figure out (hijacking this a little bit, but since it's the same topic..)

Basically what I'm doing this this:

Code: [Select]

// 180 / PI = 57.3065
float Utility::AngleBetweenVectors(sf::Vector2f a, sf::Vector2f b){
return 57.3065f * atan2(b.y - a.y, b.x - a.x);
}

//Simply a wrapper
void Entity::SetRotation(float facing){
sprite.SetRotation(facing);
}

main () {
     Entity z, p;
     z.SetPosition(...);
     p.SetPosition(...);
     //z and p are two objects of class Entity which contain a sprite.
     //p is the player and z is another sprite which is supposed to follow p around.

     float f=Utility::AngleBetweenVectors(z.GetPosition(), p.GetPosition());

     // x = speed * cosine( angle * PI/180 )
     // y = speed * sine( angle * PI/180 )
     float nx= 1 * cos(f * 0.01745);
     float ny= 1 * sin(f * 0.01745);

     z.Move(nx, ny);
     z.SetRotation(f);
}


The code *should* work well. And determining nx and ny does work correctly. But the sprite of z is turning into the wrong direction.
To get the right rotation I need to use:
Code: [Select]

     z.SetRotation(360 - f);


So AngleBetweenVectors returns a degree measured counter-clockwise and SetRotation treats it as being clock-wise.

Is that an error on my part? Or a problem with atan2?

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
How to make an sprite go to correct direction
« Reply #5 on: January 04, 2011, 02:12:48 am »
Hi s3rius,

Your function works like a charm.

You can see my game prototype here:
http://www.prsolucoes.com/downloads/solodefender.zip

But i have a simple problem now, my shoot with your function go correctly to X/Y position, but i need that it continue moving if not collide with target, so i need that the shoot continue moving to current direction and go out of screen.

So resuming:
How i can change your function to make my shoot go to a direction based on targetX / targetY position and dont stop move?

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
How to make an sprite go to correct direction
« Reply #6 on: January 05, 2011, 03:10:21 am »

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
How to make an sprite go to correct direction
« Reply #7 on: January 05, 2011, 03:18:51 am »
A great tutorial and solution is here:
http://www.benoitfreslon.com/actionscript-throw-bullets-to-mouse-direction

This will help everybody.