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.

DarkBASIC Professional Discussion / 2d camera movement (following controllable character) need help please!!

Author
Message
MattyBoyIsBack
16
Years of Service
User Offline
Joined: 21st Apr 2010
Location:
Posted: 23rd Jul 2011 03:47
Hey there,

I have looked around on the forums endlessly for help with this subject and all i find is camera movement for 3d models like cubes and stuff.

What im looking for is help with coding a camera movement that follows the character as you move it. so when the left key is pressed, the character moves and so does the camera. If you want a reference then look a the suikoden 2 movement.

I have got the character to move correctly so im half way there, so if anyone has any input on what code i can use or if someone could point me in the right direction it would be greatly appriciated...thank you
Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 25th Jul 2011 09:55
Hey Matty,

The basic principle behind two dimensional scrolling is actually pretty straightforward. What you do is create two variables (I call them CamX and CamY) that represent the "camera" (which isn't really a camera at all, as you'll see). When you move your main character, use the arrowkeys, put the mouse to the edge of the window, or whatever you want to use to start scrolling, you adjust the values of these variables. As you scroll to the right, you increase CamX. Then whenever you draw anything to the screen like a tile or a monster, you subtract the value of CamX from the X coordinate you draw the object to. That way, as you scroll right, you just draw stuff further to the left, creating the scroll effect.

MattyBoyIsBack
16
Years of Service
User Offline
Joined: 21st Apr 2010
Location:
Posted: 25th Jul 2011 22:40
Agent,

Thank you very much this will be very very helpful, is it possible to do the scrolling using the arrow keys as you move the character and not the mouse. i dont want the scrolling to start when the character gets to the edge of screen. the character is to remain central to the screen and as soon as you move the scrolling starts. is that possible?
Max P
16
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 26th Jul 2011 14:49
You can use something like this:
MattyBoyIsBack
16
Years of Service
User Offline
Joined: 21st Apr 2010
Location:
Posted: 27th Jul 2011 02:00
Hey Max P,

Sorry to ask for more, but would you be able to explain the code you have given a bit more clearly. sorry but i have never used cameras and stuff before. i understand some of the code but not all of it. If it helps, i am going to include my own code for the movement of the character



Any more help you can give will be much apprieciated
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 27th Jul 2011 07:05
Max P got a good example.

If the character is always center then there's no need to move it, just scroll everything else.

Max P
16
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 27th Jul 2011 12:50 Edited at: 27th Jul 2011 13:02
I'll give a larger example with explanation

If you want to scroll the screen and let the character stay in the center, there is no

need to move the character.
So you can replace

with

To make sure the character is always in the center of the screen.
Now, if we want to let the character move to the right, we scroll the screen to the left

instead. Keeping the character in the center but still giving the impression it is

moving.
So we need to change

with something like


In this code the CamX and CamY are the x and y position of the camera.
There isn't a real camera, but we pretend there is, it will get clearer later on.

Now when we press the keys, nothing will happen yet.
So now we need to scroll the screen.
We do this by decreasing the x position of everything in the map, except the character,

with the x position of the camera, same for the y position.
This code should be in the place where you draw your map

Using this code the larger the camera x position is, meaning the camera moved to the

right, the lower the x position of the map sprites is. This will result in the sprites

being drawed more to the left.

So when you press the rightkey, the x position of the camera will increase, resulting in

the sprites scrolling to the left.

The MapSprite() in the code above should be an array holding the id, x position and y

position of all the sprites in your map.

edit
To get a better performance you can change the drawing code to something like this:
MattyBoyIsBack
16
Years of Service
User Offline
Joined: 21st Apr 2010
Location:
Posted: 27th Jul 2011 18:52
Thank you very much Max P,

I understand all the code you have given me it has really helped. The only thing i dont get is the seocnd to last code snippet about the MapSprites.



You have put a number of different variables as i can see. i also read you explination at the bottom about the MapSprite() being the variable for the sprites on the map. The only sprites i have on the map is the map itself and the character. If you could jus clear that bit up and let me now what the i stands for (eg: for i=0 and next i)...

I no it sound stupid to ask haha, iv only just started learning this code. thank for all you help so far its great
Max P
16
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 27th Jul 2011 21:28 Edited at: 27th Jul 2011 21:29
If your whole map consists of only 1 sprite you can scrap the For Next loop, But later on you will probaly have more sprites and/or enemies.
I'll just start from the begining, using the expended code



To make this code work you have to create an array holding the data of all sprites in the map (except the character).
You can do this with the next code:


What the Dim command does is creating an array using the user defined type sprite_data.
The user defined type is so you can use .id, .x and .y.

When creating a sprite for your map you should add the folowing lines


Now we have your created sprite stored in an array so we can use the data later on.

MattyBoyIsBack
16
Years of Service
User Offline
Joined: 21st Apr 2010
Location:
Posted: 28th Jul 2011 14:05
Hey Max P,

This is a lot more clearer thank you. i have put the code into my game and it doesnt come up with errors. However, nothing is actually scrolling. The only thing that happens is the character movement animation. I have all my code (with ur code) below. If you can would you have a quick look to see if i missed anything.



Thank you
Max P
16
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 28th Jul 2011 16:17 Edited at: 28th Jul 2011 16:18
Well.. you forgot the next few things:
- You forgot to add a sync in your loop, so I added it.
- There wasn't a Sync on command. (The screen will start flickering if this isn't there)
- You pasted this peace of code
in the wrong place, you pasted it in the startup function. But you have to update it every frame, so I added a new function
- You aren't adding sprites to the mapSprite() array, so it won't update anything (added a function to easely add sprites)
- I guess this one was my fault, but the inc/dec camX and the inc/dec camY were wrong, where the inc was should be a dec. (Changed it)
- Also changed the sync rate from 100 to 40.

New code:
MattyBoyIsBack
16
Years of Service
User Offline
Joined: 21st Apr 2010
Location:
Posted: 28th Jul 2011 18:13
Max P! Thank you very much!

Everything works now and i understand the code perfectly now! Couldnt of done it without you thank you!

TheCreed
MattyBoyIsBack
16
Years of Service
User Offline
Joined: 21st Apr 2010
Location:
Posted: 28th Jul 2011 20:27
Hey Max P,

Thanks again for all your help. Im now working on collisions with no success so if you can recomend and links to within the forum then that will be great. I cant seem to find a collison that will stop the character when it collides with the house. If you can point me in the right direction that would be great

Login to post a reply

Server time is: 2026-07-11 12:03:19
Your offset time is: 2026-07-11 12:03:19