speed control

wild wolf
10-05-2001, 05:40 AM
Once more iam here with a new question, my last question was neatly and perfectly explained by Ad1, thanx buddy.
I got two questions this time


1. Speed control. This is the code that iam using to control the speed of my car in other words if the user keeps the up key pressed for a specific time, the speed increases. The speed increamented is shown with the effect of the middle line on the road moving down. If the key is pressed for long it will increase the speed of the lines coming down. My car will stay at the base, such that the final simulation will make it look like the car is moving. (iam using a ghost car as a flag and it moves up the screen and when it reaches the top, the speed of the lines is increamented and the position of the ghost car is reset to the base and it goes on. Visibility of the ghost car is set to false
this code is under the function keydown

dim counter as integer
dim flag as integer

flag = 8
if keycode = vbkeyup then ghostcar.top = ghostcar.top - 8
if keycode = vbkeyup and ghostcar.top + ghostcar.height < 0 then
while counter < 12 (i have 12 middle lines)
middleline(counter) = middleline(counter) + flag
counter = counter + 1
wend
ghostcar.top = 750 (750 is the base value of the form)
flag = flag + 8
end if


the above code will only function if i remove the line where i code ghostcar.top = 750 and that means speed is increamented only once. if the line is there, then there is no effect to the speed, can i know the possible error in this matter? or any other solution for the speed increamented which can be easier then mine?

2. Collision detection. Can u help me in this matter as iam completely blur on how to do it, i have an idea but it is very complicated and i have tried it more then ten times!!

Ad1
10-05-2001, 06:00 AM
not really sure what's going on in your code, maybe you could attatch it to a post

anyway if you want to do acceleration then instead of saying - 8 use an integer - intSpeed and alter intspeed's value to adjust the acceleration

as for collision detection there are several ways to do this (try searching this forum), the easiest is probably to check if one object is within the edges of another ie
<pre><font color=blue>If</font color=blue> picCar.Left < picObject.Left + picObject.Width <font color=blue>And</font color=blue> picCar.Left + picCar.Width > picObject.Left <font color=blue>Then</font color=blue>
<font color=blue>If</font color=blue> picCar.Top < picObject.Top + picObject.Height <font color=blue>And</font color=blue> picCar.Top + picCar.Height > picObject.Top <font color=blue>Then</font color=blue>
MsgBox "crash"
<font color=blue>End If</font color=blue>
<font color=blue>End If</font color=blue></pre>

Computer_Guy
10-07-2001, 10:08 PM
Your game sounds interesting, could I get a copy sometime, my e-mail is jollyrogers3@yahoo.com, just send an exe of it. I'll see if I can help with the code at all, too

wild wolf
10-10-2001, 01:43 AM
Thanx Ad1, the collision script is working but the only problem is that once it collides, the car position is set to the end of the direction it is moving in, i did not have my code here with me so i couldnt attach it with this post but in my next post i will surely attach it for you to have a look, thanx anyway for helping, i appreciate it !

Fear is in the eye of the beholder, dont let it be you!!

wild wolf
10-13-2001, 11:18 AM
hey Ad1 , here is my code, and iam also attaching an exe file of the game for your ref


Dim speed As Integer


Private Declare Function sndPlaySound Lib "WINMM.DLL" Alias _
"sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As _
Long) As Long

Const SND_SYNC = &amp;H0
Const SND_ASYNC = &amp;H1
Const SND_NODEFAULT = &amp;H2
Const SND_LOOP = &amp;H8
Const SND_NOSTOP = &amp;H10


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then Timer1.Enabled = True
If KeyCode = vbKeyLeft Then Timer2.Enabled = True
If KeyCode = vbKeyRight Then Timer3.Enabled = True
collision
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim counter As Integer
Dim counter1 As Integer

If KeyCode = vbKeyUp Then Timer1.Enabled = False
If KeyCode = vbKeyLeft Then Timer2.Enabled = False
If KeyCode = vbKeyRight Then Timer3.Enabled = False

While counter &lt; 3
If KeyCode = vbKeyUp And traffic(counter).Top + traffic(counter).Height &gt; 850 Then traffic(counter).Top = 1066
counter = counter + 1
Wend
End Sub

Private Sub Form_Load()
SoundName$ = "E:\myfiles\cs\Project\Lost In Space.wav"
wFlags% = SND_ASYNC Or SND_NODEFAULT
x% = sndPlaySound(SoundName$, wFlags%)

opptime.Interval = 10
oppchecker.Interval = 10
Timer1.Interval = 10
Timer2.Interval = 10
Timer3.Interval = 10
End Sub

