Game Development Community

Move character to mouse click

by Chris Bowles · in Technical Issues · 09/02/2007 (11:44 am) · 23 replies

I'm trying to modify Torque so that I can click on the terrain with the mouse, and the character will move to the point at which the mouse was clicked.

Has anyone accomplished this?

I've implemented both pieces of code described here:

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2173
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=3645

However, this has just given me the option to toggle to mouse movement (pressing M) which then rotates the character around as I move the mouse.

I'd like to be able to click any piece of terrain, and have the character walk directly to that point.

Any help is appreciated! :)
Page «Previous 1 2
#1
09/03/2007 (3:15 am)
If anyone's got any advice whatsoever it would be really useful!
#2
09/03/2007 (4:32 am)
Well here is some short pseudo code that should get you thinking in the right direction:

1) Get the world coordinates of where the mouse is clicked. (could be broken into multiple steps)
2) Make the player move to that spot in the world.

This is just a very simple way of doing it. You would have to get more complex and have the player using some kind of path finding in the end. Otherwise you would get the player running into objects and trying to walk through them.

Hope this helps.
#3
09/03/2007 (5:16 am)
Thanks - that does help :)

I think I can use this resource to actually work out the vector:

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4146 ("Click and Pick")

However, I didn't appreciate it would be so complicated to actually get the character to move to that point. I appreciate what you're saying though - it will be a bit of a challenge to get it to navigate around obsticles successfully.
#4
09/04/2007 (1:49 am)
Looks like the click and pick won't patch on 1.5 - and the RTS resource won't work either! :(
#5
09/04/2007 (7:04 am)
Chris I suggest purchasing the afx coretech kit, it has a player targeting system. After purchasing this pack I was easily able to point and click and direct an aiplayer to that spot.

Just like you I spent a ton of time trying to figure it out with free resources, the afx kit made that feature really easy.
#6
09/04/2007 (7:34 am)
Thanks for your advice :)

I did look at the AFX pack but really had no idea that it could do what I needed - it just seemed to be focused around graphical effects rather than anything else.

I'll check it out.
#7
09/04/2007 (6:25 pm)
Chris I thought the *exact* same thing until I more or less posted the same question you did and got the answer I am giving you now. In addition to all of the cool effects you can do with it it has a targeting system. If it's pointing and moving aiplayers you want afx will easily help you do this.
#8
09/05/2007 (12:56 am)
Thanks Steve,

I hope you don't mind if I ask you another question :)

I've purchased the AFX pack and am busily stripping away all the spells stuff from the GUI of the demo app, so that i'm just left with the object selection stuff.

In your opinion, should I then be looking to move the main character with the mouse clicks - or rather making the player character invisible, and moving some AI player with mouse clicks?
#9
09/05/2007 (1:42 pm)
It depends Chris, what are trying to do or imitate? And fyi by default afx doesn't let you select your own player, there is a global switch set to false.
#10
09/06/2007 (5:07 am)
Basically trying to get a point and click style interface going. I guess i'm trying to imitate the broken sword style of interface - where the camera is not fixed to the player, allowing the player to roam free within the scene, moving to where the mouse is clicked.
#11
09/06/2007 (5:41 am)
Chris look up at this post in the AFX forums

http://www.garagegames.com/mg/forums/result.thread.php?qt=59653

Looks towards the bottom and you will see more basically asking the same question and you will see Jeff's answer, including where to look for the code. If you need further help feel free to ask but with that I was pretty easily able to point and click my aiplayer anywhere.
#12
09/06/2007 (8:50 am)
That sounds just what i'm after. Thanks for all your help :)

I can't promise no more questions but this will certainly keep me busy for a while ;)
#13
09/06/2007 (12:25 pm)
I'm glad to help out when I can since there are a bunch of people that have answered my questions when I'm stuck. Good luck!
#14
09/09/2007 (5:07 am)
I've been battling with this for a while now, but I can't seem to override the existing actions that happen when the user clicks the mouse at a location on screen. Essentially i've been trying to hook into the onMouseDown to relay the co-ordinates around - but to no avail.

Could you clue me in on how you managed it?
#15
09/09/2007 (6:59 am)
Override what actions? This can all be done in script.

Under mouse_btn1 in default.bind.cs -

