Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Game programming

Drag and Drop a 3d object  [SOLVED]

All about the StoneScript

Drag and Drop a 3d object

Postby arthur » 05 Feb 2012, 05:21

Hello everyone!

I am trying to implement a ai code to drag and drop a 3d object in the scene. I did search in forums and didnt find anything. So, how can i do this?

I started trying to catch the mouseButtonDown, Up and Move in this way (my object has control var called "bMouseDown"):
Code: Select all
function aiMain.onMouseButtonDown ( nButton, nPointX, nPointY, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ )
--------------------------------------------------------------------------------
   
   --
   -- Write your code here, using 'this' as current AI instance.
   -- This handler is called when a mouse button is pressed.
   --
   local hScene = application.getCurrentUserScene ( )
    local hHitObject = scene.getFirstHitSensor ( hScene, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ, 1000 )
   
    if( hHitObject ) then
        object.setAIVariable ( hHitObject, "aiBall", "bMouseDown", true )
        log.message ( "Setted bMouseDown true for ", hHitObject )
    end
--------------------------------------------------------------------------------
end

Code: Select all
function aiMain.onMouseButtonUp ( nButton, nPointX, nPointY, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ )
--------------------------------------------------------------------------------
   
   --
   -- Write your code here, using 'this' as current AI instance.
   -- This handler is called when a mouse button is released.
   --
   local hScene = application.getCurrentUserScene ( )
    local hHitObject = scene.getFirstHitSensor ( hScene, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ, 1000 )
   
    if( hHitObject ) then
        object.setAIVariable ( hHitObject, "aiBall", "bMouseDown", true )
        log.message ( "Setted bMouseDown false for ", hHitObject )
    end
--------------------------------------------------------------------------------
end

Code: Select all
function aiMain.onMouseMove ( nPointX, nPointY, nDeltaX, nDeltaY, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ )
--------------------------------------------------------------------------------
   
   --
   -- Write your code here, using 'this' as current AI instance.
   -- This handler is called when the mouse is moved.
   --
   local hScene = application.getCurrentUserScene ( )
    local hHitObject = scene.getFirstHitSensor ( hScene, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ, 1000 )
   
    if( hHitObject ) then
        if( object.getAIVariable ( hHitObject, "aiBall", "bMouseDown" ) ) then
            object.translate ( hHitObject, nRayPntX, nRayPntY, nRayPntZ, object.kGlobalSpace )
            log.message ( "Moved ", hHitObject )
        end
    end
--------------------------------------------------------------------------------
end


I dont know which arguments use in "object.translate" function to put the object on position where the mouse its pointing. :?

Please, someone could help me with this??

Thanks in advance,
Arthur
arthur
Fresh Boarder
Fresh Boarder
 
Posts: 7

Re: Drag and Drop a 3d object

Postby dbladez » 05 Feb 2012, 07:39

When I currently just did this in a next part in one of my games.

Although for me It was a 3d object that wouldn't be moving on the Z axis with movement. So I just used the nDeltaX, nDeltaY of the onMouseMove arguments then you just multiply those by 30 or so since they are only like .1 values and translating and object by .1 is not very far.

Problem with if you plan on moving with the Z with the mouse, I'm not exactly sure how you would know how the Z depth of your mouse you would want would be.

I think, I could be wrong. That the nRayPntZ is probably the Z coordinates of your camera. So the each little movement of the mouse, your translating your object that distance on the Z plane.

I think a good idea for you to test and see what exactly is happening.
Is setup a log.message and display the
nPointX, nPointY, nDeltaX, nDeltaY, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ
Values so you can get a feel for what is happening.

Also I had to use a few booleans, once the object was moving I had a bIfMoving, bIfSelected, bIfMousedown, then also had a Handle to the object itself that I had selected so it was easy to Handle. No pun intended, well maybe a little one.

So all in all, if the object is not moving on the Z plane.
I would suggest to just use the nDeltaX, nDeltaY,
then you just get the x, y, z translation of your object, then setTranslation x + DeltaX, y + nDeltaY and z + 0.

Or translateTo with frametime and smoothFactor as the F to make it smoother if you wish.


Hopefully that helps a bit, or brings up more questions I guess.
User avatar
dbladez
Platinum Boarder
Platinum Boarder
 
Posts: 665
Location: Ontario, Canada

Re: Drag and Drop a 3d object

Postby arthur » 05 Feb 2012, 12:53

Thank you dbladez!
Helped a bit, but unfortunely i need translate to z-axis too.
I think that i can do some calculation with nDeltaX and nDeltaY using current object position to give it a Z-Position. But i never did this before, so i need help with it! Someone? :?:
arthur
Fresh Boarder
Fresh Boarder
 
Posts: 7

Re: Drag and Drop a 3d object

Postby michax » 05 Feb 2012, 14:14

Hi,

Could you provide some info about what for this drag and drop will be used.
I could think of two use cases:
- drag and drop on the floor. As in RTS games. But for that x,y solution should be enought. If you want to change Z, create plane with visible false and detect ray colision with it. And if needed change distance from camera.

- Drag and drop in FPS, TPP view. You have draged item in front of you. For that i would create plane that is always looking at the players head. Of course visible set to false.
Test ray collision shooted from player head onto our helper plane.
User avatar
michax
Platinum Boarder
Platinum Boarder
 
Posts: 450
Location: Poland

Re: Drag and Drop a 3d object

Postby arthur » 05 Feb 2012, 22:16

Hi michax, thank you for the reply!

I am testing a concept game that the player manipulate objects on the scene, in first person view, so i think that is your second case.

Could you give me some example code? Because i am beginning with shiva 3d and game development... :|

THanks in advance,
Arthur
arthur
Fresh Boarder
Fresh Boarder
 
Posts: 7

Re: Drag and Drop a 3d object  [SOLVED]

Postby TRANE23 » 09 Feb 2012, 21:06

Drag n drop a 3d object with 2D mouse coordinates does not mean anything, you need some constraints(with Z=0 for example).
The easiest way to add constraint is using shiva sensors or colliders. You clic on a surface using raycasting, a plane for example, detect the surface 's 3d point clicked by the mouse and to translate your object(your ball) to this 3d point
So you can drag n drop your ball on the plane or anything else.

Code: Select all
onMouseMove
local hHitPlan, nHitDist,nID = scene.getFirstHitSensor( s, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ, this.nRayLength ( ))
    if(hHitPlan)
   then
      local x, y, z   = math.vectorScale  ( nRayDirX, nRayDirY, nRayDirZ, nHitDist )
      x, y, z         = math.vectorAdd    ( nRayPntX, nRayPntY, nRayPntZ, x, y, z  )
      object.setTranslation(myball,x,y,z,object.kGlobalSpace)


Same thing with collider(scene.getFirstHitCollider) and you can use the shape you want.
:wink:
TRANE23
Senior Boarder
Senior Boarder
 
Posts: 53
Location: France


Return to Game programming