Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LWJGL drag shape with mouse - How would I do it?  (Read 2189 times)
0 Members and 1 Guest are viewing this topic.
Offline aqdawg

Junior Newbie

Exp: 10-12 months

Im just a guy you shouldn't care about

« Posted 2014-08-20 16:56:01 »

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. Smiley
Offline lcass
« Reply #1 - Posted 2014-08-20 17:18:00 »

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 Smiley . 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
Offline thedanisaur

JGO Knight

Medals: 59

« Reply #2 - Posted 2014-08-20 17:20:43 »

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.

Every village needs an idiot Cool
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jmguillemette
« Reply #3 - Posted 2014-08-20 17:59:06 »

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.


-=Like a post.. give the author a medal!=-
Offline aqdawg

Junior Newbie

Exp: 10-12 months

Im just a guy you shouldn't care about

« Reply #4 - Posted 2014-08-20 18:34:16 »

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.  --- Image of my program.
Offline jmguillemette
« Reply #5 - Posted 2014-08-20 19:13:03 »

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.


-=Like a post.. give the author a medal!=-
Pages: [1]
  ignore  |  Print  

EgonOlsen (60 views)
2018-06-10 19:43:48

EgonOlsen (42 views)
2018-06-10 19:43:44

EgonOlsen (61 views)
2018-06-10 19:43:20

DesertCoockie (241 views)
2018-05-13 18:23:11

nelsongames (142 views)
2018-04-24 18:15:36

nelsongames (141 views)
2018-04-24 18:14:32

ivj94 (883 views)
2018-03-24 14:47:39

ivj94 (144 views)
2018-03-24 14:46:31

ivj94 (795 views)
2018-03-24 14:43:53

Solater (159 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!