About Store Forum Documentation Contact
Donations:
169$/mo



Post Reply 
hit detection 2D/3D
Author Message
craksy Offline
Member

Post: #1
hit detection 2D/3D
hey grin
is it possible to do hit detection on mixed 2D and 3D objects?
the thing is that i made a simple drag select/box select/whatever (what are those things called anyway?), like for a simple RTS or something.
its working fine except ... i dont know how to make hit detection on 3D objects with it :/

how exactly would i do this?
thanks smile

-Craksy
10-14-2009 07:59 PM
Find all posts by this user Quote this message in a reply
Xhizors Offline
Member

Post: #2
Re: hit detection 2D/3D
craksy Wrote:hey grin
is it possible to do hit detection on mixed 2D and 3D objects?
the thing is that i made a simple drag select/box select/whatever (what are those things called anyway?), like for a simple RTS or something.
its working fine except ... i dont know how to make hit detection on 3D objects with it :/

how exactly would i do this?
thanks smile

-Craksy

You mean like this? Well I can look into that.
10-15-2009 08:18 AM
Find all posts by this user Quote this message in a reply
craksy Offline
Member

Post: #3
Re: hit detection 2D/3D
yeah exactly grin
what is those called anyway? pfft

and thanks smile
i hope you will be able to help me out xD
10-15-2009 09:16 AM
Find all posts by this user Quote this message in a reply
Xhizors Offline
Member

Post: #4
Re: hit detection 2D/3D
craksy Wrote:yeah exactly grin
what is those called anyway? pfft

and thanks smile
i hope you will be able to help me out xD

I think they are called selection boxes pfft

Well I will try write some sample when I get time off.

Cheers.
10-15-2009 09:26 AM
Find all posts by this user Quote this message in a reply
craksy Offline
Member

Post: #5
Re: hit detection 2D/3D
awesome grin
thanks m8 xD
10-15-2009 09:55 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
Re: hit detection 2D/3D
these functions will help you (from Camera.h)

Code:
Vec2 PosToScreen (Vec &pos             ); // convert 3D position to 2D screen position
Vec2 PosToScreenM(Vec &pos             ); // convert 3D position to 2D screen position (transformed by current object matrix)
Bool PosToScreen (Vec &pos,Vec2 &screen); // convert 3D position to 2D screen position                                       , false on fail (point is behind the camera)
Bool PosToScreenM(Vec &pos,Vec2 &screen); // convert 3D position to 2D screen position (transformed by current object matrix), false on fail (point is behind the camera)
10-15-2009 11:59 AM
Find all posts by this user Quote this message in a reply
craksy Offline
Member

Post: #7
Re: hit detection 2D/3D
yeah i thought about that after looking at the "hit detection under mouse" (or something like that) tutorial.
but exactly what am i supposed to do?
ray testing only hit tests a point right? im looking more for a kinda areal hit detection i guess?

hmm... anyway i like to figure out things myself, instead of just copying others code, so if you could just give me a push in the right direction it would be great pfft

thanks in advance grin
10-15-2009 12:24 PM
Find all posts by this user Quote this message in a reply
craksy Offline
Member

Post: #8
RE: hit detection 2D/3D
anyone? :/ please...

i know i ask stupid questions a lot, but i really want to understand this.

help would be appreciated smile
10-15-2009 09:06 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #9
RE: hit detection 2D/3D
try using the functions I have posted above, first convert a position to onscreen 2D position, then you can check if it cuts a custom 2D Rectangle
10-15-2009 09:39 PM
Find all posts by this user Quote this message in a reply
craksy Offline
Member

Post: #10
RE: hit detection 2D/3D
you mean converting the characters position to screen 2D pos, and checking with the rect?
but i couldnt fint anything to hit detect Rect :S
only Ball, box, and capsule, and stuff like that...
10-15-2009 09:58 PM
Find all posts by this user Quote this message in a reply
craksy Offline
Member

Post: #11
RE: hit detection 2D/3D
basically i wanna know what function can be used to do collision detection on a 2 dimensional area...
so far iv'e only seen Cuts() which only works with balls, boxes, capsules and other shaped, and Ray() which tests in a straight line... i cant think of anyway to use them with a square :/
10-16-2009 04:57 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #12
RE: hit detection 2D/3D
there is also Cuts for Rect, check Rectangle.h header file
10-16-2009 06:05 PM
Find all posts by this user Quote this message in a reply
craksy Offline
Member

Post: #13
RE: hit detection 2D/3D
oh... i only looked at the physics.h as i assumed all collision detection would be there :/
im sorry for wasting your time :/

but thanks a lot grin
10-16-2009 06:17 PM
Find all posts by this user Quote this message in a reply
craksy Offline
Member

Post: #14
RE: hit detection 2D/3D
Thanks a lot for your help grin
i figured it out, and it.... almost works pfft
here's some of the code, of my Update() function:
Code:
    if(Ms.b(0)){
        if(!MouseP.any()) MouseP = Ms.pos;
        rectangle.set(MouseP, Ms.pos);
        
        
        REPA(Chrs){
            Vec2 chrPos = PosToScreen(Chrs[i].pos());
            hit = (Cuts(chrPos, rectangle) ? true : false);
        }
    }
    else{
    MouseP.zero();
    rectangle.zero(); //i only used zero() because i couldn't figure out how to make it null
    }

in my draw function i just have:
Code:
    rectangle.draw(BLUE, false);
    D.text(0, -.8, ((hit) ? "Yay it hit :D" : "no hit"));

anyway as said: it almost works.
it only returns true if i drag from bottom left of character and up/right.
if i drag from fx. top right of character and drag down/left nothing happens :S
why is that?

hope you can help me out smile

-Craksy
10-16-2009 07:46 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #15
RE: hit detection 2D/3D
you're using
Rect& set (Vec2 &min,Vec2 &max)
which assumes that you specify first the 'min' values, and then 'max' values

in your case you should use
Rect& from (Vec2 &a,Vec2 &b); // create from 2 points
10-16-2009 08:37 PM
Find all posts by this user Quote this message in a reply
Post Reply