if (%val)
{
%coord = Playgui.getMouse3dPos();
%vec = Playgui.getMouse3dVec();

%sel_obj = ServerConnection.getSelectedObj();
if (%sel_obj != -1)
%sel_obj_ghost = ServerConnection.GetGhostIndex(%sel_obj);
else
%sel_obj_ghost = -1;

if(%sel_obj_ghost != -1)
{
commandToServer('Target', %vec, %coord, %sel_obj_ghost);
}

cursorOff();
}

Then under commands.cs on the server (script can be called anything really)

//-----------------------------------------------------------------------------
// object selection additions
//-----------------------------------------------------------------------------
function serverCmdTarget(%client, %mouseVec, %cameraPoint, %ghost)
{
//Determine how far should the picking ray extend into the world?
%selectRange = 200;

// scale mouseVec to the range the player is able to select with mouse
%mouseScaled = VectorScale(%mouseVec, %selectRange);

// cameraPoint = the world position of the camera
// rangeEnd = camera point + length of selectable range
%rangeEnd = VectorAdd(%cameraPoint, %mouseScaled);
%FirstSearchMask = $TypeMasks::InteriorObjectType;
%SecondSearchMask = $TypeMasks::TerrainObjectType;

// Search for objects within the range that fit the masks above. If we are
// in first person mode, we make sure player is not selectable by setting
// fourth parameter (exempt from collisions) when calling ContainerRayCast

$obstruction = ContainerRayCast (%cameraPoint, %rangeEnd, %FirstSearchMask);

if(!$obstruction)
{
%scanTarg = ContainerRayCast (%cameraPoint, %rangeEnd, %SecondSearchMask);
}
// a target in range was found so select it

if (%scanTarg)
{
%targetObject = getWords(%scanTarg,1,3);

%target = %client.ResolveGhost(%ghost);

AIManager.MovePlayer(%target, %targetobject);

}
}

Then in my aimanager script file (aimanager is a scriptobjevt)

function AIManager::MovePlayer(%this, %aiplayer, %moveto)
{

%aiplayer.SetMoveDestination(%moveto);

}
#16
09/10/2007 (12:14 am)
Hi Steve,

Thanks for your continued help :)

This was kind of the approach I was taking - but (and forgive me if i'm wrong), the code you've posted above relies on there being a selectable object available in order to relay co-ordinates to the AI player and request that it move?

I guess what I need to retrieve is the terrain co-ordinates where the mouse was clicked - and to have the character move to that point.

Do you know if such a thing would be possible?
#17
09/10/2007 (6:21 am)
That's exactly what my code does, right clicking anywhere on the map produces coordinates to feed to the AI which in turn will travel to where ever you clicked.
#18
05/24/2009 (7:32 pm)
@Chris Bowles: Did you ever get this working for TGEA or Torque 3D?

I'm pretty new to torque, so far I've mostly just: (integrated advanced camera for god mode), (done a basic dts/dsq import for AIPlayer), (merged torque 3d beta 2 changes into beta 1)...

I would love to see a step-by-step walk-through / resource for adding (Move character to mouse click)?

#19
05/24/2009 (9:00 pm)
I'd like to see one as well, I want to use it for an adventure game sort of like the old Kings Quest series.
#20
05/25/2009 (10:18 pm)
I made some progress, but so far I have this error trying to use that code in Torque 3D:

scripts/client/default.bind.cs (601): Unknown command getMouse3dPos.
Object PlayGui(1466) PlayGui -> GameTSCtrl -> GuiTSCtrl -> GuiContainer -> GuiControl -> SimGroup -> SimSet -> SimObject
scripts/client/default.bind.cs (602): Unknown command getMouse3dVec.
Object PlayGui(1466) PlayGui -> GameTSCtrl -> GuiTSCtrl -> GuiContainer -> GuiControl -> SimGroup -> SimSet -> SimObject
scripts/client/default.bind.cs (604): Unknown command getSelectedObj.
Object ServerConnection(2361) ServerConnection -> GameConnection -> NetConnection -> SimGroup -> SimSet -> SimObject
scripts/client/default.bind.cs (606): Unknown command GetGhostIndex.
Object ServerConnection(2361) ServerConnection -> GameConnection -> NetConnection -> SimGroup -> SimSet -> SimObject
scripts/client/default.bind.cs (614): Unable to find function cursorOff


Page «Previous 1 2