Archived

This topic is now archived and is closed to further replies.

2d tile engine math

This topic is 5574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

my 2d engine is coming along nicely, but i need a way to scroll the map, and objects moving around, better. currently, things just move by tile to tile. Each tile is 32x32(it can be changed), when you move, it just goes 32 pixels in whatever direction the movement occurs. i need some type of formula to be able to render the map so it scrolls when you move from one tile to the next. Please note that i want to keep the tile system, i just want it to look like its scrolling. For instance, lets say i move right. This will move the map in the opposite direction, making it appear that i went right. Now, instead of just rendering the map 32 pixels to the left from where it used to be, i would like to render maybe 8 frames during the movement. each frame being 4 pixels. 4 * 8 = 32. So we move, one frame passes, the map has moved 4 pixels. This repeats for 7 more frames, where the map stops because i stopped moving. How can i do this with the map? The next problem is rendering objects so that they appear to move with a "scroll" or "slide" instead of jittering across the screen by tile. When something moves, i want it to have the same effect as the map, 4 pixels in 8 frames, which equals 32. So if i am standing still, and a bot or NPC is moving across the screen, lets say once space left of where he was originally standing. I would not see him move from one tile to the next, but i would see him actually slide from the original tile to the new tile, 4 pixels at a time in 8 frames. How can i do this with objects? The current formula's for drawing stuff drawing the map
ix as long
iy as long
for ix = 0 to viewsizex-1 //11x11 is 0 - 10 x 0 - 10
for iy = 0 to viewsizey-1
    map(playerx + ix - halfviewsize,playery + iy - halfviewsize).render ix * 32, iy * 32
next
next
 
drawing an object
bitblt dc,((posx - playerx) + halfviewsize) * 32,
((posy - playery)+ halfviewsize) * 32, 32,32,srcdc,srcx,srcy,src_and
 
