Hello. Yesterday, I started messing around with LWJGL. I am currently working on a project, that I have named Polygons. One component of it will be to be able to hover over a shape, click on that shape and be able to drag the shape any where you want, then right click and be able to drop the shape. I was wondering if someone could help me do this. Thank you.
Ok this is a bit of a difficult one to visiualise but say with me.
Firstly you will need to decide which object you are selecting , so I suggest you check the layer and check the bounds. So say of instance I have a block of width and height 1 and its at coordinates (5,3) then I know that the area the mouse is able to click it will be (5,3) (5,4)(6,3)(6,4) . First problem sorted, now for movement . In order to move the object you will need to decide your rendering technique , you can use either shader based transformations or transformf , if its just a test of one object then use transformf if its a lot of objects I recomend you make a shader. There are other tutorials that expain that because this is a reply not an essay . Once you have decided and understand how to perform transformations you must then check if you are scaling an object , what I mean by this is are you drawing a 2 by 2 sprite at size 4 by 4 , this can sometimes effect the way its rendered, especially if it directly effects the verticies. So after all of that you then apply this by performing , setposition(mouse.x,mouse.y) when clicked. simples , somewhat.
P.S : aplogies for my horrific spelling im here to program not to recite shakespear :0
In 2D all you need to do is figure out where your mouse is clicking and then check to see if you clicked inside one of the objects. The easiest way is to know how big your object is and test to see if the mouse is inside it.
Simply know the center location of your object and then check to see if the mouse’s location is less than the top height, greater than the bottom height, greater than the left edge, and less than the right edge.
This assumes that the origin is in the bottom left of your window.
Then you calculate the offset of the center of your object from the mouse (center - mouse)
Then whenever you move the mouse just update the location of your object by setting it to the mouses position plus the offset.
Ok… what you want to do should (as in most things in life) be broken down into smaller steps that when combined get the result your looking for.
Step 1: is my mouse over my model.
Now you didnt say if this was a 2d or 3d game… the the concept is the same either way.
If your in a 2d game you needs a bounds checking (is my mouse x,y inside my shape)…
If your in a 3d game you need to do a raytrace or picker routine.
This is probably the hardest part… once you have this working.
Next comes what to do now that we know your over the object with your mouse.
Step2: Keyboard / mouse listener
LWJGL provides a very nice interface for building a keyboard and mouse listener.
Implement this and youll be able to write code that reacts to when a button on either peripheral is pressed.
Step 3 and 4
Now that we know we were over a object and a key was pressed change the coords of the model as we move the mouse.
This really just changing the coords your using based on more keyboard / mouse listener imputs. Thing of moving the mouse as another way of pushing buttons… you can get the motion from the same interface provided by lwjgl.
They then push a button and you stop updating the model’s coords.
Ok… so this is very very abstract… but if you tell me more about your game i can try to be more specific.
j.
So, my game is 2d. I have linked an image below of what the game looks like. You can change the shape of the… shape and its colour with some keys on your keyboard. You can also move the shape around with arrow keys.
http://i.imgur.com/kvXUXrU.png — Image of my program.
ok… so your a 2d application which makes things easier…
you have two choices on how to tell if your mouse in “inside” your shape.
option1 : treat all your shapes like rectangles.
This is a simplification… if your mouse Y is less than highest modelY and more than lowest modelY then its within the height.
If your mouse X is less than the highest modelX and more than the lowest modelX then its inside X
if your inside Y and inside X your in the “rectangle” of the shape!
Option 2: very accurate approach but more work.
For your irregular shape (like your image)
your going calculate the area of the shape. To do this take 3 points that make up the shade and find the area of that triangle.
Next repeat this step taking another 3 points where at least 1 point has never been used in previous calculations
repeat until you covered all the points.
If you add up all the area calculations you will have the area of the shape.
Next we repeat the process but now we use the mouse x,y as if it was a point in the shape.
If the x,y of the mouse is inside the shape the area sum will == our first calculation. if its outside the shape it will result in an area larger than the original sum of the areas.
This approach will give you very accurate detection.