Moving 3D shapes in a "Drag and Drop" Fashion

Hello all, new to OpenFrameworks (I’ve recreated Doodle Jump if that counts for something). Intermediate in C/C++.

I’m creating a simulation/ program where students can connect different atoms together to start a chemical reaction, thus creating a compound. EX: combining hydrogen and oxygen atoms to get water.

The basic UI of the program will consist of 2 main panels. On the right side will be a small, slim panel which will serve as an element bank. The left side will be the student’s workspace. What I’d like is for students to be able to drag and drop elements from the ‘element bank’ panel, and into their workspace so that they could connect them to form compounds.

Currently, I have the panels set up, as well as the hydrogen and oxygen atoms drawn. (I just drew spheres to represent them). I have yet to add the ofEasyCam, but the shapes are being drawn in 3D.

I’m currently working on trying to add the drag and drop functionality, but I can’t seem to be able to move the sphere at all. As mentioned above, I used OpenFrameworks to recreate Doodle Jump. To move the character in Doodle jump my code looked something similar to this:

if (key == ‘a’){
player.location.x -= 30;

This line of code was able to move the player when I was recreating DoodleJump, but it doesn’t work when i try to move the position of the sphere. I’m not sure why I can’t move the position of the sphere. Is it because i’m using the ofPoint function to store and edit the position of the sphere? Could someone take a look at my code (it’s not much, aside from the standard stuff 25 lines tops) and let me know what I’m doing wrong? Thank you !!!

P.S. If you’re trying to create a project in project generator in order to plug in the source code files in the ‘src’ folder, please put the Elements.h and .cpp files in the parent folder of the ‘src’ folder.

EDIT: Apparently I can’t upload the files, so i’ll create a Google Drive link of the files. Thanks.

Your Oxygen::draw() resets the position to a discreet value, so any adjustments you make (with the ‘a’ key) are not retained by position. Using ofPoint is fine; its a typedef for ofVec3f. There are some newer vector types and functions in oF. Have a look at the glm section of the documentation on the oF website. Also the forum has a </> button where you can copy/paste code, just in case that’s easier than the google docs route.

1 Like

Thank you TimChi!!! By moving the location = ofPoint(895, 256, 25) to the setup function instead of the draw function, by using my piece of code earlier, I am now able to move the 3d shape with the ‘a’ key (just wanted to use the ‘a’ key as a test to see if I could even move the sphere). Thank you so much!

Now I’m just working on the drag and drop functionality.

For anyone curious, this is the code to move the shapes in a drag and drop fashion. I placed it under the mouseDragged option in the ofApp.cpp file. The ‘oxygen’ is just a sphere and I used ofPoint to keep track of its location.

if (button == 0) {
		if (x >= oxygen.location.x - 75 && x <= oxygen.location.x + 75) {
			if (y >= oxygen.location.y - 75 && y <= oxygen.location.y + 75) {
				oxygen.location.x = ofGetMouseX();
				oxygen.location.y = ofGetMouseY();