Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Studio Chat / Wrapping A virtual world help

Author
Message
BitManip
10
Years of Service
User Offline
Joined: 20th Dec 2013
Location: Essex
Posted: 1st May 2023 20:08
Hi all and thx in advance.

I want to have a virtual world which is say, 2000 pixels wide. The viewport (lets say it is 600 wide) moves left and right. Needless to say when the view port is moving right, it will need to wrap to the start and vice versa, move left will wrap around the the end (2000).

When this happens the viewport is both at the end and start. How do i manage sprites that move independently of the viewport. I can do it for static sprites but when sprites are moving on their own path, it gets messed up?

Any ideas will be gratefully received…

Cheers
I control all the juicy in here!
Sph!nx
15
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 2nd May 2023 14:42
Set new positions by adding/removing from current values for eatch sprite.
Regards Sph!nx
BitManip
10
Years of Service
User Offline
Joined: 20th Dec 2013
Location: Essex
Posted: 2nd May 2023 15:46
Thanks Sphinx

Not that simple unfortunately! That only works when the screen(viewport) is within the virtual worlds bounds.

i.e if the virtual world is say 2000 pixels wide, and the virtual resolution is 800 wide, that only works whilst the top left of the viewport position is between 0 and 1200.

Once it moves beyond those bounds, the beginning of the virtual world and the end of the virtual world are both within the viewport.

With the above method only, sprites 'magically' appear mid screen as they wrap around.

Can can get it to work for static and moving objects, but the issue occurs when I have a wave of say 6 sprites (think bidirectional 2d shmup) and the viewport/camera is moved out of this bounds mid formation.

Just cannot get my head wrapped around it?
I control all the juicy in here!
hendron
8
Years of Service
User Offline
Joined: 20th Dec 2015
Location:
Posted: 2nd May 2023 16:32
Instead of setting your wrap boundary from 0 to world width, try setting your wrap boundary start to the viewport X offset and your wrap boundary end to the viewport X offset + world width
BitManip
10
Years of Service
User Offline
Joined: 20th Dec 2013
Location: Essex
Posted: 3rd May 2023 07:53
Hendron,

Many thanks. That sounds good. I will try that today!
I control all the juicy in here!
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 4th May 2023 12:05 Edited at: 4th May 2023 12:07
if you find a solution, please share? if it can be whittled down to a simple snippet (which i think is doable), please post a "[AGK] 2D Wrapping World" offering to Code Snippets for easy reference as i'm sure there are many of us who want to remake Defender or the like but also struggle with the notion.

otherwise, i have an idea on how i'd go about it but if you beat me to it (or validate the theory), i'd appreciate it
Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 4th May 2023 23:02 Edited at: 4th May 2023 23:06
If you're trying to do a Defender clone then this could help. Use the image I've included, I know it's crap but it shows the concept. First you draw it in Paint.NET or what ever package you prefer, then double the canvas width then copy/paste the same image next to the first one.




Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 4th May 2023 23:12
Here's a two-way version. Use the same image from my previous comment.


blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 5th May 2023 02:46
You could use UVs as well;
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 5th May 2023 03:09 Edited at: 5th May 2023 03:47
i think you guys are missing it:
Quote: "How do i manage sprites that move independently of the viewport."

where i don't believe the OP is talking about background sprites but, say, Defender™ enemies that are at the far right of the 2000px wide "world" which would wrap left of 0 when the player is nearing 0 and vice versa.

otherwise, whether they are static sprites (apart from the background and could be interacted with/changed) or dynamic (enemies, etc), their "OriginX" values are recorded (or adjusted if dynamic).

the idea is, and correct me if i'm wrong, BitManip, if the low end of the view is < 0 then it would set the sprites that would be visible from the far right @ SpriteX-2000 to put them in view (left of 0).
same for when the player is nearing 2000. get the sprites from the far left that would be visible (from the other end of the world) and wrap them with SpriteX+2000.

the theoiry is, all the while, their OriginXs should never wrap, only (potentially) their placement at either ends of the world.

sounds simple but i'm having a horrible time getting it to work. i started too complex and am tightening it up... but:

some code that's working toward the theory:

where BufferX# = 1/2 the width of the viewport# + 1/2 the width of the widest sprite in the world (i'm setting sprites with SetSpritePositionByOffset()).

when the player (Player.ID) is within the bufferX# at either end, Low# will be larger than High#.

those (Wrapped) values (Low#, High#) can be used to determine which sprites at the other end of the world should be wrapped (by comparing them to their OriginXs) to determine which should be Set @ ±2000) .

when the view isn't into the buffered ends of the world, Sprites that were wrapped (Set < or > 2000) should be placed back at their OriginXs.

i should mention, i'm doing this without SetViewOffset() where i don't believe it's necessary using this suggested method. if you are bent on SetVIewOffset, i remember a post by Jack who offered some code for Split Screen 2-player views that would probably be helpful (i just can't find it ATM).
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 5th May 2023 06:39 Edited at: 5th May 2023 08:46
Hopefully this will do the trick.

