 # Thread: OpenGL, 3D game and collision with wall

1. ## OpenGL, 3D game and collision with wall

Hello.
I'm trying to make simple 3D game via OpenGL extension. At first I was having a problem with movement, because the options "forwards" and "backwards" in camera allows to fly in game, but I solved it using trigonometry with X and Z of camera. Also I added gravity and collision with top of 3d boxes. It works well.

But I'm stuck with, I guess simple, problem. How to make an effect such as in other 3D games when I go forward into the wall, while I'm in front of it I'll get stopped while pressing "w", but when I change angle of view, I'll be moving faster on side depending of direction and my camera angle?  Reply With Quote

2. ## Re: OpenGL, 3D game and collision with wall

Hey Grim!

Its great to hear others are working with OpenGL in MMF2

There are a number of methods to achieve what your talking about; the following in a somewhat simple implementation

I assume your movement code looks something like the following

Camera:
X position = X position + Sin ( Y Angle )
Z position = Z position + Cos ( Y Angle )

Change it to the following

Camera:

IF location specified by (X position + Sin ( Y Angle ), Z position) is not inside something solid THEN
X position = X position + Sin ( Y Angle )
ENDIF

IF location specified by (X position, Z position + cos( Y Angle )) is not inside something solid THEN
Z position = Z position + Cos ( Y Angle )
ENDIF

Performing separate collision detections for separate dimensions allows the player to slide along a wall instead of simply being unable to approach it.

I would love to see your project!

And if you have further questions please ask   Reply With Quote

3. ## Re: OpenGL, 3D game and collision with wall

Well, you said there must be a negate of collision to allow X or Z moving. At first I had exactly like you said:
Code:
```*Repeat while "W" is pressed
- (OpenGL Camera) Set X Position to: X( "OpenGL Camera" )+Sin(Yaw( "OpenGL Camera" ))
- (OpenGL Camera) Set Z position to: Z( "OpenGL Camera" )+Cos(Yaw( "OpenGL Camera" )+180)```
And it works well when there are no walls. :P

I was trying to make a dependence of collision with your helpful post, so I made this that way:
Code:
```*Repeat while "W" is pressed
*Alterable Value A of ("Valuer") = 0
- (OpenGL Camera) Set X Position to: X( "OpenGL Camera" )+Sin(Yaw( "OpenGL Camera" ))

*Repeat while "W" is pressed
*Alterable Value B of ("Valuer") = 0
- (OpenGL Camera) Set Z position to: Z( "OpenGL Camera" )+Cos(Yaw( "OpenGL Camera" )+180)```
To make a negate collision I used two ifelse object in following events:
Code:
```*(OpenGL MS3D) On object loop
*(IfElse1)Start Nested IF 0
*Y("OpenGL Camera") > Y("OpenGL MS3D") - 20
*Y("OpenGL Camera") < Y("OpenGL MS3D") + 20
*X("OpenGL Camera") + Sin(Yaw("OpenGL Camera")) * 2 > X("OpenGL MS3D") - 12
*X("OpenGL Camera") + Sin(Yaw("OpenGL Camera")) * 2 < X("OpenGL MS3D") + 12
*Z("OpenGL Camera") > Z("OpenGL MS3D") - 12
*Z("OpenGL Camera") < Z("OpenGL MS3D") + 12
*(IfElse1)End Nested IF
- ("Valuer") Set Alterable Value A to 1

*(IfElse1)Nested Else 0
- ("Valuer") Set Alterable Value A to 0

*(OpenGL MS3D) On object loop
*(IfElse2)Start Nested IF 0
*Y("OpenGL Camera") > Y("OpenGL MS3D") - 20
*Y("OpenGL Camera") < Y("OpenGL MS3D") + 20
*X("OpenGL Camera") > X("OpenGL MS3D") - 12
*X("OpenGL Camera") < X("OpenGL MS3D") + 12
*Z("OpenGL Camera") + Cos(Yaw("OpenGL Camera")) * 2  > Z("OpenGL MS3D") - 12
*Z("OpenGL Camera") + Cos(Yaw("OpenGL Camera")) * 2  < Z("OpenGL MS3D") + 12
*(IfElse2)End Nested IF
- ("Valuer") Set Alterable Value B to 1

*(IfElse2)Nested Else 0
- ("Valuer") Set Alterable Value B to 0```
I hope I haven't made any mistakes while I was rewriting. :P

The following code works only in specific case. It depends of position when going into the cuboid. While I'm trying going to one wall, it works, but when going into different wall it just holds both camera X and Z.

Could you help me and fix the "code" above?
Thanks.  Reply With Quote

4. ## Re: OpenGL, 3D game and collision with wall

I may have a solution,

Your code is not in reverse polish notation..

"Z("OpenGL Camera") + Cos(Yaw("OpenGL Camera")) * 2 > Z("OpenGL MS3D") - 12"

should be

"Z("OpenGL Camera") + ( Cos(Yaw("OpenGL Camera")) * 2 ) > Z("OpenGL MS3D") - 12"

Also;
For 3D movement Including elevation try the following

X position = X position + (Sin ( Y Angle [Yaw] ) * Sin ( X Angle [Pitch] ))
Z position = Z position + (Cos ( Y Angle [Yaw] ) * Sin ( X Angle [Pitch] ))
Y position = Y position + cos ( X Angle [Pitch] )

Maybe a screen-shot ?  Reply With Quote

5. ## Re: OpenGL, 3D game and collision with wall

Thanks for the reply. Unfortunatelly it doesn't solve my problem.

Here, check my mfa.
http://www.speedyshare.com/files/28012287/OpenGL.mfa
For now it's poor, I'm just trying to finish engine and when I do it, I'll try to finish game.

I also translated whole comments from polish to english to understand it better.

The problem is, when player touch a wall, he will be stuck there.   Reply With Quote

6. ## Re: OpenGL, 3D game and collision with wall

Ah, I have fixed the problem right now and it works great. Thanks SEELE for helping me.  Reply With Quote

7. ## Re: OpenGL, 3D game and collision with wall

I thought you might, the code you uploaded was impressive, and quite close too working ***and even written without any access to direct OpenGL commands for example via scripting extensions such as Xlua;  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•