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.

Newcomers DBPro Corner / Ray casting to mouse coordinates?

Author
Message
the nameless
16
Years of Service
User Offline
Joined: 7th Mar 2008
Location: Hell
Posted: 18th Mar 2008 22:29
Hi!

I`m trying to cast a ray towards the mouse, which isn`t in the center of the screen. The function Pick Object() is really useful for detecting 3d objects, but let`s assume that I want to create a rail shooter and shoot towards a wall. I need to cast a ray and to create a bullethole at the proper place.

A friend of mine helped me to achive this on differnt platform using matrixes and one function called "CoordToScreen()".

How can I achieve this in DB? In particular how can I find the origin point and the rotations to create the ray?
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 18th Mar 2008 23:37
The easiest method would be to point the camera when you move the mouse rather than moving the mouse cursor - so you are always pointing directly at the target. Hide the mouse pointer and have a cross-hair in the centre of the screen for aiming.

It might be easier to use Sparky's collision DLL to do this (assuming it works the same in DBP as Sparky's does in DBC - which is what I use).

TDK_Man

the nameless
16
Years of Service
User Offline
Joined: 7th Mar 2008
Location: Hell
Posted: 19th Mar 2008 00:25
Thanks, TDK.

I know it would be easier to have the crosshair centered,
but unfortunately this isn`t an option.

Actually the case is more complicated, but it wasn`t nesessary to explain in detail.

I`m sure I`ll find sensible solution. But now I`d better take the first plane to the fairy Dreamland...

Still, if someone has handled this before any help or advice still will be highly appreciated.

PS: Ah, and Sparky`s collision isn`t an option too, since I`m using Newton Physics dll...
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 19th Mar 2008 01:00
I've never used Newton, but would it allow you to do what you wanted to do if you could point the camera?

If so, the next - not as easy - method would be to calculate the point the mouse is pointing to with maths, then actually rotate the camera to point at that position to use the relevant Newton command.

As long as you put the camera back to the initial angles before the Sync, you wouldn't see the camera move. This is what I do in my DBC level editor to select an object not being pointed at, so I know it works OK. It uses Sparky's IntersectObject() function which also only works if the camera is pointing exactly at the point of intersection.

TDK_Man

Darth Vader
19
Years of Service
User Offline
Joined: 10th May 2005
Location: Adelaide SA, I am the only DB user here!
Posted: 19th Mar 2008 05:40
I might be wrong, but isn't Newton physics only? I thought that Sparkys dll was for collision and raycasting?


the nameless
16
Years of Service
User Offline
Joined: 7th Mar 2008
Location: Hell
Posted: 19th Mar 2008 10:11
Quote: "If so, the next - not as easy - method would be to calculate the point the mouse is pointing to with maths, then actually rotate the camera to point at that position to use the relevant Newton command.
"


This is exactly the problem. Maths. He-he. And yes, I`ll do it using Newton, if I could calculate the point of the ray origin and its pan and tilt angles + some perspedtive stuff... I`ll try your method...

Quote: "I might be wrong, but isn't Newton physics only? I thought that Sparkys dll was for collision and raycasting?
"


Actually I think that the good physics engine won`t be good if there ain`t a collision support. And raycasting isn`t that difficult to add if you`re creating such an engine too. Raycasting is a nessessity too, that`s why it`s already implemented in Newton.
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 19th Mar 2008 11:29
OK, not the most elegant way around it - I'm sure someone with better maths skills than me will probably explain why this is not the best method but...



This gives you PlaneX#, PlaneY# and PlaneZ# for wherever the mouse is pointing on the screen. But, PlaneY# is always 0.

However, this doesn't matter as all you need is three values to use with Point Camera.

So, grab the current camera angles, use these three co-ords to point the camera, do your IntersectObject() equivalent, reset the camera back to the original angles then finally do a Sync. You won't even know the camera moved!

If you move the camera, be sure to update the PointCamX#, PointCamY# and PointCamZ# variables then use:

Point Camera PointCamX#,PointCamY#,PointCamZ#

TDK_Man

the nameless
16
Years of Service
User Offline
Joined: 7th Mar 2008
Location: Hell
Posted: 19th Mar 2008 14:05
Thanks for the efforts and the exact code, TDK.

Although I`ll try to find a better solution, I think this is very good alternative to have. Thanks again!
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 19th Mar 2008 17:23
Quote: "I`ll try to find a better solution, I think this is very good alternative to have"




Alternative? Either it does what you want or it doesn't!

TDK_Man

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 19th Mar 2008 21:33
It sounds like you want to calculate your mouse position using the PICK SCREEN command with a zero distance, then another position using PICK SCREEN with a much longer distance - that defines your ray.

Then you can use the INTERSECT OBJECT command to determine the object being intersected and the 3D position of the intersection.

Does that sound like what you are after?

the nameless
16
Years of Service
User Offline
Joined: 7th Mar 2008
Location: Hell
Posted: 19th Mar 2008 23:59 Edited at: 20th Mar 2008 00:02
Quote: "Alternative? Either it does what you want or it doesn't!"


Hehe! It WILL do the job for sure- the implementation in your project is the living proof. This is good, but there ain`t a reason not to try to do it in a different way... Things can always be better...

Quote: "It sounds like you want to calculate your mouse position using the PICK SCREEN command with a zero distance, then another position using PICK SCREEN with a much longer distance - that defines your ray.

Then you can use the INTERSECT OBJECT command to determine the object being intersected and the 3D position of the intersection.

Does that sound like what you are after?"


Hmm... the scheme is almost the same, but I weren`t aware of the use of these functions. Right now it`s too late in the evening to give a try, but probably tomorrow I`ll...

So thanks for the advice. Or was it a conclusion?! Ha-ha! Of course it IS a good idea. Hopefully it will work.
DB PROgrammer
17
Years of Service
User Offline
Joined: 9th Feb 2007
Location: Nowhere But Everywhere
Posted: 20th Mar 2008 02:38
I just discussed this with someone and figured I'd post the link. It gets a little side tracked after a while but it's got what your after.

http://forum.thegamecreators.com/?m=forum_view&t=125651&b=1


I'm Pro grammer.
culmor30
17
Years of Service
User Offline
Joined: 16th Jun 2007
Location: In my head.
Posted: 20th Mar 2008 02:42 Edited at: 20th Mar 2008 02:43
Dunno if you still need help or not, but here's what I did for my shooting raycasting:


(The shooting in my game "erases" objects.)

Of course, you'll need to replace the PHY commands with the DBP equivalent. And credit goes to whoever it was that taught me how to do this thing with the vectors.

Also, I haven't actually used DBP's raycasting, so this might not be any help at all if it doesn't use the same "vector" system. It might still work though.
the nameless
16
Years of Service
User Offline
Joined: 7th Mar 2008
Location: Hell
Posted: 20th Mar 2008 15:12
Many thanks to all of you!!!

This is what I wanted to create and use:


I modified it a little to adjust it to my needs, and it works like a charm.

@Culmor30: This code works also, but it is in FPS style. What I needed was to cast a ray, not poiting towards the camera positive Z axis, but in the mouse cursor direction. Ahhh, and you`re a lucky one to use Dark Physics extension. I use Newton and can only dream on using it, since I cannot afford it right now. I really like the Enhanced Animation extension also...

My problem is SOLVED! Thanks again to all of you helpful people

Login to post a reply

Server time is: 2024-11-25 05:39:45
Your offset time is: 2024-11-25 05:39:45