How to grab an object?

This is tricky but I wanted to make some guesses and do some thinking before I just get the answer from the professor.

So we project a beam out from our pawn that sends the data to the processor that calculates what we are about to collide with. Is this like ray casting? Oh so much to learn.

Maybe that ray can talk to or interact with the object in question. That object can have a setting- or better yet a component. The chair can have a component.

I don’t think that the pawn would need any special component to do this, but maybe it would. I see it as being more unique to the chair needing one, that can attach to and drag with the rest of the object.

I think he implied that all we have to do is touch the chair and it will be picked up magnetically but it would be better to have a user instruction to make that happen, such as a click or button press or better yet, press and hold from the keyboard. I know there is code we can use from C++ libraries, I think getasynckey or something from windows, but that only works on Windows platforms so that’s not good enough.

Anyway we use some if statements- such as “if the pawn is close enough to the component of the chair object”, then “if key is pressed”- and then instruct it to be lifted (raise the vertical plane) and then move in correspondence horizontally with the pawn (but not vertically anymore or on the z axis). Hmm, the lifting action is binary- either up or down, but the lateral is continuous, in the horizontal plane.

So that’s what I can think of. I wonder if we can link the object’s location in 3d space with the pawns location or co-ordinates, just offset by a certain amount- an amount that is fixed in all the relevant planes, at least once the object is grabbed.

Assuming I am even somewhat on track, how would we write the pseudo code for this process? I can see now, coming up with the right method or algorithm is challenging, and then coming up with the pseudo code is a battle in itself, as it gets more specific, and then finding the exact C++ instructions associated with the PseudoCode is the next and last- and then of course refactor. Rome wasn’t built in a day but as a noob, just mapping the problem and getting general stabs at it is huge.

Pseudocode attempt:

Pre-pseudo code: Create component on chair in Unreal Engine

In the header and CPP file:

// determine if the component is in range of the pawn

// determine if the button is pressed that hoists the object

// if those are true, give the formula or mathematical equation that moves the component and object

 // link the component of the object with the object in such a way that when the component is moved, the object moves exactly in step with it.

I think that pretty much sums it up. I’m a genius. Well now I’m curious to see what his answer will be and how far off I am, but I’m very glad I wasn’t lazy or in a hurry and attempted this.

1 Like

Good initial thinking but I think you start to make it too complicated later on.
Having a ray, good thinking. Rays in game engines are not directly rays in a mathematical sense, but rather lines since we are working in an environment with limited resources, which excludes infinity. A ray has therefore an origin, a direction and a length and can return what it hits. That’s already quite a lot and basically all that we need. If we now attach the ray to a certain point, we know exactly where it begins and ends. From here on we can do something very simple. All you need to do now is having an input event, bind it and create a nice function around it, which basically only needs to say, while button is pressed, move the grabbed actor to the end of your ray. The only question left is how you determine whether an Actor can be grabbed or not. And here you have different options. You can either say, in my game, every actor can be grabbed. As long as my HitResult is an Actor, grab it. You could also differentiate. Therefore you could go the more programmatical way of saying everything that can be grabbed, must implement an interface, like IGrabbable. Or you can go a more pragmatic route and simply add tags to the objects you want to grab. And only if your ray returns an actor with the tag “grabable”, it can be grabbed.

Not sure how they do it in the end. I think by using a physicshandle wich is more elegant.

Thanks for the reply. This is great. The thinking is the most important part of learning but it’s hard to have the time to think these kinds of things through. He gives us a structure and platform for thinking so I want to take advantage of it before I ‘cheat’ and get the answer.

So rays are finite lines in the computer sense. They return the object they hit. That’s cool. What they hit or collide with can be ‘discovered’ by the program and hence returned back. To be honest I only just heard of ray casting when I was watching a One Lone Coder youtube video. I love that guy. I’m so new at this but I’m learning a lot. The slower I learn the faster I learn. There’s a lot to learn. Ok so the ray has an origin and just keeps going (iterating) until it hits something that is not a null value- at least that’s what I assume the case is. Besides that, it could have an upper limit of iterations if nothing has been hit. This is just my assumption.

When it hits something, if it returns the thing we are looking for, it can move to a new part of the code in the stack (yes I’m using fancy language to get used to it, even if it’s not correct), that tells it to ‘drag’ or hoist and drag or move alongside the pawn with a certain offset from it’s central point and your central point (or edge point).

“move the grabbed actor to the end of your ray”. Hmm interesting- to the origin of the ray you mean?

I like the tag method for grabbing. Thanks for your input.

I wonder about inheritance, since that was an option. I won’t go there. I don’t think he covered it. Actually maybe it’s not that hard. We just create a subclass or child-class and add some unique new functions that allow dragging? We add that to the chair maybe? Anyway let’s see how he does it. I’ll just copy him. I don’t know if I’ll ever have the time to become a really decent programmer or else it will take a long time but it’s fun learning and hopefully I’ll be able to put this all to productive use somehow somewhere.

Technology is amazing.

Privacy & Terms