Making a player jump using physics in Visual Basic? - physics

I have been trying for some time to implement jumping into the start of my platformer game, but without success.
I have managed to implement gravity by continuously trying to move the player downwards, and back up if he falls below the bottom of the screen.
Case GAME_STATUS.VOID
Keyboard_Controls()
plyer.movePlayer(0, g)
If plyer.Bottom > Height Then
plyer.playerStanding(True)
plyer.movePlayer(0, -g)
Else
plyer.playerStanding(False)
End If
What i am struggling with is implementing a "jump" based on real-life physics.
I am an AS level student studying physics, so i am aware of SUVAT and all the equations, and f=ma, but i cannot get my player to jump when the up key is pressed. Any ideas?
Key Event:
Case Keys.W
If Me.Controls.Contains(gamePanel) And gamePanel.Controls.Contains(plyer) Then
u = True
End If
My failed attempts at physics:
If u = True Then
Dim t, v As Double
t = Get_Elapsed_Time() / 1000
v = 0.5 + (g * t)
plyer.movePlayer(0, v)
'plyer.movePlayer(0, -(g*3))
'If plyer.Top > 25 Then
' plyer.playerStanding(False)
' plyer.movePlayer(0, g)
'End If
End If

Finally cracked it!
Added xVel, yVel into player class and re-implemeted gravity by adjusting the yVel of the player in a subroutine that is called every gameLoop. By doing this, all i had to do was change the yVel to make the player jump. Code below:
In mainLoop (in replace of my previous gravity code):
plyer.nextStep()
If plyer.Bottom >= Height Then
plyer.playerStanding(True)
plyer.Location = New Point(plyer.Location.X, (Height - 10))
Else
plyer.playerStanding(False)
End If
In player class:
Public Sub nextStep()
If standing = False Then
yVel += g
Else
yVel = 0
End If
Top += yVel
End Sub
Sub tryToJump()
If standing Then
standing = False
Dim v, s As Double
v = 0
s = 75
yVel = -(Math.Sqrt((v * v) - (2 * -g * s)))
End If
End Sub
Now, when jump key is pressed, it calls tryToJump(), and jumps if the player is standing!

Related

Trying to infinitely reference square picturebox collisions

I'm trying to get this to detect collisions from my thousands of pictureboxes at once. I cant seem to make it work and I just get a 150 timer tick delay and then infinite collisions
BikeB moves every timer tick also xb, yb is the location on Bikeb. I'm basically creating a line.
Dim lineb As New PictureBox
lineb.Size = New Size(24, 24)
lineb.BackColor = Color.FromArgb(192, 0, 0)
lineb.Location = New Drawing.Point(xb, yb)
lineb.Tag = creatorb
Controls.Add(liner)
liner.Name = "liner" + CStr(creatorb)
creatorb -= 1
For Each Ctrl As Control In Me.Controls
If TypeOf Ctrl Is PictureBox Then
If Ctrl.Name <> "BikeB" Then
If Ctrl.Tag < creatorb - 150 Then
If BikeB.Bounds.IntersectsWith(Ctrl.Bounds) Then
MsgBox("Blue Loses")
BikeMove.Enabled = False
End If
End If
End If
End If
Next

mouse controlled by an IMU

