Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Raycasting with set ray length

Author
Message
JohnStabler
AGK Bronze Backer
10
Years of Service
User Offline
Joined: 16th Aug 2013
Location: Cardiff, Wales, UK
Posted: 18th Nov 2013 14:15
I'd appreciate any help you could give me with my problem.

I have a spaceship at a given 3D point (x, y, z) and rotation (rx, ry, rz). The spaceship fires a laser which is of a fixed length (r). How do I calculate the end point of the ray for use with ObjectRayCast ?

I've used various methods I googled, but I'm not getting any hit detections and I'm not sure if it's the maths that's wrong or an issue of precedence in AppGameKit basic.
Thanks
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 18th Nov 2013 17:49 Edited at: 18th Nov 2013 17:54
if you have a direction you can multiply
x,y,z * length.

xend#=xstart#+dx#*l#
yend#=ystart#+dy#*l#
zend#=zstart#+dz#*l#

to get a direction you need 2 points

float!
dx=x2-x1
dy=y2-y1
dz=z2-z1
length=sqrt(dx*dx + dy*dy + dz*dz)
if length<1.0 then length=1.0
dx=dx/length
dy=dy/length
dz=dz/length

d is now a vector with length of 1 unit

AGK 108 B19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
JohnStabler
AGK Bronze Backer
10
Years of Service
User Offline
Joined: 16th Aug 2013
Location: Cardiff, Wales, UK
Posted: 18th Nov 2013 18:46
I don't have the end point, only a single point (the position of the spaceship). I don't have a direction, just the rotation of the spaceship (in degrees).
Fallout
21
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 18th Nov 2013 21:36
Don't be afraid to use the object movement commands if you don't know your 3d maths. You can simply move your ship forward the length of the laser using moveobjectlocalz, save this coordinate, then move the ship back using the same command..
JohnStabler
AGK Bronze Backer
10
Years of Service
User Offline
Joined: 16th Aug 2013
Location: Cardiff, Wales, UK
Posted: 19th Nov 2013 11:30
Ha! Genius idea Fallout. Sometimes we always look for the difficult answers...
Fallout
21
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 19th Nov 2013 13:26
Yeah. Maths can be more elegant and often it's more efficient, but if the code is tidier, it saves you 3 days trying to figure something out and code speed isn't noticeably affected, do it the easy way! I generally use simple dummy objects and the 'easy way' until such a time as it's becoming a bottleneck and then I'll do the grind of learning some complex maths to do it more efficiently.

Login to post a reply

Server time is: 2024-05-20 13:19:07
Your offset time is: 2024-05-20 13:19:07