Results 1 to 14 of 14

Thread: Make my player face the mouse?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84

    Unhappy Make my player face the mouse?

    Hi all, need a little help here I'm making a game (well, attempting to) and I've got a problem with aiming. It's a 2D shooter and I want to use the mouse, but that means I need to rotate (or simulate rotation of) my player character. So how would I make my player face in the direction of the mouse cursor? (The player rotation is not the problem, I'll just load loads of pictures of the player into a resource file).

    I fear it's gonna include alot of maths, angles and other complicated stuff which I will struggle to understand

    Thanks in advance.

    Walter
    Anticipation of death is worse than death itself

  2. #2
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    I have some example code for a 2D shooter which uses the mouse, but its in C++ though....let me know if want it.
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    That'd be great thanks. I suppose I could try and use it as an example to code it in VB but I don't have much experience in C++. (I'll give it my best shot anyway). Could you email it to me at [email protected] please. Thanks again!

    Walter
    Anticipation of death is worse than death itself

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    Anyone know how to do this in VB??
    Anticipation of death is worse than death itself

  5. #5
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    There's a game called "Craft", I think it was made by Plenderj (yes he's in this forum), it's exactly what you're looking for
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    Cool, thanks alot I'll check it out. Has he uploaded it in a post or am I gonna have to email him for it (and if so could I have his email address please?). Thanks again.

    Walter
    Anticipation of death is worse than death itself

  7. #7
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Just use the forum's Search button to look for one of his posts, and then send him a PM or check his user profile for his e-mail address
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    Thankyou. Couldn't find it in a post so I've emailed him (at the only address I could find). Thanks again Jotaf

    Walter
    Anticipation of death is worse than death itself

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Hmmmmmm ego massage.

    Aaaaanyway.
    The later versions of Craft, rotate the ship by using the cursor keys.
    But I have a version of it thats about two years old or more that still rotates that way.

    The rotation code is still in the application, as you can see below.
    The code, I'm afraid to say, isn't too clean, as this is a very early version.

    You can download the entire app from here :
    http://www.coolground.com/plenderj/c...unt_bitblt.zip
    But do remember, this is a very old version, so the code isn't the greatest.

    VB Code:
    1. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    2.        
    3.     If (doloop = False) And (var_status = 1) Then
    4.        
    5.         doloop = True
    6.         RunGameLoop
    7.    
    8.     End If
    9.        
    10. '''    For i = ccl.LBound To ccl.ubound
    11. '''        If ccl(i).FillColor = &HFF0000 Then ccl(i).FillColor = &H80FFFF
    12. '''    Next i
    13. '''    For i = ccl_stn.LBound To ccl_stn.ubound
    14. '''        If ccl_stn(i).FillColor = &HFF0000 Then ccl_stn(i).FillColor = &H40C0&
    15. '''    Next i
    16.    
    17.     If (var_status <> 0) Then
    18.    
    19.         var_speed = Sqr(((mewidth / 2) - x) ^ 2 + ((meheight / 2) - y) ^ 2)
    20.         lblspeed = var_speed
    21.    
    22.         var_temp_x = x
    23.         var_temp_y = y
    24.         var_cords_x = (mewidth / 2) - x
    25.         var_cords_y = (meheight / 2) - y
    26.    
    27.         If (var_cords_y <> 0) Then
    28.             var_angle = (Atn((var_cords_x * -1) / var_cords_y) * var_pi)
    29.         Else: var_cords_y = 0.001
    30.         End If
    31.    
    32.         If (var_angle < 0) Then var_angle = var_angle * -1
    33.    
    34.         If (var_angle = 0) And (var_cords_x = 0) And (var_cords_y < 0) Then var_angle = 180
    35.                
    36.         If (var_cords_x < 0) Then
    37.             If (var_cords_y > 0) Then
    38.                 var_angle = var_angle
    39.             ElseIf (var_cords_y < 0) Then
    40.                 var_angle = 90 - var_angle
    41.                 var_angle = var_angle + 90
    42.             End If
    43.         ElseIf (var_cords_x > 0) Then
    44.             If (var_cords_y > 0) Then
    45.                 var_angle = 270 - var_angle
    46.                 var_angle = var_angle + 90
    47.             ElseIf (var_cords_y < 0) Then
    48.                 var_angle = var_angle + 180
    49.             End If
    50.         End If
    51.    
    52.         var_angle = (Int(Int(var_angle) / 10) * 10)
    53.         var_dist_x = Sin(var_pi2 * var_angle) * var_speed
    54.        
    55.         If ((Cos(var_pi2 * var_angle) * var_speed) = 0) Then var_dist_y = var_speed Else var_dist_y = Cos(var_pi2 * var_angle) * var_speed
    56.    
    57.     End If
    58.    
    59.     lblstuff.Caption = ""
    60.    
    61. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Oh yes and the ship is then drawn by this awful piece of code


    VB Code:
    1. If (var_angle = 0) Then
    2.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_0, 0, 0, vbSrcInvert
    3.             End If
    4.             If (var_angle = 10) Then
    5.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_10, 0, 0, vbSrcInvert
    6.             End If
    7.             If (var_angle = 20) Then
    8.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_20, 0, 0, vbSrcInvert
    9.             End If
    10.             If (var_angle = 30) Then
    11.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_30, 0, 0, vbSrcInvert
    12.             End If
    13.             If (var_angle = 40) Then
    14.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_40, 0, 0, vbSrcInvert
    15.             End If
    16.             If (var_angle = 50) Then
    17.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_50, 0, 0, vbSrcInvert
    18.             End If
    19.             If (var_angle = 60) Then
    20.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_60, 0, 0, vbSrcInvert
    21.             End If
    22.             If (var_angle = 70) Then
    23.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_70, 0, 0, vbSrcInvert
    24.             End If
    25.             If (var_angle = 80) Then
    26.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_80, 0, 0, vbSrcInvert
    27.             End If
    28.             If (var_angle = 90) Then
    29.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_90, 0, 0, vbSrcInvert
    30.             End If
    31.             If (var_angle = 100) Then
    32.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_100, 0, 0, vbSrcInvert
    33.             End If
    34.             If (var_angle = 110) Then
    35.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_110, 0, 0, vbSrcInvert
    36.             End If
    37.             If (var_angle = 120) Then
    38.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_120, 0, 0, vbSrcInvert
    39.             End If
    40.             If (var_angle = 130) Then
    41.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_130, 0, 0, vbSrcInvert
    42.             End If
    43.             If (var_angle = 140) Then
    44.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_140, 0, 0, vbSrcInvert
    45.             End If
    46.             If (var_angle = 150) Then
    47.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_150, 0, 0, vbSrcInvert
    48.             End If
    49.             If (var_angle = 160) Then
    50.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_160, 0, 0, vbSrcInvert
    51.             End If
    52.             If (var_angle = 170) Then
    53.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_170, 0, 0, vbSrcInvert
    54.             End If
    55.             If (var_angle = 180) Then
    56.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_180, 0, 0, vbSrcInvert
    57.             End If
    58.             If (var_angle = 190) Then
    59.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_190, 0, 0, vbSrcInvert
    60.             End If
    61.             If (var_angle = 200) Then
    62.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_200, 0, 0, vbSrcInvert
    63.             End If
    64.             If (var_angle = 210) Then
    65.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_210, 0, 0, vbSrcInvert
    66.             End If
    67.             If (var_angle = 220) Then
    68.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_220, 0, 0, vbSrcInvert
    69.             End If
    70.             If (var_angle = 230) Then
    71.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_230, 0, 0, vbSrcInvert
    72.             End If
    73.             If (var_angle = 240) Then
    74.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_240, 0, 0, vbSrcInvert
    75.             End If
    76.             If (var_angle = 250) Then
    77.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_250, 0, 0, vbSrcInvert
    78.             End If
    79.             If (var_angle = 260) Then
    80.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_260, 0, 0, vbSrcInvert
    81.             End If
    82.             If (var_angle = 270) Then
    83.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_270, 0, 0, vbSrcInvert
    84.             End If
    85.             If (var_angle = 280) Then
    86.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_280, 0, 0, vbSrcInvert
    87.             End If
    88.             If (var_angle = 290) Then
    89.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_290, 0, 0, vbSrcInvert
    90.             End If
    91.             If (var_angle = 300) Then
    92.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_300, 0, 0, vbSrcInvert
    93.             End If
    94.             If (var_angle = 310) Then
    95.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_310, 0, 0, vbSrcInvert
    96.             End If
    97.             If (var_angle = 320) Then
    98.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_320, 0, 0, vbSrcInvert
    99.             End If
    100.             If (var_angle = 330) Then
    101.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_330, 0, 0, vbSrcInvert
    102.             End If
    103.             If (var_angle = 340) Then
    104.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_340, 0, 0, vbSrcInvert
    105.             End If
    106.             If (var_angle = 350) Then
    107.                 BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_350, 0, 0, vbSrcInvert
    108.             End If


    dc_ship_[0-350]: these are a collection of numbers with the device context of the ship at each angle.
    It was then later replaced with an array

    VB Code:
    1. Public dc_ship(0 To 35) As Long

    And the device contexts in that array were generated as follows :
    VB Code:
    1. dc_ship(0) = GenerateDC(App.Path & "\images\ship_0.bmp")
    2.     For i = 1 To 35
    3.         dc_ship(i) = GenerateDC(App.Path & "\images\ship_" & i & "0.bmp")
    4.     Next i
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    That's wonderful! I'll go threw all the code and see what I can make out of it all. Thanks again everyone

    Walter
    Anticipation of death is worse than death itself

  12. #12
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    let us know how you get on.
    if you have any problems just post back here
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    Ok, think I've managed to do it! Now all I need to do is test it out...someone know where I can get some "royalty free" game characters?? I'm absolutely hopeless at drawing on computers!

    Walter
    Anticipation of death is worse than death itself

  14. #14
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Try looking at the games section of www.vbexplorer.com

Posting Permissions

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



Click Here to Expand Forum to Full Width