I am using pitch and yaw angle from an IMU to move a mouse pointer for PC in vb.net.
Unfortunately it doesn't work steady.
First I set the max angles which should be performed
LOAD-Event
'set max angles in degree
maxAngle_YawRight = 30
maxAngle_YawLeft = -30
maxAngle_PitchUp = 20
maxAngle_PitchDown = -20
calculateCurParameter(maxAngle_YawRight, Panel_Right.Location.X, maxAngle_YawLeft, Panel_Left.Location.X + Panel_Left.Width, maxAngle_PitchUp, Panel_Top.Location.Y+Panel_Top.Height, maxAngle_NickDown, Panel_Bottom.Location.Y)
I use four these max angles (pitch_up, pitch_down, yaw_right, yaw_left) to assign them to a part of my form, to get 4 points, e.g. (pitch_up,panel_2.location.Y)
pitch_up and pitch_down are used for the y coordinate of the mouse pointer and yaw_right and yaw_left are used for the x coordinate. This is done by function calculateCurParameter.
Then I use this four points to calculate parameters for an linear equation:
CursorPos=m*angle+b
So you get for every new angle a new Cursor Position.
Private mX As Double
Private bX As Double
Private mY As Double
Private bY As Double
...
Private Sub calculateCurParameter(ByVal maxYawRight As Double, ByVal rightBorder As Double, ByVal maxYawLeft As Double, ByVal leftBorder As Double, ByVal maxPitchUp As Double, ByVal topBorder As Double, ByVal maxPitchDown As Double, ByVal bottomBorder As Double)
'this function calculates the parameter for Ycur=m*Xangle+b for each Pitch and Yaw
'calculate the parameter for the CursorPosition X
dim deltaX= maxYawLeft-maxYawRight
dim deltaY=leftBorder-rightBorder
mX=deltaY/deltaX
bX=rightBorder-mX*maxYawRight
'calculate the parameter for the CursorPosition Y
deltaX=bottomBorder-topBorder
deltaY=maxPitchDown-maxPitchUp
mY=deltaY/deltaX
bY=topBorder-mY*maxPitchUp
end Sub
After that calculation, I use a quadratic weighted moving average (QWMA) as a low pass filter cursor outputs calculated by the function y=m*x+b.
For that moving average I use the last 50 samples of each angle.
After that I put the two angles in each linear equation(cursorPos=m*angle+b) as described.
'Timer-Event which is triggered each 8ms
' yaw and pitch are in degrees and are updated in this timer event before
Dim xCoordCur = Math.Round(mX * yaw + bX)
Dim yCoordCur = Math.Round(mY * pitch + bY)
arrayCurY(cursorCounter) = yCoordCur
arrayCurX(cursorCounter) = xCoordCur
If cursorCounter =50 Then 'need 50 samples to do the QWMA
Dim aqwmY = qwma_calculating(arrayCurY)'function to calculate the QWMA, seems to be working
Dim aqwmX = qwma_calculating(arrayCurX)
'the mouse pointer should not leave the form
If aqwmX > Panel_Right.Location.X Then
aqwmX = Panel_Right.Location.X
ElseIf aqwmX < (Panel_Left.Location.X + Panel_Left.Width) Then
aqwmX = Panel_Left.Location.X + Panel_Left.Width
End If
If aqwmY > Panel_Bottom.Location.Y Then
aqwmY = Panel_Bottom.Location.Y
ElseIf aqwmY < (Panel_Top.Location.Y + Panel_Top.Height) Then
aqwmY = Panel_Top.Location.Y + Panel_Top.Height
End If
'Set the new Cursor Position
Cursor.Position = New Point(aqwmX, aqwmY)
arrayValuesMoving(arrayCurX)'function to move the values one index forward
arrayValuesMoving(arrayCurY)
Else
cursorCounter += 1
End If
In the my last step I set the new Cursor-Position
Cursor.Position=New Point(xCoor,yCoor)
Now I can control the mouse pointer by moving the IMU, but it is very unstable.
For example the mouse pointer still moves although the IMU doesn't move.
It's even impossible to hover over some form elements.
What did I do wrong?
Thanks in advance!
I added the follwoing code:
If xCoor > Cursor.Position.X + 3 Or xCoor < Cursor.Position.X - 3 Then
istxCoor = True
Else
istxCoor = False
End If
If yCoor > Cursor.Position.Y + 3 Or yCoor < Cursor.Position.Y - 3 Then
istyCoor = True
Else
istyCoor = False
End If
If istxCoor And istyCoor Then
SetCursorPos(xCoor, yCoor)
ElseIf istxCoor Then
SetCursorPos(xCoor, Cursor.Position.Y)
ElseIf istyCoor Then
SetCursorPos(Cursor.Position.X, yCoor)
End If
it is now more steady but it has now a time lag.

Why do the loops in this code not work?

