# Problems with projectiles on a map.

First I would like to say first time posting and monogame is awesome! Keep up the great work. But I do have a problem that I can’t seem to figure out.

So I have been working on this issue for a long time now and is annoying me. I am trying to get projectiles to move back and forth on the map with collision to my player. I have successfully done this if the projectile is within the first screen but as my player moves to the right towards the map then everything gets all messed up. I can’t seem to get it right.

Initialization:

bullPos = new Vector2(1400 - mapX, 530);
bulletRectangle = new Rectangle((int)bullPos.X, (int)bullPos.Y, 10, 20);
moveR = true;

bulletUpdate:

if (moveR)
{
bullPos.X += 5;
bulletRectangle.X += 5;
}
else
{
bullPos.X -= 5;
bulletRectangle.X -= 5;
}

``````            if (bullPos.X > (1700 - mapX) || bullPos.X < (1400 - mapX))
moveR = !moveR;
``````

Collision:

if (map.bulletRectangle.Contains(player.worldPosition))
{
bullCollided = true;
map.bulletHit = true;
}

and where I think the problem is seems to be here

The Camera:

float cameraSpeed = 6f;
if (player.cameraPosition.X > screenW / 2)
{
map.mapX += (int)cameraSpeed;
player.cameraPosition.X -= 6f;

``````            map.bullPos.X -= 5f;
map.bulletRectangle.X -= 5;
}

if (player.worldPosition.X <= 0)
{
player.worldPosition.X = 0;
player.cameraPosition.X = 0;
}

if (map.mapX < 0)
{
map.mapX = 0;
player.cameraPosition.X -= 6f;

map.bullPos.X -= 5f;
map.bulletRectangle.X -= 5;
}

if (player.cameraPosition.X < screenW / 2 && player.facing == "left")
{
player.cameraPosition.X += 6f;
map.mapX -= (int)cameraSpeed;

map.bullPos.X += 5f;
map.bulletRectangle.X += 5;
}

if (map.mapX > map.MapW - screenW)
{
map.mapX = map.MapW - screenW;
player.cameraPosition.X += 6f;
}

if (player.cameraPosition.X > screenW)
{
player.cameraPosition.X -= 12f;
player.worldPosition.X -= 12f;
}
``````

If anyone has any kind of idea that would be awesome.

Thanks, Jake

Unfortunately that’s a lot of code and context for someone to take on to answer this question. I’d suggest trying to simplify your code, break it down to isolate the bug, and if you can get it down to fewer lines of code then I’m sure someone will be able to help.

Okay well I have been trying to get projectiles to work on my map for a couple days now but can’t seem to get it working. The thing I have noticed is that when the projectile is off the map then the image will move back and forth like its supposed to but the rectangle that is used for collision doesn’t move. It stays in the spot that it was drawn. So like the example above it only says at 1400 for the X position.

If anyone has any time to take a quick look that would be great.

Thanks, Jake

Is your `bulletRectangle` a field or a property of the map class? If it is a property (ie has get and set), then you’ll be coming up against the fact that structs are always returned by value. That is, the property would return a full Rectangle and then you’d alter the X field and the change would be discarded. Either switch it to a field (remove the get / set) or set the whole rectangle every time with a call like `map.bulletRectangle = new Rectangle(map.bulletRectangle.X + offset, ....)`

If this is not the problem, it would be helpful to see your drawing code too, particularly how the camera offset is applied.

Hi Aranda thanks for the reply, I appreciate the help. bulletRectangle is a field i believe. It is just declared simple like:

public Rectangle bulletRectangle2;

Also for the drawing code it is:

if (bulletHit == false)
{
if (tilemap[y, x] == 24)
spriteBatch.Draw(bulletTex, bullPos2, Color.White);
}

I tried the collision by seeing if the player.rectangle.Contains(map.bullPos) but that didn’t seem to work. You told me to try and set the whole rectangle every time. Would I do that every time I update when moving the X value?

Thanks, Jake

That only applies if your modifying the rectangle through a property. Fields are fine to do what you are doing now.

That said, it’s not really a good idea to modify both the position and collision rect separately from external classes. Maybe make the Position a property or SetPosition() method that also sets the rectangle position. That way you reduce the chance of them getting out of sync.You could even just create the rectangle from the position and size whenever you need to test for collision.

Also, it seems like your problem stems from how you are moving your camera (you haven’t posted that code, ie how you draw the world when the camera moves). You may be treating world positions as screen positions or vice versa. I’d recommend going with a Camera transform passed to your spritebatch (eg: XNA Camera 2d with zoom and rotation | David Amador), that way you have one world position which doesn’t change as your camera moves around.

Ahh I think that may be the solution… I will check it out soon. Thanks a lot!