(VB2010)Setting multiple moving speeds to one picture

have a code that kind of works. not to sure how to fix

Page 1 of 1

6 Replies - 1341 Views - Last Post: 08 January 2011 - 06:16 PM Rate Topic: -----

#1 holly-namu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-November 10

(VB2010)Setting multiple moving speeds to one picture

Posted 03 January 2011 - 07:17 PM

Pretty much I'm trying to move one picture box to a set time. The time is read from a text file than is outputted by a timer. The picture(Megaman head) is supposed to travel down the page and then when it hits with the image it resets itself and starts again. The problem I am having is that the picture moves to the first time set, but then ignores all the rest and either becomes a continuous loop, or that the picture only moves very slowly.

Here's the Code I used:

Public Class Form1
    Dim f_keyval As Integer
    Dim beat, f_time1(), f_time2(), f_time3(), f_CurSeconds(), f_NewInterval(), f_CurBeat As Integer
    Dim f_direction() As String
    Dim f_score As Integer
    Dim f_minsec, f_sec, f_min As Integer

    Dim loc As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        loc = ("\Computer Programming\gif check\gif check\Resources\kick(1).gif")[color="#006400"]'picture the Megaman hits[/color]

        Me.PictureBox2.Image = Image.FromFile(loc)


        FileOpen(1, "piano(loud)red-lucifer.txt", OpenMode.Input, OpenAccess.Read)[color="#006400"]'sequential file that is read[/color]

        Do Until EOF(1)
            ReDim Preserve f_time1(beat)[color="#006400"]'mins, sec, milisec (f_time1-f_time3) in that order[/color]
            ReDim Preserve f_time2(beat)
            ReDim Preserve f_time3(beat)
            ReDim Preserve f_direction(beat) [color="#006400"]'what picture will be moving[/color]
            ReDim Preserve f_CurSeconds(beat)[color="#006400"]'current seconds that have passed [/color]
            ReDim Preserve f_NewInterval(beat)[color="#006400"]'when the next interval should begin[/color]

            Input(1, f_time1(beat))
            Input(1, f_time2(beat))
            Input(1, f_time3(beat))
            Input(1, f_direction(beat))


            beat += 1

        Loop

        beat -= 1

        For Seconds = 0 To beat

[color="#006400"]'I have it so that the time is read to the certain time. So if I say that the time in the file is 00,01,000,U then the Megaman head will move at 1sec[/color]
            f_time1(Seconds) += 1

            If f_time1(Seconds) = 100 Then
                f_time1(Seconds) = 0
                f_time2(Seconds) += 1

            End If

            If f_time2(Seconds) = 60 Then
                f_time2(Seconds) = 0
                f_time3(Seconds) += 1
            End If

            f_CurSeconds(Seconds) = (f_time1(Seconds) * 60) + f_time2(Seconds) + (f_time3(Seconds) * 100)

        Next

        For a = 1 To beat
            If a = 1 Then
                f_CurSeconds(a) = f_CurSeconds(a) * 100
            End If
            f_CurSeconds(a) = f_CurSeconds(a - 1)[color="#006400"]'makes it so that the time is simply added onto the next[/color]
        Next

        For i = 0 To beat
            f_NewInterval(i) = (f_CurSeconds(i) * 100) / 5[color="#006400"]'represents when the next interval should be[/color]

        Next

        f_CurBeat = 0

        timTime.Enabled = True

        timBeat.Enabled = True

        FileClose(1)


    End Sub


[color="#006400"]'timer code[/color]

    Private Sub timTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timTime.Tick


        For v = 0 To beat
            If f_direction(v) = "U" Then[color="#006400"]'the declaration of what pic should move[/color]

                Megarmans.Top += v[color="#006400"]'Megaman head moves at the speed of beat[/color]

                If Megarmans.Bounds.IntersectsWith(PictureBox2.Bounds) Then
                    Megarmans.Top = 5
                    f_CurBeat += 1
                    timTime.Interval = f_NewInterval(v)
[color="#006400"]'the Megaman head is reset to the top of the form at 5. Then the next time should be read. And then the cycle changes according to the next time.[/color]
                End If
            End If
        Next

    End Sub
End Class



This piece of code is the one that when the second time comes around the picture just inches forward and does not intersect with the other picture in time. I have a timer running at the side to see that the first time does intersect with the picture on time, but is everything thing else that does not.
Also I think that the way I have my text file written out, might also have something to do with it. Here is an examlpe:

min, sec, milisec, picture
00, 01, 000, U
00, 05, 000, U
00, 10, 000, U

I greatly appreciate any help that I can get. I really do want to get this code to work. Thank you. :)

This post has been edited by stayscrisp: 04 January 2011 - 09:28 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: (VB2010)Setting multiple moving speeds to one picture

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: (VB2010)Setting multiple moving speeds to one picture

Posted 04 January 2011 - 04:51 PM

It is common to lock all game actions to 60 frames per second. Are you doing this with every entity?
Was This Post Helpful? 0
  • +
  • -

#3 holly-namu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-November 10

Re: (VB2010)Setting multiple moving speeds to one picture