I don't understand why it doesn't work, I've tried a For loop, do until loop and do while loop but they just don't want to work. Without the loops, it works perfectly fine, but I want to use loops to reduce the lines of code that are being used. The "Hub" calls this class using MoveHub.MoveUp(10) and the same goes for MoveDown but it's like it does nothing at all. Any suggestions?
I've tested everything out in the Hub form, the code there is not the problem. It's the MoveHub class that causes issues.
Here's the code:
Public Class MoveHub
Public pos As Integer
Public toggle As Boolean
Public Sub MoveUp(speed As Integer)
If (pos > 0) Then
' For Me.pos = 9 To 0
Do Until pos = 0
pos -= 1
Hub.Location = (New Point(Hub.Location.X, Hub.Location.Y - speed))
If pos = 0 Then
Hub.DragPic.Image = My.Resources.DropPicture4
toggle = True
End If
Loop
' Next
End If
End Sub
Public Sub MoveDown(speed As Integer)
If (pos < 10) Then
'For Me.pos = 0 To 9
Do Until pos = 9
pos += 1
Hub.Location = (New Point(Hub.Location.X, Hub.Location.Y + speed))
If pos = 9 Then
Hub.DragPic.Image = My.Resources.DropPicture4_Up
toggle = False
End If
Loop
'Next
End If
End Sub
End Class
I think you are calling MoveUp(10) and asusme that the loop will run 10 times.
This assumption is incorrect. Because Parameter that receive passed value 10 is speed. While the Loop is being controlled by variable pos.
If my assumption is not correct then you need to provide the code calling these methods and where the value of pos is being set.

How do I make my picturebox detect collision and bounce off?

this is my collision method but its just bouncing in the opposite direction.
'Collision Method 1
Dim Col As Boolean = Collision(pxbox, pbpaddle1)
If Col = True Then
moveRight = Not moveRight
moveDown = Not moveDown
End If
Dim Col2 As Boolean = Collision(pxbox, pbpaddle2)
If Col2 = True Then
moveRight = Not moveRight
moveDown = Not moveDown
End If
This is my function for the collision
Private Function Collision(ByVal P1 As PictureBox, ByVal P2 As PictureBox) As Boolean
If P1.Left + P1.Width < P2.Left Then Return False
If P2.Left + P2.Width < P1.Left Then Return False
If P1.Top + P1.Height < P2.Top Then Return False
If P2.Top + P2.Height < P1.Top Then Return False
Return True
End Function
There are a few problems.
It's not clear whether P1, P2, or both are moving. Is a picturebox moving inside a containing picturebox, or are two pictureboxes moving on the screen?
In the function Collision, you should test all four possibilities and return false when none of the four is satisfied. Otherwise, there is a collision.
In your collision methods, for a collision with a vertical or horizontal "wall", you should change only Moveright or Movedown, but not both (unless the collision is in a corner).
If the collision is between two balls, you should use the average angle (weighted with speed and mass for each ball), and use the perpendicular of that angle as the "wall" the balls bounce off of. (This assumes the balls have no diameter.)

vb.net LED BOARD DISPLAY user control

I am developing LEDBOARD user control in vb.net.I have done it also .Actually its taking too much time to load .In the vb6 same application I am loading 3000 labels using a label control array but not time consuming .In vb.net I am doing same but it's taking too much time to load 3000 labels.Is there any other way(any control or any custom control) to draw input text(any font style),image like below image
It looks like below
Create your LedBoard control from scratch by inheriting from Control, instead of using a UserControl and adding tons of labels.
I just made a little test to show you what I mean. You will have to adapt the logic to meet your needs.
Public Class LedBoard
Inherits Control
Private _rand As Random = New Random()
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.FillRectangle(Brushes.Black, 0, 0, Width, Height)
Const nx As Integer = 40, ny As Integer = 25
Dim w = CInt((Width - 1) / nx) - 1
Dim h = CInt((Height - 1) / ny) - 1
For x As Integer = 0 To nx - 1
For y As Integer = 0 To ny - 1
If _rand.NextDouble() < 0.8 Then
e.Graphics.FillRectangle(Brushes.Red, x * (w + 1) + 1, y * (h + 1) + 1, w, h)
End If
Next
Next
End Sub
End Class