Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 1,382 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19055 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 | chip programmers | 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