Simple object path gone wrong!

Apr 15, 2013 at 9:18pm
So I have this code to make a bullet shoot out of another object. Here's the move function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Bullet::Move(sf::RenderWindow& Window)
{
	angle.x = target.x  - sprite.getPosition().x;
	angle.y = target.y - sprite.getPosition().y;
	distance = sqrt((angle.x * angle.x) +  (angle.y * angle.y));
	direction.x = angle.x / distance;
	direction.y = angle.y / distance;
	velocity.x = direction.x * speed;
	velocity.y = direction.y * speed;
	position.x = sprite.getPosition().x;
	position.y = sprite.getPosition().y;

	sprite.move(velocity.x * speed, velocity.y * speed);
}


But that goes to the mouse position. I'd like make it so the length of the bullet path won't stop at the cursor but keep going until it exits the screen or hits something. How would I change that?
Last edited on Apr 15, 2013 at 9:44pm
Apr 15, 2013 at 10:45pm
closed account (D80DSL3A)
Much of the code in your Move() function doesn't belong there.
The calculation of the velocity components is something to be done once when the bullet is fired, not continually while the bullet is moving. I believe this was pointed out in an earlier thread of yours on this, but perhaps it wasn't clear.

While the bullet is in motion you just need to increment the position using the already calculated values for the velocity.

Here's an idea of what's involved. I'm assuming a fire() function, which would be called when the firing event occurs only (a press of the space bar or the click of a mouse button, whatever your event trigger is).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Bullet::Fire(float targetX, float targetY)
{
    // just use local variables to help with the calculations. These dont need to be data members
    float distX = targetX - x0;// x0, y0 are coordinates the bullet is fired from
    float distY = targetY - y0;// you may need to pass these to the function as well
    float distance = sqrt(distX*distX + distY*distY);
    
    // velocity.x and .y and speed should be data members of the bullet class
    velocity.x = distX *speed/distance;// these will be used in the Move()
    velocity.y = distY *speed/distance;

    // position.x and .y may be useful as members too
    position.x = x0;
    position.y = y0;
    sprite.SetPosition(x0, y0);
}

The Move() function would be called repeatedly while the bullet is in travel. You just need to update the bullet and sprite positions using the velocity.x and .y found in the Fire() function.
1
2
3
4
5
6
7
8
void Bullet::Move()// what is the sf::RenderWindow object needed for?
{
	
	position.x += velocity.x;// update the bullet position
	position.y += velocity.y;

        sprite.SetPosition(position.x, position.y);
}

I hope that helps.
Apr 15, 2013 at 10:45pm
Do not recalculate the velocity every update. Calculate the velocity once, when the bullet is first fired. Then record that velocity (probably as a member of the Bullet class) and apply it every update.

EDIT: ninja'd by a much more detailed response. lol
Last edited on Apr 15, 2013 at 10:46pm
Apr 16, 2013 at 12:00am
Awesome, that worked. Thanks!

But for some reason now I'm getting an error code every time the program ends - Unhandled exception at 0x770015de in Practice SFML.exe: 0xC0000005: Access violation reading location 0xfeeefef6. Any idea why?
Apr 16, 2013 at 12:05am
Wow, so I found out the problem on accident. Not sure why it's happening though.

When I set shootSpeed = sf::milliseconds(100);

the error happens. When I do shootSpeed = sf::milliseconds(0);

the error never comes up.
Apr 16, 2013 at 12:10am
Last edit. This is where that's at.

1
2
3
4
5
6
7
8
9
10
11
12
13
void Turent::Shoot(sf::RenderWindow& Window)
{
	if(shootClock.getElapsedTime().asSeconds() > shootSpeed.asSeconds() && sf::Mouse::isButtonPressed(sf::Mouse::Left))
	{
		Bullet blt;
		blt.Setup(sprite.getPosition().x, sprite.getPosition().y, rotation + 90, sf::Mouse::getPosition(Window).x, sf::Mouse::getPosition(Window).y);

		bullets.push_back(blt);
		shootClock.restart();
	}
	

}
Topic archived. No new replies allowed.