playerx and playery is where the camera is on the map, viewsizex is the number of tiles across in the render box on the screen, viewsizey is the number of tiles down in the render box on the screen. halfviewsize is the middle of the rendering area (11x11 would be 5 in the middle) srcdc,srcx,srcy,src_and isnt really important any help would be appreciated thanks, --Fireking Owner/Leader Genetics 3rd Dimension Development [edited by - fireking on September 7, 2002 8:53:14 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I have no idea why you are having so much trouble with this. I''ve implemented it just fine:

Have a player position on the map.

Translate that position to be in the middle of the screen and render the tiles.

Render your player in the middle of the screen.

Translate all of the NPCs but subtracting the upper left hand corner from it''s position and render them.

Make sence?

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
I have no idea why you are having so much trouble with this. I''ve implemented it just fine:

Have a player position on the map.

Translate that position to be in the middle of the screen and render the tiles.

Render your player in the middle of the screen.

Translate all of the NPCs but subtracting the upper left hand corner from it''s position and render them.

Make sence?


lol did you read my post?
my engine already does exactley what you said, and i even showed it in code in my post

im asking what do i need to do in order to implement sliding objects and scrolling maps, described thoroughly in my post (please read it entirely).

thanks,


--Fireking

Owner/Leader
Genetics 3rd Dimension Development

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by fireking
lol did you read my post?
my engine already does exactley what you said, and i even showed it in code in my post

im asking what do i need to do in order to implement sliding objects and scrolling maps, described thoroughly in my post (please read it entirely).

thanks,


--Fireking

Owner/Leader
Genetics 3rd Dimension Development
This is so funny. I told you that I''ve implemented this PERFECTLY in my 2d scroling engine. If you aren''t getting the idea then I would be more than happy to spell it out for you.

Yes I did read your post, the entire thing. I answered the first question which solves the second question: "How can I do this with the Map?"

The only thing you should worry about tiling is the map (and looking at your code, it seems you have no (zero) culling going on). Everything else should have it''s world coordinates stored with it''s description.

Culling:
With the map: get a worldspace RECT for the current view area based on the player''s position as the center of the screen. Then test to see if any edge of the worldspace tile in the map can be seen in the view. If it can be, then translate it to the screen position by subtracting the upper corner of the rect and blitting it to the screen.

There. Your back ground in renderered.

Next, cull the NPCs in the same fashion and blit them if they can be seen.

Blit the player to the middle of the screen.

Cull and blit anything between the player and the user.

Before you draw the next fame, get the inputs from the user and cycle the AI. The cycle should increment the positions of everything by your (damned) 4 pixels. This will slide the map back by 4 pixels because the player will still be rendered in the middle of the screen and because the view is calculated as the player in the center of the screen. The NPCs however, will be translated along with the map and it will seem as though they have moved 4 pixels.
quote:
Original post by fireking
any help would be appreciated
thanks,

Really?

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
[quote]Original post by fireking
lol did you read my post?
my engine already does exactley what you said, and i even showed it in code in my post

im asking what do i need to do in order to implement sliding objects and scrolling maps, described thoroughly in my post (please read it entirely).

thanks,


--Fireking

Owner/Leader
Genetics 3rd Dimension Development
This is so funny. I told you that I''ve implemented this PERFECTLY in my 2d scroling engine. If you aren''t getting the idea then I would be more than happy to spell it out for you.

Yes I did read your post, the entire thing. I answered the first question which solves the second question: "How can I do this with the Map?"

The only thing you should worry about tiling is the map (and looking at your code, it seems you have no (zero) culling going on). Everything else should have it''s world coordinates stored with it''s description.

Culling:
With the map: get a worldspace RECT for the current view area based on the player''s position as the center of the screen. Then test to see if any edge of the worldspace tile in the map can be seen in the view. If it can be, then translate it to the screen position by subtracting the upper corner of the rect and blitting it to the screen.

There. Your back ground in renderered.

Next, cull the NPCs in the same fashion and blit them if they can be seen.

Blit the player to the middle of the screen.

Cull and blit anything between the player and the user.

Before you draw the next fame, get the inputs from the user and cycle the AI. The cycle should increment the positions of everything by your (damned) 4 pixels. This will slide the map back by 4 pixels because the player will still be rendered in the middle of the screen and because the view is calculated as the player in the center of the screen. The NPCs however, will be translated along with the map and it will seem as though they have moved 4 pixels.
quote:
Original post by fireking
any help would be appreciated
thanks,

Really?




sorry for getting a bad start with you, i dont want to be on bad terms with you or anything

im not sure as to what you mean by culling? because my map is stored in an array, and we are rendering what can be seen by modifying the number that goes in between the () or [](in c++) for the array. So i dont have to check what can be seen, because we already know what can be seen by using the player position, and the width and height of our view(viewx or c.viewx, and viewy or c.viewy, c stands for Camera, i forget what i used). So infact, there is a rectangle, im just specifying it with a width and a height, cuz I know that 0 is the left and top, and width and height is right and bottom.

I am still struggling with this, but I beileve i have found a solution. It isnt a pretty solution thuogh. It gives everything variables destx and desty and a boolean move variable. When they are moving, destx and desty are changed accordingly, then this is used in the equation to draw them. When destx equals current x, then move is false and we have reached 32 increments from where it originally was. Now, this leads me to an effeciency question. 3d games often have a MoveTo function, called each tiem in the loop, to increment it in the coordinate space. I would like to have a moveto function in my 2d engine in a similair fasion, but just using two coords, x and y of course. How could a moveto function be implemented?

Sorry for the lack of knowledge, I just got started in making engines

Thanks for your help, REALLY



--Fireking

Owner/Leader
Genetics 3rd Dimension Development

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by fireking
im not sure as to what you mean by culling? because my map is stored in an array, and we are rendering what can be seen by modifying the number that goes in between the () or [](in c++) for the array.
Excellent, that problem is solved. I mean culling by not wasting time trying to draw tiles cannot be seen. Normally these tiles are clipped by the clipper or the view port, depending on the API and its version you are using.
quote:
Original post by fireking
I am still struggling with this, but I beileve i have found a solution. It isnt a pretty solution thuogh. It gives everything variables destx and desty and a boolean move variable. When they are moving, destx and desty are changed accordingly, then this is used in the equation to draw them. When destx equals current x, then move is false and we have reached 32 increments from where it originally was. Now, this leads me to an effeciency question. 3d games often have a MoveTo function, called each tiem in the loop, to increment it in the coordinate space. I would like to have a moveto function in my 2d engine in a similair fasion, but just using two coords, x and y of course. How could a moveto function be implemented?
I hate to get into vectors, which is what 3d games use. Instead of using a destination, use the increments that will get you to that destination, ie: the player is moving 5 to the right and 4 up. Think of your problem this way: If you are limiting your game to 30 frames per second, then you can move everyobject in the direction it wants to go 1/30th of the distance, provided it takes the object 1 second to get to where it is going. If it takes the object 1/2 a second to get to where it is going, then you increment its position by 1/15th of the distance it is traveling.

Now that you have the direction the object is moving towards, then you add in gravity. You test to see if the object is standing on a solid tile, and make him fall if he is not.

Jumping can be thought of in two ways: you can calculate the curve you want your object to follow, and turn off gravity during the jump sequence, or you can specify a path in a straight line that the jump will follow, and the gravity will curve the path for you. If you are just starting out and don''t have too much math behind you, you might want to do the first way.

Also, to determine which direction to move the player or whatever in, you could use a switching mechanism and flags. Set a flag if the player is moving right when the right arrow key is pressed, and then cycle the switch on him to move him right (1/30th of the distance or whatever). Then cycle the gravity and collision detection, then draw him.
quote:
Original post by fireking
Sorry for the lack of knowledge, I just got started in making engines
Don''t appoligize, you''ve done nothing wrong. Not knowing something is not a crime. Admitting that you don''t know something takes courage too.

Here''s a tip for doing the math/physics/design for your game engine/game. Take out a peice of paper and draw all of the objects in your world that you are focusing on, and draw their bounding rects, positioning them where they will lie. Now draw the view screen centered where the main player is and cut off what is not drawn. Then visualize what you want to do and attempt the math for it.
quote:
Original post by fireking
Thanks for your help, REALLY
--Fireking
Do you have MSVC++ 6.0 and the DirectX7 SDK? I could ship over my old testing space for this stuff...

Share this post


Link to post
Share on other sites
thanks!

i have msvc++ 6.0 yeah, and direct x 7 sdk yeah

but i think you failed to notice one thing, or i failed to mention it, the game is top down, like final fantasy, but its not a game like final fantasy...

ok anyways, i worked on scrolling today, in a vb example (i do most stuff in vb first, cuz its geared towards RAD, and when i just want to see my idea work, i put it in vb, then use the same concepts in the c++ version, but optimize it and make it more object oriented)

it worked ok, but then i had something really bad happen. You move one tile, and it scrolls perfectly, but if you hold down the move button god is it laggy. But i know why. I was doing all the tile increments in the render loop, and not in the "holding down a key" loop. So if i just put my code in the key loop, then im good to go

thanks again for all your help

now i just need to know how to build a lib, and how to include the lib in a project, so i can use my engine. Ive created a lib before, but have never been able to use it. Please remember that i want to be able to see all the functions, variables, and class of the lib. For instance, if you add windows.h, you dont get to see all the classes (they exist in intellisense complete word, but not in the class menu). I want to write the lib to where i can actually see the classes in the class menu.

thanks,


--Fireking

Owner/Leader
Genetics 3rd Dimension Development

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by fireking
thanks!

i have msvc++ 6.0 yeah, and direct x 7 sdk yeah

but i think you failed to notice one thing, or i failed to mention it, the game is top down, like final fantasy, but its not a game like final fantasy...
Well, my test space is a 2d side scroller, so it may not help you in that sence, but I believe it has alpha blending using DirectX7. I''ll send it when I get home.
quote:
Original post by fireking it worked ok, but then I had something really bad happen. You move one tile, and it scrolls perfectly, but if you hold down the move button god is it laggy. But I know why. I was doing all the tile increments in the render loop, and not in the "holding down a key" loop. So if I just put my code in the key loop, then I''m good to go
Well, you could increase the velocity by increasing the substeps. You would have to level out at a top velocity, but you are on the right track.
quote:
Original post by fireking
Now I just need to know how to build a lib, and how to include the lib in a project, so I can use my engine. I''ve created a lib before, but have never been able to use it. Please remember that I want to be able to see all the functions, variables, and class of the lib. For instance, if you add windows.h, you dont get to see all the classes (they exist in intellisense complete word, but not in the class menu). I want to write the lib to where I can actually see the classes in the class menu.

To actually see the classes that the header file includes in your class pane, goto Project->add->files and add the header file to the project. This will add the classes and structs in that header to the workspace class pane.

As for building a lib, the only way I know is to build a win32 dll that exports some symbols, and when it is compiled, it creates a lib file you can link too. Just include the header file in the manner above and add the lib to the modules line in the Project->settings->link tab.
quote:
Original post by fireking thanks,
--Fireking
No Prob

Share this post


Link to post
Share on other sites