Sign in to follow this  
BoBanducci

screen/view scrolls with mouse - how?

Recommended Posts

BoBanducci    100
Hi,

I'm making a 2d, top-down game in Flash. This is the perspective.


.[img]http://i1213.photobucket.com/albums/cc466/Bo_Banducci/FullGroupnofilter.jpg[/img]

I want to program the camera to move when the mouse is detected at the edges of the screen, RTS style. However, I don't just want 4-directional - that would be easy. I'd like to have full 360 degree movement.

I assume it's going to require some trigonometry, which is why I'm posting here. I've done almost no programming that requires more than multiplication and division. Can anyone give me any tips on how to do that, or post a link to a tutorial they know?

Thanks a lot,

Bo

Share this post


Link to post
Share on other sites
wolfscaptain    200
You can take the difference between your mouse and the center of your window (which happens to be a 2d vector), normalize it, and add it to your camera's translation, multiplied by the distance you want to move every time.

Share this post


Link to post
Share on other sites
BoBanducci    100
[quote name='wolfscaptain' timestamp='1313383263' post='4849254']
You can take the difference between your mouse and the center of your window (which happens to be a 2d vector), normalize it, and add it to your camera's translation, multiplied by the distance you want to move every time.
[/quote]

Thanks wolfscaptain!

I looked up "normalizing 2d vectors". Here's the page I'm referring from: [url="http://www.fundza.com/vectors/normalize/index.html"]http://www.fundza.co...lize/index.html[/url]

I think the correct formula to find the length of the vector is:

length = sqrt( (ax*ax) + (ay*ay))

But, what is "a"? On that page, it looks like "a" is at 0,0,0. Would that be the center of the screen?

After finding the length, I divide the components (x and y) by the length, right?

Cool, I think I can do that once I know for sure what "a" is.

My next question - this is my first time using a moving camera. What is the cameras "translation"? Would that be its change in position each frame?


So, would I multiply the vector's components by the speed I want the camera to travel, and then add the components to the corresponding x,y points of the camera?

Thanks so much for the help so far.

Bo

EDIT: Oops, just reread your post, and you told me what "a" is. Hopefully I can get it working now. Thanks a ton.

Share this post


Link to post
Share on other sites
wolfscaptain    200
That article refers to ax as the first component of a, as in x (same goes to ay).
Writing a.x might be more obvious.

After you get your unit vector, you can multiply it by whatever value you want to get different effects.
For example, multiplying by 10*dt (assuming you have a variable that tracks the change of time), will move the camera 10 units per second, making smooth movement.

By translation I meant the position of your camera.

Share this post


Link to post
Share on other sites
boogyman19946    1487
a doesn't really mean anything aside from distinguishing the members x and y as belonging to vector a. its a typical pythagorean theorem. c^2=x^2+y^2 so when you solve for c, the lenght, you get the sqrt'ed function

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this