how to Spin a triangle in a circle?

andrewo
10-25-2001, 03:35 AM
I've seen examples of triangles spinnin in circles and have had a look at the code but dont fully understand..
this is what i know

You have a main point with 3 other points equally distance from the middle and each other

then u move the 3 points around the middle point while still being eually distance from the middle and each other

if someone has done this before could you show some example code

andrewo
10-25-2001, 03:39 AM
heres the code i looked at


Option Explicit

'The gettickcount API delcaration
Private Declare Function GetTickCount Lib "kernel32" () As Long

Const PI = 3.14159 'Mmmm.. Pi
Const ACCEL = 0.1 'Rate of increase of speed
Const ROTATION_RATE = 15 'Rotation speed
Const SHIP_RADIUS = 10 'Distance from center of triangle to any vertex
Const MS_DELAY = 25 'Milliseconds per frame (25 = 40 frames per second)

Dim mlngTimer As Long 'Holds system time since last frame was displayed
Dim msngFacing As Single 'Angle the ship is facing (ok, ok, it's a triangle, not a ship! Shut it!)
Dim msngHeading As Single 'Current direction in which ship is moving
Dim msngSpeed As Single 'Current speed with which ship is moving
Dim msngX As Single 'Current X coordinate of ship within form
Dim msngY As Single 'Current Y coordinate of ship within form
Dim mblnRunning As Boolean 'Is the render loop running?
Dim mblnLeftKey As Boolean 'Is the left arrow-key depressed?
Dim mblnRightKey As Boolean 'Is the right arrow-key depressed?
Dim mblnUpKey As Boolean 'Is the up arrow-key depressed? ... Are YOU depressed? Fly my triangle around for a while, you'll feel better!

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

'Check for keypresses
If KeyCode = vbKeyLeft And Not mblnRightKey Then mblnLeftKey = True
If KeyCode = vbKeyRight And Not mblnLeftKey Then mblnRightKey = True
If KeyCode = vbKeyUp Then mblnUpKey = True

End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)

'Check for keyreleases
If KeyCode = vbKeyLeft Then mblnLeftKey = False
If KeyCode = vbKeyRight Then mblnRightKey = False
If KeyCode = vbKeyUp Then mblnUpKey = False

End Sub

Private Sub Form_Load()

'Initialize the variables
msngX = 200
msngY = 200
msngFacing = 0
msngSpeed = 0
msngHeading = 0
mlngTimer = GetTickCount()
mblnRunning = True

'Display the form
DrawShip
frmShip.Show

'Start the render loop
Do While mblnRunning
'Check if we've waited for the appropriate number of milliseconds
If mlngTimer + MS_DELAY <= GetTickCount() Then
mlngTimer = GetTickCount() 'Reset the timer variable
Physics 'Allow the ship's location to be updated
DrawShip 'Draw the ship
End If
'Allow other events to occur
DoEvents
Loop

End Sub

Private Sub Physics()

Dim sngXComp As Single 'Resultant X and Y components
Dim sngYComp As Single

'Rotate right
If mblnRightKey Then
msngFacing = msngFacing + ROTATION_RATE * PI / 180
End If

'Rotate left
If mblnLeftKey Then
msngFacing = msngFacing - ROTATION_RATE * PI / 180
End If

'Thrust
If mblnUpKey Then
'Determine the X and Y components of the resultant vector
sngXComp = msngSpeed * Sin(msngHeading) + ACCEL * Sin(msngFacing)
sngYComp = msngSpeed * Cos(msngHeading) + ACCEL * Cos(msngFacing)
'Determine the resultant speed
msngSpeed = Sqr(sngXComp ^ 2 + sngYComp ^ 2)
'Calculate the resultant heading, and adjust for arctangent by adding Pi if necessary
If sngYComp >= 0 Then msngHeading = Atn(sngXComp / sngYComp)
If sngYComp < 0 Then msngHeading = Atn(sngXComp / sngYComp) + PI
End If

'Adjust coordinates w.r.t. speed
msngX = msngX + msngSpeed * Sin(msngHeading)
msngY = msngY - msngSpeed * Cos(msngHeading)

'Ensure that the ship is visible in the form (wrap edges)
If msngX > frmShip.ScaleWidth Then msngX = 0
If msngY > frmShip.ScaleHeight Then msngY = 0
If msngX < 0 Then msngX = frmShip.ScaleWidth
If msngY < 0 Then msngY = frmShip.ScaleHeight

End Sub

Private Sub DrawShip()

Dim intX1 As Integer 'Coordinates of the 3 triangle verticies
Dim intY1 As Integer
Dim intX2 As Integer
Dim intY2 As Integer
Dim intX3 As Integer
Dim intY3 As Integer

'Calculate the new ship vertices
intX1 = msngX + SHIP_RADIUS * Sin(msngFacing)
intY1 = msngY - SHIP_RADIUS * Cos(msngFacing)
intX2 = msngX + SHIP_RADIUS * Sin(msngFacing + 2 * PI / 3)
intY2 = msngY - SHIP_RADIUS * Cos(msngFacing + 2 * PI / 3)
intX3 = msngX + SHIP_RADIUS * Sin(msngFacing + 4 * PI / 3)
intY3 = msngY - SHIP_RADIUS * Cos(msngFacing + 4 * PI / 3)

'Assign the vertices to the three lines drawn on the form
Line1.X1 = intX1
Line1.Y1 = intY1
Line1.X2 = intX2
Line1.Y2 = intY2
Line2.X1 = intX2
Line2.Y1 = intY2
Line2.X2 = intX3
Line2.Y2 = intY3
Line3.X1 = intX3
Line3.Y1 = intY3
Line3.X2 = intX1
Line3.Y2 = intY1

End Sub

Private Sub Form_Unload(Cancel As Integer)

'Terminate the render loop
mblnRunning = False

End Sub

Squirm
10-25-2001, 06:43 AM
That code works fine, provided you have a form called frmShip (make it quite big) and line controls called Line1, Line2, Line3.

Plus, it (the ship) is a little small, I would suggest you change the value of <font color=blue>Const SHIP_RADIUS</font color=blue> to something like 500, and the value of <font color=blue>Const ACCEL</font color=blue> to something like 0.5.

Then use the normal arrow keys for movement. It works a treat! The physics are excellent.

andrewo
10-26-2001, 01:30 AM
dont worry i worked it out!!!

my ship turns by addin degrees and minus degrees etc
ill post the code later when i've perfected it

Teric
10-26-2001, 08:40 AM
I did a very similar thing with my asteroids program: I had the ship spinning and moving, and the asteroids spinning and moving. Please take a look:

http://www.visualbasicforum.com/bbs/showflat.php?Cat=&amp;Board=gp&amp;Number=38369&amp;page=0&amp;view=collapsed&amp;sb=3&amp;o=9 3&amp;fpart=

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum