Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (564)
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 264 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

Senior Member


Medals: 10
Exp: 1 year



« 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.

j.

-=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.

http://i.imgur.com/kvXUXrU.png  --- 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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Grunnt (21 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (34 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (108 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!