screen/view scrolls with mouse - how?

Started by
3 comments, last by boogyman19946 12 years, 8 months ago
Hi,

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


.FullGroupnofilter.jpg

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
Advertisement
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.

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.


Thanks wolfscaptain!

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

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.
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.
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

Yo dawg, don't even trip.

This topic is closed to new replies.

Advertisement