Private Sub oppchecker_Timer()
Dim counter As Integer
Dim middlecounter As Integer
Dim counter2 As Integer
Dim oppcounter As Integer
Dim lightcounter As Integer

While counter &lt; 3
If traffic(counter).Top + traffic(counter).Height &lt; 0 Then
traffic(counter).Top = 850
End If
counter = counter + 1
Wend

While oppcounter &lt; 3
If oppcar(oppcounter).Top + oppcar(oppcounter).Height &gt; 850 Then
oppcar(oppcounter).Top = 0
End If
oppcounter = oppcounter + 1
Wend

While middlecounter &lt; 12
If middleline(middlecounter).Top + middleline(middlecounter).Height &gt; 850 Then
middleline(middlecounter).Top = 0
End If
middlecounter = middlecounter + 1
Wend


While lightcounter &lt; 5
If light(lightcounter).Top + light(lightcounter).Height &gt; 850 Then
light(lightcounter).Top = 0
End If
lightcounter = lightcounter + 1
Wend
collision

While counter2 &lt; 6
If oppositecar(counter2).Top + oppositecar(counter2).Height &gt; 850 Then
oppositecar(counter2).Top = 0
End If
counter2 = counter2 + 1
Wend

End Sub

Private Sub opptime_Timer()
Dim counter As Integer
Dim oppspeed As Integer
Dim oppspeed1 As Integer
Dim counter1 As Integer

While counter &lt; 3
oppspeed = 1 + Int(2 * Rnd())
traffic(counter).Top = traffic(counter).Top - oppspeed - 2
oppcar(counter).Top = oppcar(counter).Top - oppspeed - 2
counter = counter + 1
Wend

While counter1 &lt; 6
oppspeed1 = 1 + Int(5 * Rnd())
oppositecar(counter1).Top = oppositecar(counter1).Top + oppspeed + 5
counter1 = counter1 + 1
Wend

End Sub

Private Sub Timer1_Timer()
Dim counter As Integer
Dim counter1 As Integer
Dim lightcounter As Integer
Dim counter2 As Integer


While counter &lt; 3
traffic(counter).Top = traffic(counter).Top + 8
counter = counter + 1
Wend

While counter2 &lt; 3
oppcar(counter2).Top = oppcar(counter2).Top + 8
counter2 = counter2 + 1
Wend

speed = 8
'If speed &lt; 100 Then
While counter1 &lt; 12
middleline(counter1).Top = middleline(counter1).Top + speed
counter1 = counter1 + 1
'speed = speed + 2
Wend
'End If

While lightcounter &lt; 5
light(lightcounter).Top = light(lightcounter).Top + speed
lightcounter = lightcounter + 1
Wend

counter = 0
counter1 = 0
counter2 = 0
lightcounter = 0
End Sub

Private Sub Timer2_Timer()
maincar.Left = maincar.Left - 5
leftcollision
End Sub

Private Sub Timer3_Timer()
maincar.Left = maincar.Left + 5
rightcollision
End Sub

Sub leftcollision()

If maincar.Left - sideline(0).Left &lt; 0 Then maincar.Left = 304

End Sub

Sub rightcollision()

If maincar.Left - sideline(1).Left &gt; 0 Then maincar.Left = 690

End Sub
Sub collision()
Dim lightcounter As Integer
Dim middlecounter As Integer

While lightcounter &lt; 5
If maincar.Left &lt; light(lightcounter).Left + light(lightcounter).Width And maincar.Left + maincar.Width &gt; light(lightcounter).Left Then
If maincar.Top &lt; light(lightcounter).Top + light(lightcounter).Height And maincar.Top + maincar.Height &gt; light(lightcounter).Top Then
maincar.Left = 592
'MsgBox "crash"
End If
End If
lightcounter = lightcounter + 1
Wend
End Sub



Dim speed As Integer


Private Declare Function sndPlaySound Lib "WINMM.DLL" Alias _
"sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As _
Long) As Long

Const SND_SYNC = &amp;H0
Const SND_ASYNC = &amp;H1
Const SND_NODEFAULT = &amp;H2
Const SND_LOOP = &amp;H8
Const SND_NOSTOP = &amp;H10


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then Timer1.Enabled = True
If KeyCode = vbKeyLeft Then Timer2.Enabled = True
If KeyCode = vbKeyRight Then Timer3.Enabled = True
collision
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim counter As Integer
Dim counter1 As Integer

