PDA

Click to See Complete Forum and Search --> : Make my player face the mouse?


Walter
Aug 10th, 2002, 09:52 AM
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

CyberCarsten
Aug 10th, 2002, 10:58 AM
I have some example code for a 2D shooter which uses the mouse, but its in C++ though....let me know if want it.

Walter
Aug 10th, 2002, 11:09 AM
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 walter@totalfraghead.co.uk please. Thanks again! :)

Walter

Walter
Aug 10th, 2002, 11:18 AM
Anyone know how to do this in VB??

Jotaf98
Aug 10th, 2002, 06:19 PM
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 ;)

Walter
Aug 11th, 2002, 08:15 AM
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. :D

Walter

Jotaf98
Aug 11th, 2002, 11:15 AM
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 :)

Walter
Aug 11th, 2002, 11:36 AM
Thankyou. Couldn't find it in a post so I've emailed him (at the only address I could find). Thanks again Jotaf :cool:

Walter

plenderj
Aug 13th, 2002, 02:54 AM
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/client_gettickcount_bitblt.zip
But do remember, this is a very old version, so the code isn't the greatest.


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)

If (doloop = False) And (var_status = 1) Then

doloop = True
RunGameLoop

End If

''' For i = ccl.LBound To ccl.ubound
''' If ccl(i).FillColor = &HFF0000 Then ccl(i).FillColor = &H80FFFF
''' Next i
''' For i = ccl_stn.LBound To ccl_stn.ubound
''' If ccl_stn(i).FillColor = &HFF0000 Then ccl_stn(i).FillColor = &H40C0&
''' Next i

If (var_status <> 0) Then

var_speed = Sqr(((mewidth / 2) - x) ^ 2 + ((meheight / 2) - y) ^ 2)
lblspeed = var_speed

var_temp_x = x
var_temp_y = y
var_cords_x = (mewidth / 2) - x
var_cords_y = (meheight / 2) - y

If (var_cords_y <> 0) Then
var_angle = (Atn((var_cords_x * -1) / var_cords_y) * var_pi)
Else: var_cords_y = 0.001
End If

If (var_angle < 0) Then var_angle = var_angle * -1

If (var_angle = 0) And (var_cords_x = 0) And (var_cords_y < 0) Then var_angle = 180

If (var_cords_x < 0) Then
If (var_cords_y > 0) Then
var_angle = var_angle
ElseIf (var_cords_y < 0) Then
var_angle = 90 - var_angle
var_angle = var_angle + 90
End If
ElseIf (var_cords_x > 0) Then
If (var_cords_y > 0) Then
var_angle = 270 - var_angle
var_angle = var_angle + 90
ElseIf (var_cords_y < 0) Then
var_angle = var_angle + 180
End If
End If

var_angle = (Int(Int(var_angle) / 10) * 10)
var_dist_x = Sin(var_pi2 * var_angle) * var_speed

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

End If

lblstuff.Caption = ""

End Sub

plenderj
Aug 13th, 2002, 02:58 AM
Oh yes and the ship is then drawn by this awful piece of code



If (var_angle = 0) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_0, 0, 0, vbSrcInvert
End If
If (var_angle = 10) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_10, 0, 0, vbSrcInvert
End If
If (var_angle = 20) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_20, 0, 0, vbSrcInvert
End If
If (var_angle = 30) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_30, 0, 0, vbSrcInvert
End If
If (var_angle = 40) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_40, 0, 0, vbSrcInvert
End If
If (var_angle = 50) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_50, 0, 0, vbSrcInvert
End If
If (var_angle = 60) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_60, 0, 0, vbSrcInvert
End If
If (var_angle = 70) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_70, 0, 0, vbSrcInvert
End If
If (var_angle = 80) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_80, 0, 0, vbSrcInvert
End If
If (var_angle = 90) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_90, 0, 0, vbSrcInvert
End If
If (var_angle = 100) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_100, 0, 0, vbSrcInvert
End If
If (var_angle = 110) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_110, 0, 0, vbSrcInvert
End If
If (var_angle = 120) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_120, 0, 0, vbSrcInvert
End If
If (var_angle = 130) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_130, 0, 0, vbSrcInvert
End If
If (var_angle = 140) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_140, 0, 0, vbSrcInvert
End If
If (var_angle = 150) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_150, 0, 0, vbSrcInvert
End If
If (var_angle = 160) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_160, 0, 0, vbSrcInvert
End If
If (var_angle = 170) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_170, 0, 0, vbSrcInvert
End If
If (var_angle = 180) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_180, 0, 0, vbSrcInvert
End If
If (var_angle = 190) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_190, 0, 0, vbSrcInvert
End If
If (var_angle = 200) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_200, 0, 0, vbSrcInvert
End If
If (var_angle = 210) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_210, 0, 0, vbSrcInvert
End If
If (var_angle = 220) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_220, 0, 0, vbSrcInvert
End If
If (var_angle = 230) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_230, 0, 0, vbSrcInvert
End If
If (var_angle = 240) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_240, 0, 0, vbSrcInvert
End If
If (var_angle = 250) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_250, 0, 0, vbSrcInvert
End If
If (var_angle = 260) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_260, 0, 0, vbSrcInvert
End If
If (var_angle = 270) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_270, 0, 0, vbSrcInvert
End If
If (var_angle = 280) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_280, 0, 0, vbSrcInvert
End If
If (var_angle = 290) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_290, 0, 0, vbSrcInvert
End If
If (var_angle = 300) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_300, 0, 0, vbSrcInvert
End If
If (var_angle = 310) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_310, 0, 0, vbSrcInvert
End If
If (var_angle = 320) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_320, 0, 0, vbSrcInvert
End If
If (var_angle = 330) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_330, 0, 0, vbSrcInvert
End If
If (var_angle = 340) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_340, 0, 0, vbSrcInvert
End If
If (var_angle = 350) Then
BitBlt Me.hdc, 450, 300, 81, 81, dc_ship_350, 0, 0, vbSrcInvert
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


Public dc_ship(0 To 35) As Long


And the device contexts in that array were generated as follows :

dc_ship(0) = GenerateDC(App.Path & "\images\ship_0.bmp")
For i = 1 To 35
dc_ship(i) = GenerateDC(App.Path & "\images\ship_" & i & "0.bmp")
Next i

Walter
Aug 13th, 2002, 11:09 AM
That's wonderful! :D I'll go threw all the code and see what I can make out of it all. Thanks again everyone :cool:

Walter

plenderj
Aug 13th, 2002, 11:13 AM
let us know how you get on.
if you have any problems just post back here :)

Walter
Aug 17th, 2002, 08:13 AM
Ok, think I've managed to do it! :D 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

amitabh
Aug 17th, 2002, 11:42 PM
Try looking at the games section of www.vbexplorer.com