![]() |
|
Enslave the Mouse |
SkaxCo
Member #8,323
February 2007
|
Is there any good way to keep the mouse within a set area? I'm trying to make a little shooting game in which there is a limited range. However, using set_position causes the game to slow down drastically. What other method is there? |
Matthew Dalrymple
Member #7,922
October 2006
![]() |
set_mouse_range() =-----===-----===-----= |
SkaxCo
Member #8,323
February 2007
|
Is there a way to keep it inside a curved area? |
gnolam
Member #2,030
March 2002
![]() |
Check if it's outside your arbitrary bounds, and if it is, move it back with position_mouse() -- |
SkaxCo
Member #8,323
February 2007
|
I already said, that makes it lag like crazy if its on the very edges of the screen. I would upload a demo, but I don't know how. |
Matthew Dalrymple
Member #7,922
October 2006
![]() |
He said position_mouse() and I quote Quote: causes the game to slow down drastically But I'm guessing he's calling it a little bit too much. [edit] Quote: I would upload a demo, but I don't know how.
Click attachments when you write a post... <code>tags</code> [/edit] =-----===-----===-----= |
SkaxCo
Member #8,323
February 2007
|
If you can tell me how to upload a demo you could see it slow... |
Matthew Dalrymple
Member #7,922
October 2006
![]() |
Maybe get_mouse_mickeys is going to be best for your situation... I'll wait for you to post your code first though. =-----===-----===-----= |
Steve Terry
Member #1,989
March 2002
![]() |
You will need to store the mouse x and y coords by and use get_mouse_mickeys as suggested. This way you have total control over where the mouse is. You will have to draw the cursor yourself though. ___________________________________ |
The Unknown
Member #8,441
March 2007
|
Hmm... to check if the mouse is inside your circle you should use... the equation of a cirlce! (otherwise known as pythagoras<- did i spell that right?) X^2 + Y^2 = r^2 where x and y are the mouse's co-ords, and r is the radius of your circle, if they are greater than the result is greater than r^2, then the mouse is outside of your circle. And each loop through your logic, if the mouse is inside the circle, store its co-ords in some int's, when its outside the circle, set the mouse position to the last know co-ords of the mouse. That should be one quick way of keeping your mouse in a circle Now, you'll also need to offset the x and y co-ords of the mouse when you check them, since the origin of the circle would be the top left of the screen. As for a curved area, make a bigger circle, and adjust the offsets. If you have no idea what i've been rambling on about, say so. |
SkaxCo
Member #8,323
February 2007
|
Yeah, I'm good at math and geometry and stuff. So i can adjust the range each time the mouse moves...thanks, I'll see if it works. Quote: You will need to store the mouse x and y coords by and use get_mouse_mickeys as suggested. This way you have total control over where the mouse is. You will have to draw the cursor yourself though. ...what? By manually draw, do you mean have no real cursor and manually draw the sprite? |
Steve Terry
Member #1,989
March 2002
![]() |
Yes manually draw the sprite. ___________________________________ |
SkaxCo
Member #8,323
February 2007
|
This is...sort of the code; I'll comment to make it easier to understand.
|
peelemachine
Member #8,085
December 2006
|
This might be a bad way, but it could work... Before the game begins, make a 2d integer array. Set its width and height to the maximum distance you want the mouse to be able to travel. int max_distance = 200; // Radius of the circle int mouse_box[400][400]; int xd; int yd; for (int i = 0; i < 400; ++i) { for (int j = 0; j < 400; ++j) { xd = i - max_distance; yd = j - max_distance; // Check Distance mouse_box<i>[j] = (sqrt(xd * xd + yd * yd) < max_distance); } } I didn't test it, but that should create an array with the shape of a circle in "1's" and outside the circle in "0's" Now all you have to do is record where the mouse was in the previous game cycle, and check its current position against the array.
If it works, it should work fast. You're damned if you do, and you're damned if you don't. |
Billybob
Member #3,136
January 2003
|
peelemachine: Wow, overengineering.
|
peelemachine
Member #8,085
December 2006
|
Yeah you could do a distance check every time but that would be slow, hence my happy binary circle. I didn't know position_mouse() was so slow. That's stupid. Allegro's stupid. Life sucks. EDIT: You're damned if you do, and you're damned if you don't. |
gnolam
Member #2,030
March 2002
![]() |
position_mouse() shouldn't slow anything down. I'm suspecting something else is the real culprit, like poor timing (rest()?), acquire/release pairs, mixed logic and drawing, etc. -- |
The Unknown
Member #8,441
March 2007
|
Thats pretty clever peelemachine, like what i said, but you only need to do the math once. |
Leniuch
Member #5,175
October 2004
![]() |
Quote: Enslave the Mouse
{"name":"390901~Two-Mice-Behind-Bars-Posters.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/2\/f2cacff827d0c2d79f1503d6f3c4baea.jpg","w":337,"h":450,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/2\/f2cacff827d0c2d79f1503d6f3c4baea"} Easy. They don't fight back. |
|