If KeyCode = vbKeyUp Then Timer1.Enabled = False
If KeyCode = vbKeyLeft Then Timer2.Enabled = False
If KeyCode = vbKeyRight Then Timer3.Enabled = False

While counter &lt; 3
If KeyCode = vbKeyUp And traffic(counter).Top + traffic(counter).Height &gt; 850 Then traffic(counter).Top = 1066
counter = counter + 1
Wend
End Sub

Private Sub Form_Load()
SoundName$ = "E:\myfiles\cs\Project\Lost In Space.wav"
wFlags% = SND_ASYNC Or SND_NODEFAULT
x% = sndPlaySound(SoundName$, wFlags%)

opptime.Interval = 10
oppchecker.Interval = 10
Timer1.Interval = 10
Timer2.Interval = 10
Timer3.Interval = 10
End Sub

Private Sub oppchecker_Timer()
Dim counter As Integer
Dim middlecounter As Integer
Dim counter2 As Integer
Dim oppcounter As Integer
Dim lightcounter As Integer

While counter &lt; 3
If traffic(counter).Top + traffic(counter).Height &lt; 0 Then
traffic(counter).Top = 850
End If
counter = counter + 1
Wend

While oppcounter &lt; 3
If oppcar(oppcounter).Top + oppcar(oppcounter).Height &gt; 850 Then
oppcar(oppcounter).Top = 0
End If
oppcounter = oppcounter + 1
Wend

While middlecounter &lt; 12
If middleline(middlecounter).Top + middleline(middlecounter).Height &gt; 850 Then
middleline(middlecounter).Top = 0
End If
middlecounter = middlecounter + 1
Wend


While lightcounter &lt; 5
If light(lightcounter).Top + light(lightcounter).Height &gt; 850 Then
light(lightcounter).Top = 0
End If
lightcounter = lightcounter + 1
Wend
collision

While counter2 &lt; 6
If oppositecar(counter2).Top + oppositecar(counter2).Height &gt; 850 Then
oppositecar(counter2).Top = 0
End If
counter2 = counter2 + 1
Wend

End Sub

Private Sub opptime_Timer()
Dim counter As Integer
Dim oppspeed As Integer
Dim oppspeed1 As Integer
Dim counter1 As Integer

While counter &lt; 3
oppspeed = 1 + Int(2 * Rnd())
traffic(counter).Top = traffic(counter).Top - oppspeed - 2
oppcar(counter).Top = oppcar(counter).Top - oppspeed - 2
counter = counter + 1
Wend

While counter1 &lt; 6
oppspeed1 = 1 + Int(5 * Rnd())
oppositecar(counter1).Top = oppositecar(counter1).Top + oppspeed + 5
counter1 = counter1 + 1
Wend

End Sub

Private Sub Timer1_Timer()
Dim counter As Integer
Dim counter1 As Integer
Dim lightcounter As Integer
Dim counter2 As Integer


While counter &lt; 3
traffic(counter).Top = traffic(counter).Top + 8
counter = counter + 1
Wend

While counter2 &lt; 3
oppcar(counter2).Top = oppcar(counter2).Top + 8
counter2 = counter2 + 1
Wend

speed = 8
'If speed &lt; 100 Then
While counter1 &lt; 12
middleline(counter1).Top = middleline(counter1).Top + speed
counter1 = counter1 + 1
'speed = speed + 2
Wend
'End If

While lightcounter &lt; 5
light(lightcounter).Top = light(lightcounter).Top + speed
lightcounter = lightcounter + 1
Wend

counter = 0
counter1 = 0
counter2 = 0
lightcounter = 0
End Sub

Private Sub Timer2_Timer()
maincar.Left = maincar.Left - 5
leftcollision
End Sub

Private Sub Timer3_Timer()
maincar.Left = maincar.Left + 5
rightcollision
End Sub

Sub leftcollision()

If maincar.Left - sideline(0).Left &lt; 0 Then maincar.Left = 304

End Sub

Sub rightcollision()

If maincar.Left - sideline(1).Left &gt; 0 Then maincar.Left = 690

End Sub
Sub collision()
Dim lightcounter As Integer
Dim middlecounter As Integer

While lightcounter &lt; 5
If maincar.Left &lt; light(lightcounter).Left + light(lightcounter).Width And maincar.Left + maincar.Width &gt; light(lightcounter).Left Then
If maincar.Top &lt; light(lightcounter).Top + light(lightcounter).Height And maincar.Top + maincar.Height &gt; light(lightcounter).Top Then
maincar.Left = 592
'MsgBox "crash"
End If
End If
lightcounter = lightcounter + 1
Wend
End Sub

