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?
set_mouse_range()
Do you mean that?
Is there a way to keep it inside a curved area?
Check if it's outside your arbitrary bounds, and if it is, move it back with position_mouse()
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.
He said position_mouse() and I quote
causes the game to slow down drastically
But I'm guessing he's calling it a little bit too much.
[edit]
I would upload a demo, but I don't know how.
Click attachments when you write a post...
Or copy and paste your code inside some
<code>tags</code>
[/edit]
If you can tell me how to upload a demo you could see it slow...
Maybe get_mouse_mickeys is going to be best for your situation... I'll wait for you to post your code first though.
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.
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.
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.
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?
Yes manually draw the sprite.
This is...sort of the code; I'll comment to make it easier to understand.
Note: buffy is my buffer. This also draws a line to the point at the mouse and uses trigonometry. PlayerX is the location of the player, the Y always equals 480.
1 | void aiming(int mousex, int mousey, int playerx, int weprange){ |
2 | double dx, dy, dr, angle; |
3 | int lr, pointx, pointy; |
4 | dx = mousex - playerx; |
5 | if(dx < 0){ |
6 | lr = -1; |
7 | dx *= -1; |
8 | } |
9 | else |
10 | lr = 1; |
11 | dy = FIELD_H - mousey; |
12 | (dy < 0)?(dy = 1):(dy = dy); |
13 | (dy > FIELD_H)?(dy = 480):(dy = dy); |
14 | dr = sqrt((dx*dx)+(dy*dy)); |
15 | angle = atan(dy/dx); |
16 | if(dr > weprange){ |
17 | dr = weprange; |
18 | dx = dr * cos(angle/**PI/180*/); |
19 | dy = dr * sin(angle/**PI/180*/); |
20 | pointy = (int)(FIELD_H - dy); |
21 | pointx = (int)(playerx + (dx*lr)); |
22 | set_mouse_range(playerx, 480, pointx, pointy); |
23 | //position_mouse(pointx, pointy); THIS cAUSES LAG |
24 | } |
25 | pointy = (int)(FIELD_H - dy); |
26 | pointx = (int)(playerx + (dx*lr)); |
27 | line(buffy, playerx, 480, pointx, pointy, AIMING); |
28 | } |
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.
1 | |
2 | // These 2 variables should be declared at the beginning of the program |
3 | int previous_mouse_x; |
4 | int previous_mouse_y; |
5 | |
6 | // Put this at the bottom of your game cycle: |
7 | |
8 | // If the mouse is not in the circle |
9 | if (mouse_box[mouse_x][mouse_y] == 0) { |
10 | |
11 | // Put it back to wherever it was before |
12 | position_mouse(previous_mouse_x, previous_mouse_y); |
13 | |
14 | } else { |
15 | previous_mouse_x = mouse_x; |
16 | previous_mouse_y = mouse_y; |
17 | } |
If it works, it should work fast.
peelemachine: Wow, overengineering.
The slow down is in the position_mouse function, not in the checking function. Distance check will be similarly effective.
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:
To the OP:
What if you made the cursor disappear? Then you could just not draw it, and not process any clicks or movement. I don't know what kind of game you're making though.
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.
Thats pretty clever peelemachine, like what i said, but you only need to do the math once.
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.