1

I have a bunch of objects with the following properties:

xPos = x coordinate of position in world
yPos = y coordinate of position in world
size = size of object

I have a function that draws these objects onto the screen. The screen has an x offset and y offset so the user can pan and the objects will move on and off screen as needed. This is all working perfectly. Now I would like to add in the ability to zoom in and out but I can't figure out how to apply zoom to the positions.

render(int xOff, int yOff, int zoom){
    int x = xPos + xOff;
    int y = yPos + yOff;
    s->addRenderJob(texID, x, y, size * zoom, size * zoom);
}

Zooming the size of the object is simple but how can I modify the x and y positions by zoom so that the distance between the objects is also multiplied by zoom?

JakeP
  • 327
  • 3
  • 14
  • You could calculate a delta of the zoom, and then multiply coordinates by the zoom factor. I think it would be simpler if you did work with the resolution or the drawing engine rather than the actual coordinates. MVC or something. – CinchBlue Jul 09 '15 at 03:36
  • Can you explain what you mean by delta of the zoom? If zoom is originally 1 and I make it 2, delta zoom would be 1? I don't understand. – JakeP Jul 09 '15 at 03:39
  • usually when you get user inputs, there is a certain amount of "spin" given by a mouse wheel or some other input. When this occurs, the best thing might be to multiply the coordinates by the "zoom factor" or the ratio of user input to the desired effect you'd like to achieve. The delta can be the change, if your input is measured as a "scalar" or just an absolute quantity. – CinchBlue Jul 09 '15 at 03:40
  • and if you're using SDL or SFML, I believe there is indeed a way to zoom out and in based on views or something already. OpenGL probably has similar functionality within SDL or SFML. – CinchBlue Jul 09 '15 at 03:41
  • This is what I initially thought so I did the following. `int x = xPos*zoom + xOff; int y = yPos*zoom + yOff;` This however did not have the desired result. – JakeP Jul 09 '15 at 03:44
  • If you're not storing your coordinates as floats, this is one possible cause. If the zoom change is very small, you'll see no effect. – CinchBlue Jul 09 '15 at 03:48
  • They are being stored as doubles. And I don't think that's it. The change is very large. Too large. – JakeP Jul 09 '15 at 03:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82767/discussion-between-vermillionazure-and-jakep). – CinchBlue Jul 09 '15 at 03:50

2 Answers2

0

render(int xOff, int yOff, int zoom){ int x = xPos + xOff; int y = yPos + yOff; s->addRenderJob(texID, x* zoom, y* zoom, size * zoom, size * zoom); }

if necessary, you can download game engine. study how to deal with coordinate system in game.

WindMan
  • 1
  • 2
  • This is wrong. It was the first thing I tried but it doesn't work. I now have it working by finding the difference between x and the center of the screen and multiplying that by zoom. – JakeP Jul 09 '15 at 17:26
0
halfX = SCREEN_WIDTH/2
halfY = SCREEN_HEIGHT/2
deltaX = xPos - halfX;
deltaY = yPos - halfY;
x = halfX + deltaX * zoom + xOff;
y = halfY + deltaY * zoom + yOff;

Thanks to VermillionAzure for helping me work this out.

JakeP
  • 327
  • 3
  • 14