NOTE THAT MAX_WIDTH MUST BE A MULTIPLE OF THE SCREEN WIDTH

UPDATE: Added a world map

Attachments

Login to view attachments
BitManip
10
Years of Service
User Offline
Joined: 20th Dec 2013
Location: Essex
Posted: 5th May 2023 09:32 Edited at: 5th May 2023 09:33
Hi all and thank you soooo much for the input. Wrapping the terrain (for me a tiled map) is doable as they are constant position for each tile.

I am working with enemy wave formations, i.e 3-8 enemies following the same path and flying say (left) one after the other.

I can get them to wrap and work when there is wrapping occuring and when there is not.

The mess starts when the viewport (screen) crossed the boundary mid formation. So, i.e objects 1-3 move past the beginning of the virtual world before the viewport wraps (say CameraX# = 0.0), but then the rest are managed with the camerax# moves left and hence starts at the other end (say world is 2000 wide, CameraX# moves before zero and wraps to 1997...1996. so the viewport is split.
I control all the juicy in here!
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 5th May 2023 10:35
Did you look at the project I posted?
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 5th May 2023 12:50 Edited at: 5th May 2023 14:16
woke up in the middle of the night with a solution similar to blink's tho not as pretty
WASD + [Space] to Reset

this moves some sprites unnecessarily which is what i was trying to avoid in early rounds (and it also uses SetViewOffset() which i forgot i was using all along).

anyway, it might be nice to add this functionality to THIS

re-reading your posts, BitManip, and i feel blink and i have only caught up to what you already have (this is why we should include code that shows the current start of the project so that we all start on the same page when looking for a solution)?

if i'm right, i've no time to implement now but when you're spawning groups, i'd mark 1 as the leader and the rest of the group spawns at a given offset (wrapped) from the leader's origin with those offsets recorded for future reference.

then, as the leader moves, the rest move at the same/similar rate based on their recorded offset from the leader. the wrapping (in my example) should still bring them into view when appropriate.

i'll implement groups and offsets when i get home from work this afternoon if nobody beats me to it and if this is what you're looking for. let us know?
BitManip
10
Years of Service
User Offline
Joined: 20th Dec 2013
Location: Essex
Posted: 5th May 2023 19:48
Blink0k,

Thank you for your thoughts and example code. Had a quick look and seems closer to it than I have got thus far. I need to go through the code to fully understand it over the weekend. I quickly added some npcs in a sine wave formation (will post over the weekend). It works but there is a threshold in the visible area where my formation judders slightly (enough to see) when the player is near the extremes of the map. I want to find this point as this is the ************ (put in swear words here) that has been my issue. However, yours works best so far so thank you.
I control all the juicy in here!
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 6th May 2023 02:34 Edited at: 6th May 2023 03:19
I cannot help without seeing the code.

UPDATE : Scaled the icons in the map to match the size of map

I have isolated the NPC movement to a function and reduced the "window" where it positions the NPC to be only when it is visible on screen.

There was an issue with positioning the player icon which is fixed

See the attached project

Attachments

Login to view attachments
BitManip
10
Years of Service
User Offline
Joined: 20th Dec 2013
Location: Essex
Posted: 6th May 2023 09:33
Blink0k

You are on fire. Will take a look later today and post back anything I find. Once again, thank you for your help and a different way of looking at a problem.
I control all the juicy in here!
BitManip
10
Years of Service
User Offline
Joined: 20th Dec 2013
Location: Essex
Posted: 6th May 2023 12:42 Edited at: 6th May 2023 12:43
BlinkOk

I have added a very simple formation or sprites that move in formation at the same y position. If you see the video, it all works well until (near the end of the video) where the player is near the extent of the map ~(happens at both ends!) where the x positions of the formation are 'adjusted' which is very visible and unwanted. This is what I kept coming up against with my efforts!

I control all the juicy in here!
BitManip
10
Years of Service
User Offline
Joined: 20th Dec 2013
Location: Essex
Posted: 6th May 2023 12:48
Previous post did not have my attachments for some reason....Added here
I control all the juicy in here!

Attachments

Login to view attachments
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 6th May 2023 23:11 Edited at: 6th May 2023 23:17
Change the code that checks if the sprite is beyond the start or end of the world in MoveFormation() to


My Advice is you separate out the movement of sprites from the rendering of the sprites so you would have something like
MoveFormation(...)
RenderSprites(...)

Login to post a reply

Server time is: 2024-04-25 06:36:42
Your offset time is: 2024-04-25 06:36:42