Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
3 registered members (TipmyPip, AndrewAMD, NewbieZorro), 16,055 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Mouse_trace code, screen to 3d coordinates #83783
07/30/06 01:33
07/30/06 01:33
Joined: Jan 2003
Posts: 1,738
Nashua New Hampshire
anonymous_alcoho Offline OP
Senior Developer
anonymous_alcoho  Offline OP
Senior Developer

Joined: Jan 2003
Posts: 1,738
Nashua New Hampshire
Several people have been asking for code that takes a coordinate from where the mouse is clicked so that they could send an entity to that position or create something where the mouse is clicked. I was given such a code on this forum about a year or two ago, an indispensible piece that I've used many times afterwards. I don't remember who it was originally who gave it to me. If I did, I'd thank them and prolly send them a christmas present every freaking year until one of us died Anyways, this code below will allow you to move or create an object at where the mouse position is.

Code:

var from[3];
var to[3];
function mouse_trace()
{
from.X = MOUSE_POS.X; //set from.x to mouse_pos.x
from.Y = MOUSE_POS.Y; //set from.y to mouse_pos.y
from.Z = 0; //set from.z to zero
vec_set(to,from); //copy from to to
vec_for_screen(from,camera); //convert from to 3d coords
to.Z = 3000; //set to.z to 3000
vec_for_screen(to,camera); //convert to to 3d coords
return(trace(from,To)); //return the distance between from and to
}



Like any trace, this modifies the built-in variable "target" which can then be used to send an entity to where the mouse is clicked or place an entity at that spot.

And below is an example of this code in action, placing a beacon when the mouse is right clicked for an RTS game. It also gives the unit a target to walk to:

Code:

var unitTarget[3];

function setTarget()
{
if(!levelLoaded) { return; }
result = mouse_trace(); //trace from mouse to 3d pos.
if(you == null)
{
vec_set(unitTarget,target); //copy mouse_trace target to unitTarget
ent_create(targBMP,unitTarget,beacon); //create the beacon
}
return;
}

on_mouse_right = setTarget();



So I hope this helps a few people as it has helped me. I've used it to fire rockets, shoot wizard blasts, send an entity from one point to another and place beacons on the map. Since I found myself posting this on other threads to respond to similir questions, I thought i'd give it away.


"Oh no, it's true! I'm a love magnet!" Calvin from Calvin and Hobbes My name's Anonymous_Alcoholic.
Re: Mouse_trace code, screen to 3d coordinates [Re: anonymous_alcoho] #83784
07/30/06 02:14
07/30/06 02:14
Joined: Jan 2003
Posts: 1,738
Nashua New Hampshire
anonymous_alcoho Offline OP
Senior Developer
anonymous_alcoho  Offline OP
Senior Developer

Joined: Jan 2003
Posts: 1,738
Nashua New Hampshire
And another thing, if you want to replace trace_mode and trace with c_trace, it'll still work.


"Oh no, it's true! I'm a love magnet!" Calvin from Calvin and Hobbes My name's Anonymous_Alcoholic.

Moderated by  adoado, checkbutton, mk_1, Perro 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1