Public Group

# screen/view scrolls with mouse - how?

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

## Recommended Posts

Hi,

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

.

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 on other sites
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 on other sites

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.

##### Share on other sites
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 on other sites
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

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 94
• 11
• ### Forum Statistics

• Total Topics
632973
• Total Posts
3009632
• ### Who's Online (See full list)

There are no registered users currently online

×