Click & drag game

How can i achieve the following action?

  1. Pick objects (say sphere’s) present on one part of the screen (mouse sensors)

  2. Place these objects on some other part of the 3d screen.

It seems like a very simple gameLogic…Pick & drag to another place…but sorry to say i cant seem to figure it out…

Just like icons on our desktop can be dragged from one place to another.
Any help plz?

Yeah, I also don’t know how to do that. The thing I wanted t do it when you pick something up, to drag it with your mouse to another place, and on release the place where you dropped does an action like constructing or whatever.

I thought Saluk did something similar in Cresent Dawn, a game he’s working on. maybe he could help. But I also want to know how. :stuck_out_tongue:

With some python it is not so dificult to drag things around in the scene. But the aidea of how to make it is dificult…

i’m sure there wud be some Blender Guru who would know how to do this…

Hmm, this sounds like it should be simple enough, only I’m pretty sure the mouse position is in 2D.

Maybe with a a ray sensor and tracking you could keep the object at a distance from you while still “dragging” it with with the mouse.

Let me grab a coffee and see what I can mash together…

EDIT-------------

Well I managed to get one to work, but only with the camera with LocX=0. In this way the mouse position in X and Y can be used.

Here’s the code:

#MouseMove
#RageTheSage 08/04/2004

import GameLogic

import Rasterizer

width = float(Rasterizer.getWindowWidth())
height = float(Rasterizer.getWindowHeight())

aspecty= height/55
aspectx=width/60


c=GameLogic.getCurrentController()

senGrabbed = c.getSensor("grabbed")
senClick = c.getSensor("click")
senMove = c.getSensor("move")
senOver = c.getSensor("over")
actCon = c.getActuator("con")

owner= c.getOwner()


owner.mousex = (senMove.getXPosition()-width/2)/aspectx
owner.mousey = -(senMove.getYPosition()-height/2)/aspecty

if senOver.isPositive():
	
	if senClick.isPositive():
		GameLogic.addActiveActuator(actCon,1)
		owner.grab = 50
	else:
		owner.grab = 0
		GameLogic.addActiveActuator(actCon,0)
	
if senGrabbed.isPositive():
	owner.setPosition([owner.mousex,owner.mousey,5])

Rasterizer.showMouse(1)

And here is a zipped blend file showing it in use

www.ragethesage.com/mousemove.zip

Left Mouse Button grabs sphere
Hold Left Button and move mouse to move
Release left button to drop.

I’m sure there is a way to xfer 2d coordinates into 3d, this would allow more camera freedom…I can’t figure it out though, so this is the best I can do.

Hope it helps, or can at least get you started.

We tried your code , the mouse cursor is visible but we cannot move the object with the help of the mouse.

I had to fiddle with it quite a bit…I’d suggest downloading the demo file to see how the code is used.

A couple of small tips,

-“Mouse Over” only works when looking through a camera

-To remove some perspective, set your camera lens to a high value(I used 250). Make sure to increase the clip end distance too, since you’ll need to move the camera back quite a bit.

Check out the blend file, and if you have any questions feel free to ask : )

I have looked at the original gameblender documentation and various mouse move scripts. I have NEVER understood what was the purpose of using and aspect ratio. Can somone explain this to me?

thanks for the script!

The reason is when getting the mouse position, it is given in pixels based on the window size. Upper left being 0,0.

These values need to be converted in order to be used globally.

In blender, attach this script to the default plane with a mouse movement sensor and give it two properties, x & y. Hit the debug button. When you start the game you’ll see the co-ordinates displayed


import GameLogic
import Rasterizer

c=GameLogic.getCurrentController()
owner= c.getOwner()
sensor=c.getSensor("sensor")

owner.x=sensor.getXPosition()
owner.y=sensor.getYPosition()

Rasterizer.showMouse(1)

Hope that explains why the Aspect is required for mouse scripts

By “used globally” do you mean converted to blenderunits??.

Thanks for the code it explained mouse coordinates at least

[quote=“RageTheSage”"]

Yes, by globally I meant in Blender units

supr, cool :stuck_out_tongue:
I think it helps
thx jm

we have downloaded your file. The Ball is moving perfectly with the drag of the mouse. But when I try to keep it down , It does not happen so. It seems the ‘Y’ axis movement is locked.Is there any solution for this problem.

Yes , you can rewrite script as that if you for example hold RMB the script will move object in “Y” direction.



#MouseMove
#RageTheSage 08/04/2004
import GameLogic
import Rasterizer

width = float(Rasterizer.getWindowWidth())
height = float(Rasterizer.getWindowHeight())

aspecty= height/55
aspectx=width/60

c=GameLogic.getCurrentController()

senGrabbed = c.getSensor("grabbed")
senClick = c.getSensor("click")
senMove = c.getSensor("move")
senOver = c.getSensor("over")
actCon = c.getActuator("con")

owner= c.getOwner()

owner.mousex = (senMove.getXPosition()-width/2)/aspectx
owner.mousey = -(senMove.getYPosition()-height/2)/aspecty

if senOver.isPositive():
   
   if senClick.isPositive():
      GameLogic.addActiveActuator(actCon,1)
      owner.grab = 50
   else:
      owner.grab = 0
      GameLogic.addActiveActuator(actCon,0)
   
if senGrabbed.isPositive():
   owner.setPosition([owner.mousex,owner.mousey,5])

if c.getSensor("RMB").isPositive():   <<<<<<HERE <<<<<<<
   owner.setPosition([owner.XPosition(),owner.YPosition(),owner.mousey])  <<<<<< HERE <<<<

(the last two rows)
I only simply rewrite old script, so this is only idea.

jm

It’s been awhile since I came up with the script, but I’ll see if I can help.

When I initially wrote it, I used 5 as my z height to keep the ball at while dragging.

if senGrabbed.isPositive():
owner.setPosition([owner.mousex,owner.mousey,5])

This was appropriate for my demo file, but may not be for your situation.

Try changing this value, it may be that some other set value may work for you.

If not, I think it would be possible to add a ray sensor to the sphere that would “look down” and maintain a set distance from any objects in the scene. In this way you could have it float above the ground, or even move right near it.

If you need a hand, just ask and I’ll see what I can come up with.

youngbatcat just recently brought up the topic again. Hehe, this script seems to be pretty cool. I want to see the .zip file :smiley: . :-? Too bad the link is down.

RageTheSage, do you think you can post up another link to the file? Or if anyone else had downloaded the file before, can one of you guys post up a link?

Thanks.

Jason Lin

Hey, is there a way to do this that would allow the user to control the camera angle (think swing it around and still have the same functionality)
~coheed

Been awhile!

Sorry for the delay, thought this topic was long dead.

goldentaiji-I checked the link, it’s working again. Not sure why it wasn’t before…

Coheed-Unfortunately no, you can’t rotate the camera with the little script I wrote. I cheated to make it look isometric, but if you check the file, the camera is actually facing straight(pointing down also) and the ‘world’ is rotated 45º.

If you have a small enough ‘room’ or area you’re working in, you could always rotate it, to give the feel of a orbiting camera.

The solution I came up with isn’t too flexible, since it’s basically an illusion.