Question moving ball?

groover

Member
Joined
Oct 10, 2009
Messages
23
Programming Experience
1-3
Hi all,

I want to move a ball(ovalshape) at various angles.

I made a simple program to test the movement and angle, but I can not get it right.
If the number of possible angles(N) is low then the ball is moving fine.
if the number of possible angles(N) high then the ball is moving too coarse.
I know it is because of the way i calculate, but I dont know enough about maths. I dont want to use vectors, trigonometry is ok.

On a windowsform I have a trackbar (0 - 360), a lineshape (barrel rotating 360 degrees shooting the ball)

I use the folowing code for the barrel:
VB.NET:
 Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        trackbarangle = TrackBar1.Value
        TextBox1.Text = trackbarangle.ToString
        barrel.X1 = barrel.X2 - Math.Cos(trackbarangle * (pi / 180)) * 100
        barrel.Y1 = barrel.Y2 - Math.Sin(trackbarangle * (pi / 180)) * 100
    End Sub

This is the code for the movement (in a timer):
VB.NET:
 Public Sub initiate()
      Dim n as integer  
        If start = False Then ball.Location = New Point(barrel.X1 - ball.Width / 2, barrel.Y1 - ball.Width / 2)

        ball.Location = New Point(ball.Location.X - (n * (Math.Cos(trackbarangle * (pi / 180)))), _
                                      (ball.Location.Y - (n * (Math.Sin(trackbarangle * (pi / 180))))))                    
    
        start = True
    End Sub

I know the x an y values of a point should be integers, but that is part of the broblem.

I hope someone can help

thanks,
Groover
 
Solved the problem, created a new

Hi all,

I solved my problem by using a moving image instead of an ovalshape.
Now i can move the ball at all possible angles with smooth motion.

The new problem is that other shapes that i put on the form dissapear when the ball is moving.
They reappear when they cange size or position e.g. moving the slider on the trackbar changes the angle of the barrel.

my new code:
VB.NET:
 Private Sub Form1_Paint(ByVal sender As Object, _
                            ByVal e As PaintEventArgs) Handles Me.Paint
                img = Image.FromFile("C:\Users\Documents\Visual Studio 2010\Projects\collision detection\collision detection\Resources\ball1.png")
        e.Graphics.DrawImage(img, ulcorner)


    End Sub

    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
       
        xcos = (Math.Cos(trackbarangle * (pi / 180)))
        ysin = (Math.Sin(trackbarangle * (pi / 180)))
        TextBox2.Text = xcos.ToString
        TextBox3.Text = ysin.ToString
        trackbarangle = TrackBar1.Value
        TextBox1.Text = trackbarangle.ToString
        barrel.X1 = barrel.X2 - Math.Cos(trackbarangle * (pi / 180)) * 100
        barrel.Y1 = barrel.Y2 - Math.Sin(trackbarangle * (pi / 180)) * 100
    End Sub

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If start = False Then

            ulcorner = New PointF(barrel.X1, barrel.Y1)

            x = ulcorner.X
            y = ulcorner.Y

        End If
        a = xcos
        b = ysin
        ulcorner = New PointF(x - a, y - b)
        x = x - a
        y = y - b
        Debug.Print(x)

        ulcorner = New PointF(x, y)
        
        Me.Invalidate()
        
        Me.Update()

        start = True
    End Sub

Controls as textboxes and trackbar stay visible.

Also there is flickering of the image when it moves.

Hope somone can help,

thanks,

Groover
 

Latest posts

Back
Top