Manually limit object position on canvas to integer

0 favourites
  • 7 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I've been experimenting with a custom scaling method, using fullscreen mode 'off' and wonder if there is a way to limit the coordinates of an object on the canvas to integers?

    Artefacts occur when objects are in fractional positions. Setting pixel rounding to 'on' appears to only apply when objects are moved using set position.

    When objects are moved using the 8dir behaviour for example, their canvas positions become fractional again.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • At the end of your event sheet, every tick set object position to int(self.x), int(self.y). This may have performance implications depending on how heavy your game is to begin with. But it shouldn't be too bad in normal situations.

    On the other hand, it was my understanding that pixel rounding doesn't prevent objects from existing at sub pixel locations, it just rendered them at whole pixel locations. So I wouldn't think it would cause artifacts even if the object position had a decimal. There may be another issue you are overlooking.

    Edit, from the manual on the pixel rounding property:

    Note this does not affect their actual X and Y co-ordinates, which can still be between pixels - it only affects where they are drawn on the screen.

  • Thanks, I actually just tried the int(self.x,self.y) thing and it does work to limit the objects position in the layout to integers.

    My understanding though was that the Canvas position is representative of the rendered position. So I'm not looking for a way to limit the coordinates on the layout to integers but on the actual canvas itself.

    Using Pixel Rounding 'on' I can see it working whenever set position is used. When off you can see it returning .5 values. However, it does not have the same effect when using movement behaviours.

    Am I wrong in assuming that Canvas position is representative of rendered position?

  • I'm not too familiar with the relationship between the drawing canvas coordinates and layout coordinates.

    But at least pixel rounding doesn't have anything to do with behaviors or set position. You can see in debug mode even with pixel rounding on, if make an event for example on right button pressed, set sprite position to self.x+0.11, you can still increase the position of the by decimal amounts. But the rendering engine will not attempt to draw the sprite at sub pixel locations, so you won't visually see it move until it gets rounded to the next whole pixel. This preserves crisp edges for pixel art.

    I'm assuming if your canvas position is not a whole number, pixel rounding should still prevent drawing sub pixel locations, but if the drawing canvas plugin breaks that rule then I don't know.

  • Hi oosyrag I think we might have got a bit confused :) I'm talking about the CSS canvas not the drawing canvas.

    The system expression LayerToCanvasXY should show the CSS co-ordinates above a position in layout co-ordinates for a given layer. My original understanding was that these are the coordinates that objects in the layout are rendered to in the viewport(canvas).

    I guess it boils down to not knowing if when rendering, is the canvas position the rendered position or not? When scaling the layout, I assume that that is placed over the canvas and therefore we can convert layout positions to CSS Canvas positions using that expression.

  • Ah.

    The pixel rounding setting should still overwrite whatever you see as the canvas coordinate. That is it should only attempt to draw the nearest (rounded) "full" pixels, regardless if layer coordinate or canvas coordinate is not a whole number.

    Anyways you can see what exactly pixel rounding does in this project dropbox.com/s/cdw5a1e9agxk4jy/pixelroundingexample.c3p

    Try turning it on and off to see how it behaves when pressing the right arrow.

  • That's interesting because your example shows that pixel rounding is affecting the objects movement at layout coordinates, and not the CSS canvas coordinates, which is in contrast to how it is described in the manual.

    The viewport is 9x9.

    With pixel rounding on the object only moves 1px in layout coordinates when it approaches 1px.

    With pixel rounding off the object can move to fractional positions on the layout.

    However, the CSS canvas size is much larger, depending on the scale, and the effects of pixel rounding are unclear in this example.

    I did realise what was happening though that was confusing for me before.

    In this modified example, scaling is turned off, the canvas size is set to 200x200 and there is a single red pixel. When moving this across the screen it does only move 1 px at a time, regardless of pixel rounding being on or off.

    The issue is with Windows DPI scaling. If the same example is run with 125% scaling on, every 4 layout pixels moved, the single pixel is stretched across 2 css/device pixels even if pixel rounding is on.

    It's unfortunate that pixel rounding is not able to factor in DPI scaling as it makes the feature a bit redundant on a lot of devices. It seems that pixel rounding is a pre-scaling operation, which explains why it does not appear to work when the layout is not perfectly scaled either by Construct or Windows.

    EDIT: It's clear that Windows scaling implementation is the issue. If anyone knows a way to force disable scaling in Chromium I'd be keen to know.

    Example: 1drv.ms/u/s!AkmrWgxeuxlKhIgIJuVIW8clDCyeCw

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)