Posted 04 January 2011 - 06:02 PM

View PostButchDean, on 04 January 2011 - 03:51 PM, said:

It is common to lock all game actions to 60 frames per second. Are you doing this with every entity?



I'm not really sure. How do I check to see if I am?
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: (VB2010)Setting multiple moving speeds to one picture

Posted 04 January 2011 - 06:20 PM

View Postholly-namu, on 05 January 2011 - 12:02 AM, said:

View PostButchDean, on 04 January 2011 - 03:51 PM, said:

It is common to lock all game actions to 60 frames per second. Are you doing this with every entity?



I'm not really sure. How do I check to see if I am?

When people write games they normally are taught that the basic structure is:

1. Initialize.
2. Run game loop.
3. Terminate.

But to be compatible across processor speeds to run at the same rate we actually have:

1. Initialize.
2. Run game loop.
2b. Halt game loop until 1/60 second since last iteration of game loop.
3. Terminate.

so in pseudocode your code structure should look like:

DO InitialiseGame

WHILE(gameExecuting)
    DO HaltFor60fps /* Wastes cycles until 1/60 second has elapst since last iteration. */
    DO UpdateUserInteraction
    DO UpdatePhysics
    DO UpdateGraphics
    DO UpdateAudio

DO TerminateGame



So you may need to alter structure slightly. :)

This post has been edited by ButchDean: 04 January 2011 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#5 holly-namu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-November 10

Re: (VB2010)Setting multiple moving speeds to one picture

Posted 04 January 2011 - 07:51 PM

View PostButchDean, on 04 January 2011 - 05:20 PM, said:

View Postholly-namu, on 05 January 2011 - 12:02 AM, said:

View PostButchDean, on 04 January 2011 - 03:51 PM, said:

It is common to lock all game actions to 60 frames per second. Are you doing this with every entity?



I'm not really sure. How do I check to see if I am?

When people write games they normally are taught that the basic structure is:

1. Initialize.
2. Run game loop.
3. Terminate.

But to be compatible across processor speeds to run at the same rate we actually have:

1. Initialize.
2. Run game loop.
2b. Halt game loop until 1/60 second since last iteration of game loop.
3. Terminate.

so in pseudocode your code structure should look like:

DO InitialiseGame

WHILE(gameExecuting)
    DO HaltFor60fps /* Wastes cycles until 1/60 second has elapst since last iteration. */
    DO UpdateUserInteraction
    DO UpdatePhysics
    DO UpdateGraphics
    DO UpdateAudio

DO TerminateGame



So you may need to alter structure slightly. :)

I'm not quite sure how to apply this halting feature to my code I tried this:
For i = 0 To beat
f_NewInterval(i) = (f_CurSeconds(i) * i)

If f_CurSeconds(i) = 60 Then'when the current seconds equal 60secs the next interval equals when the current seconds 1/60
timTime.Interval = (f_CurSeconds(i) * 1) / 60
End If
Next

I'm not too sure if it works. The speed seems to go in a pattern from fast, medium, slow, then repeat when this code is applied.
Could you clarify where I should apply this halting part to the code? And how I could apply it?
Was This Post Helpful? 0
  • +
  • -

#6 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: (VB2010)Setting multiple moving speeds to one picture

Posted 05 January 2011 - 07:45 PM

I actually just rewrote some of my code to compensate for this problem. I had a timer setup to draw the screen every 1/60th of a second (eg. 60 FPS), and I also ran my animation code every time the screen was drawn. This worked great except when the computer wasn't able to do 60 FPS, so I calculated a animation-speed scale targetFPS / actualFPS and multiplied any constants in my animation code by it.

void display()
{
	static int init = false;
	const double targetFPS = 60;
        static double actualFPS = 60;
	static int framesRendered = 0;	
	
	if (!init)
	{
		frameTimer = dgTimerNew(1.0 / targetFPS);
		fpsTimer = dgTimerNew(1);
		init = true;
	}
	
	const double scale = targetFPS / actualFPS;

        // It's been 1/60th of a second.
	if (TimerReady(frameTimer))
	{
                DoAnimation(scale);
		RenderScene();
		++framesRendered;
	}
	
        // It's been 1 second.
	if (TimerReady(fpsTimer))
	{
		actualFPS = framesRendered;
		framesRendered = 0;
	}
}



void DoAnimation(double scale)
{
	const double s_objSpeed = objSpeed * scale;
	
	float vel[3] = { 0, -1, 0 };
	QuaternionRotate(objRot, vel, vel);
	VectorScale(vel, s_objSpeed);
	VectorAdd(vel, objPos, objPos);
}



If actualFPS is only 30, the animation is half the speed it should be. 60 / 30 = 2 is our scaling factor that compensates for this.

Hope this helps, sorry if it's not exactly answering your original question. :P
Was This Post Helpful? 0
  • +
  • -

#7 holly-namu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-November 10

Re: (VB2010)Setting multiple moving speeds to one picture

Posted 08 January 2011 - 06:16 PM

I think I get what you are saying.:)
I'll try to apply what you guys have told me. Hopefully I can get this working...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1