# SDL Camera Interaction Problem

This topic is 2707 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone, I have a bit of a problem.

I'm working on a C++ SDL game. I just recently inputed the ability to scroll through the level with a camera. However, there is a problem. My game is turn based, and the character can fire his bullet past the limits of the camera screen. I want the player to know where the bullet lands, so I decided I would make the camera follow the bullet, keeping the bullet in the middle of the screen until it hits something.

However, for some reason, when I input the code for following the bullet with the camera, the bullet starts off to the side of the character, instead of from his gun. Without the camera following, the bullet starts from his gun as planned. I'm not completely sure why this is happening, but it seems that the bullet doesn't want to travel separately from the camera, and all the changes made with the position of the camera applies to the bullet as well.

Here is some code I have:

When the "Fire" button is clicked:
//Make the bullet position the same as the player position

Bulletx = ((Player1x) - camera.x);
Bullety = ((Player1y) - camera.y);

In function Bullet::move() :
//Move the Bullet

Bulletx += Velx;
Bullety += Vely;

//Make the camera follow the bullet
camera.x = ( Bulletx + bullet->w/2 );camera.y = ( Bullety + bullet->h/2 );

In function Bullet::show() :
//Show the bullet
apply_surface( Bulletx - bullet->w/2, Bullety - bullet->h/2, bullet, screen );

Any ideas as to what I'm doing wrong, or what I can do to fix this problem? Thanks.

(P.S. I've posted this in the beginners area, but I don't think it was the correct area.)

##### Share on other sites
This is tricky.

First, nothing's position should be relative to the camera, and the camera's position should be relative to whatever it is following (either the player or his bullet in this case).
 Bulletx = ((Player1x) - camera.x); Bullety = ((Player1y) - camera.y); 
It should just be
 Bulletx = Player1x; Bullety = Player1y; 

Also, you're not changing the bullet's draw coordinate relative to the camera!
 apply_surface( Bulletx - bullet->w/2, Bullety - bullet->h/2, bullet, screen ); 
Should be
 apply_surface( Bulletx - camera.x, Bullety - camera.y, bullet, screen ); 
if I'm understanding your code correctly.

Try those changes and see if it doesn't fix things.

##### Share on other sites
@ nfries88: Thanks for your advice! And you were correct, I wasn't drawing the bullet relative to the camera, and that was one of the problems.

The other problem that I had was that the camera still refused to follow the bullet (I'm not sure why), but I worked around that by making the camera move at the same velocity as the bullet. (Instead of making the camera move WITH the bullet)

Thanks for your help, it turns out I was just being dumb.

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631365
• Total Posts
2999581
×