In BGE: Move Object Along a Direction (Normal Movement)

okay guys so I want to snap a wall adjacent next to another wall. It’s a building system for the player. The player looks at a wall, the wall he’s placing gets snapped to the wall he’s looking at. The placed wall needs to be the same orientation as the wall he’s looking at.

EXAMPLE:

In this case the red wall’s orientation would equal the blue wall’s and it’s position would be offset on the positive x axis by the width of the blue wall


In this case the red wall’s orientation would equal the blue wall’s and it’s position would be offset on the positive y axis by the width of the blue wall and offset on the positive x axis by the width of the blue wall


Easy enough, where I need help is coming up with an algorithm that deals with the blue wall(stationary wall that the player’s wall gets snapped adjacent to) being any possible orientation on the z axis. This is where you guys come in

Attachments


I have one idea, and but there’s probably a better way of doing this and I sure hope so, I need something like this eventually too, but anyways you could have an empty parented on each side of the wall, and every time the player adds a wall, depending on which side of the wall he looks at (maybe using different objects for each face of the wall… I don’t like this idea either…), it will add another composite wall at the corresponding empty. This is probably more like a starting point and hopefully someone can come up with a better idea…

That’s certainly one way of going about it, it would be fairly easy to implement that. I’m going for a cleaner solution. To determine the face of a rectangle the player is looking at you can use (ray sensor).hitNormal. This works well on a cube that has no rotation, haven’t tested it out on a rotated object yet. I can talk more in depth about how to use it if you would like, or you can do a forum search for .hitNormal. I’m gonna crash for the night, I’ll check back tomorrow.

Actually if you wouldn’t mind could you explain it a little bit?
Glad we can help each other :slight_smile:

yeah sure, let me type out a decent explanation, might take a sec

so if you think of a cube it has 6 sides, to make things easier, you have the front, back, left, right, top, bottom.

hit_normal = ray.hitNormal

ray.hitNormal
this returns a tuple, or a set of values, looks like [0, 0, 0], this “tuple” is what we’ll be using to determine which side of the cube the player is looking at, (the ray sensor is shooting a ray on the positive y axis, this can be shot from the center of the players camera or an object parented to the center of the players camera)

think of the sides of the cube in terms of the global x, y, and z axis. The left and right side of the cube are facing along the x axis, the front and back are facing along the y axis, the top and bottom are facing along the z axis.

this will make things easier
these refer to the sides of the cube, so “front” would mean the front of the cube and so on
front = [0, 1, 0] (positive 1 in the y spot meaning forward on the y axis)
back = [0, -1, 0] (negative 1 in the y spot meaning facing backward on the y axis)
left = [-1, 0, 0] (negative 1 in the x spot meaning facing the left on the x axis)
right = [1, 0, 0] (positive 1 in the x spot, meaning facing the right on the x axis)
top = [0, 0, 1] (positive 1 on the z axis meaning facing up on the z axis)
bottom = [0, 0, -1] (negative 1 on the z axis meaning facing down on the z axis)

so we could say for example

if ray.hitNormal == [0, 1, 0]:
print(“you are looking at the front of the cube”)
do = something appropriate

if you need more clarification, I could set up a little demo blend file for you, it would be some time tomorrow

oh and if you’re trying to find out which side of an object the player is looking at and it isn’t a cube, take a cube, make it invisible. Put the object in the center of the cube, parent the cube to the object. Now you can determine where the player is looking at the object in 3d space.

ok so turns out my problem is really easy to solve, I was just making things more complicated as I tend to do.
If you want to snap a wall to another wall, this is all you need to do

wall.position[0] = ray.hitPosition[0]
wall.position[1] = ray.hitPosition[1]
wall.position[2] = ray.hitObject.position[2]
wall.alignAxisToVect((ray.hitNormal), 0, 1)

How do I mark this thread as solved?

(Go to the first post, hit edit, hit advanced, look by the thread title for a box called ‘prefix’ and change it to solved.)