How do I bound an object to current camera view?

0 favourites
  • 15 posts
From the Asset Store
Fly Bound
$2.49 USD
Design for game, asset for game, game kit, UI design, completed design for game FLY BOUND
  • Overview: If there was such thing as tags on solids that would solve the trick, but unfortunately if I just bound the view with invisible solids then enemies can't pass through (these enemies use bullet behavior as well, so if I try deactivating the solids when the enemy reaches it it'll just bounce off). So I need an alternative way to bound my player to the current visible space... Any suggestions?

    Long story:

    So I have a game where there are two types of bullet-behaving 'enemies'. The 'square' type bounces off the 'rectangle' type enemy as follows: rectangles are solids but they don't bounce off solids, and squares aren't solids but they do bounce off solids. The square enemy is also warp-behaving while the rectangle enemies aren't. I realised that when I change the viewport (aka screen) area, I can still move my player outside the viewport, but it is bounded by the layout. I want it to be bounded by the viewport (which is configured to be always inside the layout). Unfortunately as described above, if I try to bound the viewport using invisible solids, the square type enemies will also interact with them. If I only activate them/deactivate them based on certain touch-conditions, what if both a square needs to pass through and the player is touching them? So I haven't got any ideas left, any suggestions?

    Thanks in advance!!


  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Bound your enemies or objects with events instead of using solid objects to bounce from.

    If they exit the viewport by 1 pixel then have them change direction by something like (self.angle-random(170,190))

    Is this what you want to happen?


  • Maybe I should upload the capx.file ... sx3Oa?dl=0

    So in the 'Original' layout, if you run it, click and drag to start playing. You'll notice that if your height:width ratio is not exactly 2:1 then you can move the player outside of the viewport, but not the layout. The enemies (I'll call them bullet projectiles (bp) from now) are free to move in and out of the viewport, but the player isn't supposed to be able to do that. The square bps also warp though, so setting an invisible solid to destroy the bps will affect the square bps and they won't be able to warp.

    And yes this is intended to be a mobile game, just in case you were wondering <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

  • I'll take a look... so you want to bound the player to the viewport, but not the enemy? Is that correct?


  • Yes, Thanks!

  • Your link leads me to the project folder - but there is no capx file there (only a backup).

    I'm going to suggest though without looking at it, that you can bound the player to viewport (at any size or shape) by using the method I mentioned above. I have done this for my camera object on a layout that is unbounded scrolling.

    [If player.X is less than viewportleft("layer") + (player.width/2)]

    --- Player.X set X to viewportleft("layer") + (player.width/2)

    Do this for each side of the viewport (top, bottom, right) and your character player won't be able to leave the view, but enemies are free to move around the entire layout still.


  • Ok Thanks! I'll try that

    In the meantime, how do I actually link something useful?

  • You need to save it as a single file, not as a project folder. Then you can link in dropbox like you did before, but to the file only

    Like this EXAMPLE capx that I made which bounds the player to a definable area on the viewport. You can adjust the bounds as you wish - and can even set them to an expression if you want (like [player.width/2] for example).


  • Ok I see

    I hope this works: ... .capx?dl=0

    I've PMed you the password (I just remembered this isn't a personal project)

  • That's a pretty cool idea for a game, nicely made so far as well

    I think my example should do the trick for you. I was playing with mouse though, not sure if the player is supposed to appear so far away from the mouse cursor position or not - but I found it a little disorienting to control because of the offset to the cursor. The powerup collection and the effects are nice though. It has a nice feel even when using the mouse. I had to shew my browser into a funny shape though to get everything in screen - I didn't check what scaling method you're using but it's definitely not computer friendly lol.

    The example I made will work for moving the player in any way (mouse, 8 direction, physics, etc) yet still keeping within the defined bounds (the minimum you can use is like 1, or maybe something as long as it's greater than 0).


  • Haha thanks! I've been working on this for quite a while now. In fact you can see versions from development stage under the name 'dodge' (that's what it was called back then).

    I've implemented your solution, and I'm glad to say that it works, so thank you!

    The only thing which could be better is the fact that the player renders before the new co-ordinates for the mouse mapping is set up, so when you try to move the player outside, it looks a bit like the HUD elements when resizing the browser, is this supposed to happen?

    Also, the game is set so that any finger (mouse) movements are amplified x2 (I'm going to make this sensitivity adjustable in the future). The offset doesn't have to be there, you can just release the finger (or click button) and move it around and it won't affect the player. You can then touch (click) back wherever it suits

  • I'm not sure I follow what you mean.

    I couldn't move the player outside of the viewport, even in the example you provided. It would stop at the edge.

    I assumed it was some issue related specifically when using "touch" on a mobile - and that it was working as expected on the browser preview while using mouse.

    Can you post a screenshot or gif animation of what you're referring to?


  • Did you try resizing the window and trying both the horizontal and vertical edges? You can only either exploit the vertical edge or the horizontal one at any one viewport size.

    Well basically it jumps out of the viewport for a split second before reappearing back inside.

  • Yeah I tired all edges of the screen, and it seemed OK to me. I did change the scaling mode though to letterbox mode before testing it properly - I wanted to make sure I wasn't seeing something that isn't intended, which may have been altering my experience.

    Either way, if you use a bounding method then it will never appear outside of that area no matter what method you use to move it (not noticeably anyway).

    I'm glad it worked though - it's a pretty neat (and challenging) game so far. I managed a score of 218 haha...


  • Oh I see...yes letterbox scale would definitely have altered your experience...I mean the player is bounded to layout, so if you can see all sides of the layout at once, then there wouldn't have been a problem. The problem is when you don't want the black lines around the outside of the layout to appear on a phone, so you have to use 'scale inner' for the scaling method. Then one of the viewport edges is not the layout edge anymore, and that's when you have the split second thing. But anyway I'll try to change the ordering of steps and see what I can do

    And btw, you aren't even getting the full experience... if you tap with two fingers (impossible to do on a computer though) you'll bring up the upgrades panel, so you can then actually use the coins that you've collected so far Actually, I'll make it so that pressing the space bar works as well (that's what it used to be)

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