Vector Manipulation

Posts: 1,066
Joined: 2004.07
Post: #1
In my third person game, obviously the player does not see the world from the character's perspective; rather he/she sees it from the camera. For aiming I have a position, view, and direction relative to the player as well as a position, view, and direction for the camera. When I am aiming, I find that the bullet is going sort of where it should. It starts at the player and travels in the direction that I give it but my direction is calculated incorrectly. How can I get the player's gun to shoot towards the correct view? I'm uploading a new version of the program so you can see what I mean. If you zoom in you will get a crosshair and can see how it's off.

The program can be downloaded here.

EDIT: As per the request of OSC and my realization of my terrible English (not sure what happened when I was typing this) I have corrected the things I noticed were terrible.
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
a) learn english
b) learn not to contradict yourself mid-sentence
c) read How to get your post answered
Quote this message in a reply
Posts: 1,066
Joined: 2004.07
Post: #3
Ok. I edited the post to help clear up the poor english. I'm not sure why I screwed up so many things in that post.

Anyway, my main problem is getting a vector from where the player is located to where the camera is looking and taking into account that the angle of direction from the camera's position to the viewpoint is different than the angle of direction from the player's position to the viewpoint. I've tried various ideas and tried just using one or the other but neither will allow the player to actually shoot where the crosshair is located on the screen.

If anyone has any experience with third person games with shooting or just knows how to take care of this sort of problem, please help me out.

As I said in my first post, the program can be downloaded from my website so you can see the problem. Zoom in with either weapon and fire (when lines stop appearing, press the middle mouse button or R to reload). You will see that the gun doesn't shoot where it should.
Quote this message in a reply
Posts: 608
Joined: 2002.04
Post: #4
Loading texture logo.png...
Frame Rate: 1
Frame Rate: 157
Frame Rate: 174
Loading model player.obj...
Loading model ground.obj...
Loading texture ground.png...
Loading model skybox.obj...
Loading texture sky.png...
Loading model walls.obj...
Loading texture brick.png...
Apr  3 23:08:21 Bill-Abernathys-Laptop crashdump: Crash report written to: /Users/joshaber/Library/Logs/CrashReporter/The City.crash.log

Fatal signal: Segmentation Fault (SDL Parachute Deployed)


Date/Time:  2005-04-03 23:08:21 -0400
OS Version: 10.2.8 (Build 6R73)
Host:       Bill-Abernathys-Laptop.local.

Command:    The City
PID:        533

Exception:  EXC_BAD_ACCESS (0x0001)
Codes:      KERN_INVALID_ADDRESS (0x0001) at 0xbfbe92b0

Thread 0 Crashed:
#0   0x0000534c in _Z10LoadWeaponR6WeaponPKc (weapon.cpp:165)
#1   0x00053ce8 in _ZN6Player9SetWeaponEPKc (main.cpp:59)
#2   0x00005974 in _Z8GameInitv (main.cpp:51)
#3   0x0000a0e8 in _Z4Initv (init.cpp:207)
#4   0x0000c3c4 in SDL_main (init.cpp:990)
#5   0x000041e8 in -[SDLMain applicationDidFinishLaunching:] (SDLMain.m:196)
#6   0x97dfab40 in _nsNotificationCenterCallBack
#7   0x9016847c in _postNotification
#8   0x90165b90 in _CFNotificationCenterPostLocalNotification
#9   0x9318a8d0 in -[NSApplication _sendFinishLaunchingNotification]
#10  0x931616d0 in _requiredAEEventHandler
#11  0x91b56570 in _Z20aeDispatchAppleEventPK6AEDescPS_mPh
#12  0x91b590cc in _Z25dispatchEventAndSendReplyPK6AEDescPS_
#13  0x91b56478 in aeProcessAppleEvent
#14  0x96a83778 in AEProcessAppleEvent
#15  0x9308dec8 in _DPSNextEvent
#16  0x9309ff78 in -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:]
#17  0x930b1b48 in -[NSApplication run]
#18  0x00004148 in CustomApplicationMain (SDLMain.m:176)
#19  0x00004520 in main (SDLMain.m:277)
#20  0x0000223c in _start (crt.c:267)
#21  0x8fe19154 in _dyld_start

PPC Thread State:
  srr0: 0x0000534c srr1: 0x0200f930                vrsave: 0x00000000
   xer: 0x00000000   lr: 0x00053ce8  ctr: 0x90003d00   mq: 0x00000000
    r0: 0xffbea7e0   r1: 0xbfffead0   r2: 0x0047d460   r3: 0x00482a7c
    r4: 0x00050b7c   r5: 0x00000017   r6: 0x00000000   r7: 0x00000014
    r8: 0x02264010   r9: 0xa0001048  r10: 0x02264290  r11: 0x00000000
   r12: 0x90003d00  r13: 0x00000000  r14: 0x00000000  r15: 0x00000000
   r16: 0x00000000  r17: 0x00000000  r18: 0x00000002  r19: 0x00000000
   r20: 0x022906e0  r21: 0x0d9a1072  r22: 0xa3075954  r23: 0xbffff1b0
   r24: 0xa3075954  r25: 0x022906e0  r26: 0x00000000  r27: 0xbffff1cc
   r28: 0x022906e0  r29: 0xa3075954  r30: 0xbfffead0  r31: 0x97dfaabc
Quote this message in a reply
Posts: 184
Joined: 2004.07
Post: #5
This is the reason most third-person shooting games have some sort of auto-aim. If the camera is at an arbitrary position looking approximately at the player, there is only one way to shoot at what the cross-hair is pointing at, which is to actually cast a ray from the viewpoint, see what it hits, and then have the player shoot at that point. This is because the only way to shoot through the crosshair is when the shot originates somewhere on the ray of the crosshair.

Therefore, you can sort of approximate the effect by putting the camera behind the player (consider the first-person view- then back up the camera along the view vector, and you have what I'm talking about.) Obviously then the player is in the way, making it harder to see, so this is usually approximated by moving the camera slightly upward. The aim will be a little off, but games like GTA3 do similar things.
Quote this message in a reply
Posts: 1,066
Joined: 2004.07
Post: #6
I understand completely. I wasn't worried about it but the idea of sniper rifles being a close shot didn't fly with me. I'll try what I can do and get back about my results.

As far as the crash, I'm still not 100% sure what that's talking about or how to fix it but I'll see what I can do.
Quote this message in a reply
Post Reply