wild wolf
10-13-2001, 11:33 AM
Post deleted by wild wolf

wild wolf
10-13-2001, 11:39 AM
Post deleted by wild wolf

BillSoo
10-13-2001, 11:52 AM
It is against the posting guidelines to attach EXEs. Just attach source code.

Squirm
10-13-2001, 12:20 PM
Yeah, I was gonna say that too, but thought against it, as you probably wanted to allow only Ad1 to see the source and not anyone who cares to d/l it...... but in essence, if you read the posting guidelines, EXEs and OCXs are generally frowned upon, EXEs more so........

wild wolf
10-13-2001, 12:31 PM
iam really sorry for that,is there anyway i can withdraw the attachment?

Ad1
10-15-2001, 03:32 AM
hi wild wolf, attach your project and I will take a look,

like has been mentioned exe's are no good, 1)due to paranoia of downloading and 2)you can't debug and step through an exe

wild wolf
10-15-2001, 05:03 AM
ok ill attach the whole project, zipped ( i hope i can attach zip files)

Ad1
10-15-2001, 05:29 AM
your game looks nice, but what do you want it to do when you collide ?, at the moment you set it to a particular position "maincar.Left = 592",
you could change that to "maincar.Left = maincar.Left +/- 50" to give the effect of bouncing, or you could have a loop/timer which fires and runs a crash sequence ie the car spins and stops and they have to start again

wild wolf
10-15-2001, 10:32 AM
hey thats a good idea of sending the car spining, actually my initail idea was to set speed to 0 again once u collide, but i think spining the car will be a great, but do u have any idea on how that will be done? the sound i wan to use DirectX. i have updated the progam, and tomorow i will upload the updated version in which speed has also been controlled.

Ad1
10-15-2001, 10:48 AM
I don't know anything about DirectX, but you should be able to knock up a decent enough animation just using vb, I've attatched a simple demo of how to do the animation, just replace the frames with frames of your car spinning

wild wolf
10-15-2001, 12:48 PM
that code is quite decent, i never thought of it, its good and i am gonna use it for the collision. One thing i want u to notice in my code is, when the car collides with the middlelight, the code only works when it collides on the side, but when the car collides from the bottom of the light it goes through, what do u think - as per the code - is the problem?

Squirm
10-15-2001, 01:48 PM
wild wolf - I just wanna congratualte you on such a great looking project. Its looking very promising. I suggest to speed up the animation by using more API, or DirectX, but otherwise way cool..... keep it up!

wild wolf
10-16-2001, 02:26 AM
thanx squirm, thats gonna keep up my spirits and goona boost up my moral. I am going to convert all the animations and sounds using DirectX, iam still learning on it, (hope it does not take long as i got only 1 more month to complete this game)

wild wolf
10-16-2001, 02:42 AM
hey yes i forgot 1 thing, this game is based on a server - client, meaning its a multiplayer game, so, Ad1 adn Squirm, u have seen my code, do u think i need to declare all my varialbes globally or leave them the way they r? coz i fear i will have problems sending and receiving data

Ad1
10-16-2001, 03:25 AM
well in the code I've got from your attatchment you need to call Sub collision() from timer1, and you will also need to uncomment the code in Sub collision() and then it should work

Ad1
10-16-2001, 03:29 AM
I don't know about the server client thing I haven't messed around with that, you may want to have some global or you may be able to pass them as parameters

wild wolf
10-16-2001, 03:44 AM
hey discard the function sub collision() no more in my code

Ad1
10-16-2001, 03:59 AM
well you'll need to use some collision detection in timer1 as at the moment you are only checking for collision when moving sideways

Squirm
10-16-2001, 07:39 AM
Of course, if you're going to move over to DirectX, all that will change anyway........

wild wolf
10-16-2001, 10:08 PM
can u give me a rough idea on what DirectX is all about? i mean i was reading on this part and it seems like its more like system programming where u work based on each and every piixel being used

wild wolf
10-17-2001, 01:25 AM
ok Ad1, look at this code that iam attaching, the change is i have called the function collision from timer1 and then in the function there is the collision function for the middle lights and three opponet cars. Iam having probles yet, i put a check fir the height and top as per the code and set the car to its original pos after collision, but the thing is, it does collide but it will collide if the car hits in the whole section (i mean the whol line in which the opp car is), i think if u compile, then only u will understand what iam trying to say

Ad1
10-17-2001, 04:22 AM
when you are doing collision detection with oppcar you need to check the left and width properties as well as the top/height properties in the same way as you do with the lights

