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.

Newcomers DBPro Corner / Sprites and Object Position Off Screen.

Author
Message
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 19th Sep 2011 13:20
Im trying to find a way to store the position of a sprite and/or 3d object in a top down environment.
Displaying a sprite is nice and simple, just take your screen co-ordinates and do stuff with that. But what if something was off screen, how can you store where it is and draw it only when necessary?
Basically how to create "world co-ordinates" and displaying things when needed.

Are there any good tutorials that can point me in the right direction.

In addition to this how would be best to make a sprite or object off the screen walk a reasonably complex "path" so that when it needs to be displayed it will be in the appropriate place depending on where in the path it would be at the time you would be viewing it.

This is the best i can explain this i think so if no one understands what im on about then i understand but im pretty stuck.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 19th Sep 2011 21:54 Edited at: 19th Sep 2011 21:56
Creating world coordinates is quite easy, just treat the "world" as a much larger version of the computer screen. Start by creating a pair of variables for (WorldX, WorldY) coordinates, then create and fill an array holding the ID and coordinates of every item that you want in the world.

To set up your level:


Now, say that your world and computer screen both start at (0,0), or the top left of the computer screen. If your viewpoint shifts 100 pixels to the right, the World coordinates will shift to (100,0) as the top left (even though the screen is stuck on (0,0)). Here's where you can work out what to show on screen:



This isn't too good if you have a large level with thousands of items, but then you could carve the world up to different regions so you only check sprites likely to be on screen (for example, if WorldX/Y is currently (0,0), you don't need to check any items located around (100000000,5000000))


In answer to your second question, sprites can follow a complex pathif you save a list of coordinates called "waypoints". Say your sprite is at (x1,y1) and you want it to move to (x2, y2). simply call "sprite velocity ((x2 - x1)/scale#), ((y2 - y1)/scale#)" where scale# is some float that will alter the speed (scale# = 1.0 and the sprite will complete the trip in one Sync!)

Once the sprite reaches (x2,y2) (allow a plus or minus factor, as it may not reach the coordinate exactly), set the next target as (x3,y3) by retrieving it from your list, and repeat.


3D will behave in the exact same manner if it's a topdown game, but you need to use caution as the range of 3D positions visible on screen aren't the same as screen pixels - you can use "Object in Screen(ObjID)" to see if an object is even partly visible on the screen, but it's very slow and should be used with care.

Hope this helps!

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 20th Sep 2011 10:04
Thank you for the reply.

That looks like something i did a while back to position objects on a 2d scroller but i didnt really understand the concept of what was going on so couldnt really elaborate.
Just to clarify:

1) Would WorldObjects[0].SpriteID be whatever number i was using for that sprite and so each worldobject would have its own number for whatever sprite i used so if a table was sprite 6 and a chair was sprite 2 or something then
WorldObjects[0].SpriteID = 2
WorldObjects[1].SpriteID = 2
WorldObjects[2].SpriteID = 6
Would be 3 different objects all with unique x,y positions (provided i had added that which ive not here), 2 of which would both use the chair sprite and one which used the table sprite.

2) If the number of objects was slowing down the world would i break it down by say regioning the world off into any kind of divide. So say i had a massive square. I could easily break that into 4 by doing the numbers. Then would that be represented by doing a whole new bunch of objects like so:
World1Objects[0].SpriteID = 2
World1Objects[2].SpriteID = 2
World1Objects[3].SpriteID = 6
For the first world

World2Objects[0].SpriteID = 2
World2Objects[1].SpriteID = 4
World2Objects[2].SpriteID = 7
For the second etc?

3) How would i know what the worldx and worldy co-ordinates are so its not checking for every object or something.

4) Im still a little unsure of how to move sprites around. What you are saying makes sense but i have no idea how to actually use it.

Forgive the daft questions i have been on and off coding for about a year but only really spent about 3 months total on it and have never coded anything before in my life or used any other language so i am not just converting an already known language to DBpro, i am doing everything from scratch.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 20th Sep 2011 11:17
No questions are daft if you're struggling to understand something! (Besides, I remeber what I was like when I was starting out )

1) Yes, you're right. Try and avoid the use of the word "object", though, as that implies 3D commands in DBP - "entity" or "item" would be better for your variable names.

Quote: "WorldObjects[0].SpriteID = 2
WorldObjects[1].SpriteID = 2
WorldObjects[2].SpriteID = 6"

You probably know this already, but you only need one sprite for each item (table, chair), hide it, then call Paste Sprite in your main program loop to put several copies of it on screen.

2) Not quite. You can box it off like you've suggested, but that implies each region is a "mini-world" and you can't easily move from one region to another.

Carve your world up like a chessboard, the entire board is the "world" but each square is a "sector", if you like. Then you would need:

Just fill the list of items on a sector by sector basis.

Consider a 3x2 grid (v is the sector you're currently in):
[1][2][3]
[4][5][6]
You can make each sector as large as you like - say 1000x1000 pixels. Let's say that (WorldX, WorldY) = (1200, 500), then you're in sector 2. So every item in sector 2 should be drawn. However, if you look at the attached picture you'll see that the current screen viewport (shown in red) overlaps on neighbouring sectors as well - in this case, 3, 5 and 6. So sectors 3,5,6 need checking as well to see if any objects within them are on screen at this time.

I'm not sure whether it would be faster to render everything by default, or check all four of these sectors for what is actually visible and then draw what you select - you'd have to experiment.

3) Put at the top of your code:

Then when your viewpoint shifts ("camera", but not the 3D type, slides to the right) you update the coordinates:

then just read off WorldXX and WorldY as you need them.

4) My mistake , the command is called "Move Sprite". Try the help file example:

This will take the ManWalking.bmp (or any image) and move it in the a circle by rotating then shifting the image forward one pixel in the direction it is facing.

Hope this helps!

We spend our lives chasing dreams. Dark Basic lets us catch some of them.

Attachments

Login to view attachments
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 20th Sep 2011 15:07
I think im understanding it, bit by bit. I need to put it into some code when i get back from work to see if i get it
I suppose you would have an interactable variable to see wether the object in question can be walked through or not and some other things like can it be equipped or whatnot, or perhaps that would come under a different type of user defined type im thinking.
As for not drawing things off screen would that just be Hide Sprite and Show Sprite to stop it slowing down with all the items?

2 more things:
Firstly i could do with working out collision and how to do it properly so players can not walk through certain things or interact with others like picking up items or going through doors but this is a little more than beyond me right now as i tried to do it last time and failed bigtime to understand what was actually going on, it seems to stump me every time.

Second I think i might search a little more first then put a post up regards to making people move about as i nearly get it but its still a bit much to take in so i want to get as much info or examples as i can gather and couple it with a pathfinding thread, as currently i dont even know where to begin looking at reasonable pathfinding even for something simple like a 2d topdown game, but i think i would need to know collision first as there is no point pathfinding for npcs and enemies etc if they can walk through everything anyway.

But as far as positioning objects in the world and possibly breaking it up i think i have a grasp on it so ill try it in some code as soon as i can and post back if i get into trouble.

Thanks once again for the help.

Login to post a reply

Server time is: 2024-11-24 01:24:16
Your offset time is: 2024-11-24 01:24:16