School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,443 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,513 people online right now. Registration is fast and FREE... Join Now!




to shoot things

 

to shoot things

Kanvus

29 Jun, 2009 - 09:54 PM
Post #1

D.I.C Regular
Group Icon

Joined: 19 Feb, 2009
Posts: 451



Thanked: 39 times
Dream Kudos: 50
My Contributions
In orthographic mode( flat 2D ), the shooting is pretty straight forward. Shoot as in from a gun and in first person view perspective.

And in 3D, you gotta use rotating code like
CODE

//Turn left or right calculation
x = sin(angle);
z = -cos(angle);

//Applying camera change to screen
glLoadIdentity();
gluLookAt(camX,camY,camZ,
            camX+x,camY+y,camZ+z,
            0.0,1.0,0.0f);


But how in the world do you make the bullet object spawn from where you're standing and make it move forward like in every fps game?

I've come up with a few ideas that aren't working out. One was to use SDL and OpenGL then blit a bullethole texture in the center of the screen. That sounds really complicated and probably not the correct way, I think!

The real way would maybe figure out the trigonometry of the bullet object then move it in a straight line in whatever direction the camera is facing. Again, I'm assuming the basic of how it might be done.


Currently, the sphere I spawn when pressing Space would appear at wherever I stand, then float along towards the negative side of Z-axis. It moves away and appears where I am so that's good. But if I turn away, it'd spawn at the side of me, instead of in front. What do I need to think about to get the object to always appear in front and then fly away in that same direction?

This is all the "forward-action" trig code I have:
CODE

void movesObjectOrCameraForward()
{
    camX = camX + (x)*0.9;
    camZ = camZ + (z)*0.9;
    glLoadIdentity();
        gluLookAt(camX,camY,camZ,
                  camX+x,camY+y,camZ+z,
                  0.0,1.0,0.0f);
}




User is offlineProfile CardPM
+Quote Post


Tom9729

RE: To Shoot Things

30 Jun, 2009 - 02:18 PM
Post #2

Debian ninja
Group Icon

Joined: 30 Dec, 2007
Posts: 2,129



Thanked: 53 times
Dream Kudos: 425
My Contributions
This is kind of abstract, sorry.

There are a couple of ways of doing this, so if anyone has something better then please chime in.

You are probably going to want to keep track of 3 things for every bullet.

1) The bullet's position vector XYZ.

2) The direction the bullet is facing. This can either be angles (kind of like polar coordinates), or a unit vector XYZ.

3) The bullet's velocity vector XYZ.

Ideally whatever is firing your bullet should have the same properties, so it might be a good idea to create a generic "entity" class or structure.

When you "fire" a bullet it should start with the same position and direction as it's parent. It's velocity should also be the same, but it should have a greater magnitude (because most bullets travel _away_ from what is firing them smile.gif).

A little more code or explanation of how you're doing things would be helpful.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:26AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month