# Build 3D games with Construct

Discussion and feedback on Construct 2

### » Thu May 23, 2013 1:14 pm

This is indeed very cool, JoyfulDreamer.
I've been messing around with the engine and most of it is really easy to use.
There's just one thing. I want my player to shoot an object, so I create an object at CameraX, CameraZ. The problem is I can't figure out how to calculate the angle of movement for the object, so that it fires in the direction of the current view.
Do you have any ideas as to how to solve this?
B
5
S
1
Posts: 18
Reputation: 661

### » Thu May 23, 2013 9:56 pm

Hi @RuneS,

Here are some points to keep in mind:

1. Objects in the tutorial project never move (though they could).
2. The camera (player) starts at 0,0,0 (and 0 rotation) - so that means the obejcts you see on screen have NO translation applied (other than perspective and size for the view)
3. As the camera (player) moves, the camera location is simply subtracted from all object positions in the world to find their new positions relative to the player's movement (the objects x,y,z never change in this case).

So, let's assume the player NEVER moves. The player (camera) is at 0,0,0 (z,y,z) facing angle 0 on the Y axis (straight down Z+) and thus if you create an object at 0,0,0 and fire it down Z+, it will work. The trick then is to start at the player's (camera's) position, so that when the camera's position is subtracted from the objects position, it begins at the translated position of 0,0,0. ;) More direct to the point, just make the projectile start at the player's position and create a 3D direction vector facing the direction.

First, you need a non-translated direction:

X=0
Y=0
Z=1

This is a "normalized" vector (no absolute values > 1 [so you can multiply it against values for magnitude]) facing down Z+. If the player never rotates, this is all you need! :) However, when the player turns, you need to rotate the vector. I have the functions there for you already:

newX = RotateXZForX(X, Z, CameraYRotation)
newZ = RotateXZForZ(X, Z, CameraYRotation)
(Y is 0, unless you want to angle an object up or down [in which case there are functions for that as well])

Note: NEVER change X or Z before the second line (before newZ is set), otherwise madness will ensue! :)

Hope that helps! When you get your game finished, post a link here. 8)JoyfulDreamer2013-05-25 21:24:35
Jack of all trades, and master of some.
B
31
S
10
G
7
Posts: 176
Reputation: 7,816

### » Mon May 27, 2013 12:15 pm

Hi again JoyfulDreamer

Thank you for that thorough explanation. I thought I'd share my experience here.

I did manage to get a Z-direction from your instructions. But then I couldn't figure out how to use that to determine the bullet objects direction. I'm terrible at math

I did however find a solution to my problem
While I was testing and things didn't work, I became more and more annoyed that I couldn't use the standard behaviors built in to C2. Then it occured to me, that that maybe I could. If I made a player controlled object with the behaviors and then set CameraX, CameraZ and CameraYRotation to the objects X,Y and angle. And whataya know, it worked
I had to fiddle a bit, but it did.

What this means is, that I could make any kind of 2d game (as long as it's top down) and use your engine to generate a 3d layer. Now I really love your engine!!!

Here's a small demo:

http://db.tt/e4k143Jm

Not at all finished, so please be kind. use arrow keys and ctrl to shoot
RuneS2013-05-27 12:16:05
B
5
S
1
Posts: 18
Reputation: 661

### » Tue May 28, 2013 8:32 am

Wow nice job! :) You should keep the movement keys operating on X and Z only, and rotate with the mouse - perhaps even aim and shoot with it as well. ;)

What you're doing is controlling the camera. I thought your problem was firing a projectile from the camera. ;) When you have to shoot something, you'll need to follow my previous post. I'll see if I can implement a shooting example and upload the project file again if I can get some time.
Jack of all trades, and master of some.
B
31
S
10
G
7
Posts: 176
Reputation: 7,816

### » Thu May 30, 2013 1:36 pm

[QUOTE=JoyfulDreamer] Wow nice job! :) You should keep the movement keys operating on X and Z only, and rotate with the mouse - perhaps even aim and shoot with it as well. ;)[/QUOTE]

Thank you very much:)
I've made a new example here: http://db.tt/MxgDcMi9
Mouse controls left and right and shoot, WASD for x,y and strafe. Shift to run. M to turn map on and off

[QUOTE=JoyfulDreamer] What you're doing is controlling the camera. I thought your problem was firing a projectile from the camera. ;) When you have to shoot something, you'll need to follow my previous post. I'll see if I can implement a shooting example and upload the project file again if I can get some time.[/QUOTE]

No I don't! :)
Since the camera is now always positioned relative to my player controlled sprite, I can just spawn a bullet from that sprite, then make an object in the 3d representation whose WorldX and WorldZ is the same as the bullet's x and y (and update those every tick, of course).
In my example all game functionality is happening in the 2d map, and the 3d is just sort of an overlay..

By the way what kind of fps do you (or anyone else) get? I get between 17 and 25 fps which I think is pretty close to acceptable.
B
5
S
1
Posts: 18
Reputation: 661

### » Fri May 31, 2013 10:35 am

Oh I see, you are using the native C2 functions to manipulate a virtual 2D world then translating an "XY" plane onto the "XZ" plane. :) Great idea.

The controls are getting better, but for the mouse, you may want to make the rotation angle directly proportional to the mouse's X position and see if that helps.

Assuming the FPS is the number at the top, I get 15-24.

I never optimized the tutorial example to be an engine, but people are using it like one. LOL! ;) There's probably many ways to speed it up, but I'd rather build a plugin once Scirra supports 3rd-party plugins in the arcade. If anything, it would just be fun to do. 8)JoyfulDreamer2013-05-31 10:39:19
Jack of all trades, and master of some.
B
31
S
10
G
7
Posts: 176
Reputation: 7,816

### » Sat Jun 01, 2013 4:52 pm

Yes, that's exactly what I'm doing. I think it adds tremendous flexibility to work that way.

I haven't really ever played these kind of games, so I didn't know how the mouse was supposed to react . I've changed it now so that the movement is proportional to mouseX. However this causes the problem that you can't keep turning when the mouse reaches the end of the screen. Don't really know what to do about that...

Yes I'm using your example as an engine. Because I can
I was actually just thinking that it would be awesome to have a plugin to handle this. So if you think it would be fun, I really encourage you to make it.
B
5
S
1
Posts: 18
Reputation: 661

### » Sun Jun 02, 2013 5:44 am

True, if the mouse can't be "captured" then that might make it a bit odd. It might be better to keep Q and E for rotation (just make the rotation fast) and simply use the mouse to aim and shoot instead. ;)
Jack of all trades, and master of some.
B
31
S
10
G
7
Posts: 176
Reputation: 7,816

### » Sun Jun 02, 2013 6:27 am

Very cool guys.
B
8
S
2
Posts: 63
Reputation: 1,223

Previous