wild wolf
10-17-2001, 05:05 AM
yeah, i realised that and now it is working :) thanx

wild wolf
10-17-2001, 07:49 AM
hey Ad1, iam attaching an updated version os the game, in which the collision works, but there is one bug, the cars will collide but at one stage the collision does not work and wonly works on two cars instead of three, i treid debuggint it but i just dont seem to get the bug, can u please help? i hope iam not bugging u

Ad1
10-17-2001, 08:33 AM
I think the problem was caused by using the wrong variable, change the line
If maincar.Left &lt; oppcar(<font color=red>Counter</font color=red>).Left + oppcar(<font color=red>Counter</font color=red>).Width And maincar.Left + maincar.Width &gt; oppcar(oppcounter).Left Then

If maincar.Left &lt; oppcar(<font color=red>oppcounter</font color=red>).Left + oppcar(<font color=red>oppcounter</font color=red>).Width And maincar.Left + maincar.Width &gt; oppcar(oppcounter).Left Then

wild wolf
10-18-2001, 12:47 AM
nope din make any diff

Ad1
10-18-2001, 03:31 AM
can you explain to me in a bit more detail exactly what the problem is

wild wolf
10-18-2001, 03:45 AM
ok, the probe is this, if u execute the game u will notice on ur right there is one car, middle one car and the left one, and then on the otherside of the roasd are opposite coming traffic, well the car on the far right is the problem, collision on its side works but from collision from below does not work, even if i changed the car and put another one there its teh same problem

Ad1
10-18-2001, 03:58 AM
I rechecked it and found what you mean, changing Counter to oppcounter in Sub Collision solves the problem, try it again

wild wolf
10-22-2001, 05:37 AM
i think it works, thanks! but when the tough get going the goint gets tough, i mean once solved that problem its finished almost my project but there is an un-known bug whihc i cant just get a hold on, iam attaching the code again, the problem is, in the sub collision u will see i have deactivated partial code, the reason is coz whenever there is a collision the thing just hangs, i dun know the reason, i tried all ways i could. the second problem i guess will be peanuts for u, multiple key pressed again, u will see on your right there is a text box that displays your speed, the speed is working normal but once u press the left or right key it goes back to neutral, i know i havnet put in the check for it, this is because i dun know how to put it :( , hope u understand what i mean.

wild wolf
10-22-2001, 05:39 AM
oops i forgot to attach the file, here it is :)

Ad1
10-22-2001, 06:01 AM
1) the "hang" was caused by setting <font color=blue>oppcounter = 0</font color=blue> in your loop <font color=blue>While oppcounter &lt; 3</font color=blue>making it neverending

2) in the key up event you set <font color=blue>speedg.Caption = "N"</font color=blue> for every key up event, this wants to be placed in the <pre><font color=blue>If</font color=blue> KeyCode = vbKeyUp <font color=blue>Then</font color=blue></pre> statement so that it only fires when you stop going forward

wild wolf
10-22-2001, 06:08 AM
wow man ur a genius!! i din notice that, thanx very much man

Ad1
10-22-2001, 06:21 AM
no problem, heh man flattery gets you every where images/icons/wink.gif

wild wolf
10-23-2001, 02:54 AM
boooom, iam back with another crazy problem, i converted all the images into pictures, but after doing that, the key function does not work, why? iam attaching the file for ur ref :)

Flyguy
10-23-2001, 03:08 AM
in the form "game" set the KeyPreview property to True

wild wolf
10-23-2001, 03:13 AM
hey thanx man :)

wild wolf
10-23-2001, 03:17 AM
but 1 thing, did u notice a bug? the collision function does not work on the car on the far right, what do u think is the problem? (its oppcar(1))

Flyguy
10-23-2001, 03:21 AM
I did notice something else I recall strange images/icons/smile.gif.
If you are not moving the cars on the left lane are driving backwards...

wild wolf
10-23-2001, 03:28 AM
oh hehehe, i was still editing the icons for that so i just put in default ;)

wild wolf
10-23-2001, 03:34 AM
nevremind i found the bug it :)

wild wolf
10-23-2001, 11:09 AM
how do we set a picture box transparent?

BillSoo
10-24-2001, 01:49 PM
Pictureboxes cannot be transparent. But you can simulate it by copying the background behind the picturebox and pasting it into the picturebox.

wild wolf
10-25-2001, 09:57 AM
yeah i did that, has proved of some help though its limited coz the picture overlaps another so u can see the background(thats the flaw)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum