Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hi, I am currently using Visual Basic 2008.

I started my game and added my sprites and got my obstacles set up.

 

I do not know how to detect Collision.

 

How do I make it where if my character touches the building. He Stops Moving.

 

And

 

I am trying to find out which side of the building my character is touching.

 

For Example.

 

If my character touches the top side of the building then Collision = 1

If my character touches the left side of the building then Collision = 2

If my character touches the right side of the building then Collision = 3

If my character touches the bottom side of the building then Collision = 4

 

I just can't figure out how to find the side of the building I am touching.

 

I am using Pictureboxes for Everything.

 

I tried looking it up first... There is no sense in making a post if you can't find something yourself.

 

I have spent about 9 hours straight looking this up and I CANNOT for the life of me find something that works the way I need it to.

--------------------------------------------------------------------------

This piece of code is composed of :

 

1. Key Down Event.

 

2. Key Up Event.

 

3. A Timer to move the character and run the Sub that Animates movements.

 

4. A Sub to Control and Animate the movements.

 

5. The Animations to run.

 

--------------------------------------------------------------------------

 

*** = Something I am trying to add.

 

 

------------------------Dimming my functions--------------------------

Animation tells it what frame to use.

EXAMPLE : When Walking the Animation number is reset to 0 then increases by 1 with the timer. and as it increases. it animates!

Direction tells what way you face after key up **** or if you hit a collision

Example : When hitting the Up Arrow, You walk Up. and Direction = 1, When releasing the Up Arrow, Direction = 1 so you face Up.

Example2 : When hitting the Left Arrow, You walk Left. and Direction = 2, When releasing the Left Arrow, Direction = 2 so you face Left.

***Collision : When you hit an object, The number tells the key you are holding down to stop the timer and quit animating, it takes the direction and makes you face the way you where walking, The collision integer tells a number when hitting an object. and that number tells which key to stop.

Example : You walk into a wall, You quit animating that you are walking and start standing, You stand the direction you last walked.

 

'Collision = I am working on this to try to keep track of Your direction and what you hit. If it = 1 then when you hit something. It will stop you. and make you face forward according to the direction

 

Any help would be appreciated, I am nervous (This is my first post.) so I am trying to explain it as simple as possible to the person who is reading.

 

any help is APPRECIATED!!!! :D Thx!

 

Here is what I have for coding : (MAY NOT BE THE BEST, BUT 99% of it WORKS.)

 

------------------------Dimming------------------------

 

Dim Animation As Integer

Dim Direction As Integer

Dim Collision As Integer

 

 

'1. =============== Controls (Form Keydown Event)=============

Private Sub Walking_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

 

If e.KeyCode = Keys.Up Then

Direction = 1

If Collision = 1 Then

Player.Image = Player_Stand_Up

WalkMovement.Stop()

Else

WalkMovement.Start()

End If

End If

 

If e.KeyCode = Keys.Left Then

Direction = 2

If Collision = 2 Then

Player.Image = Player_Stand_Left

WalkMovement.Stop()

Else

WalkMovement.Start()

End If

End If

 

If e.KeyCode = Keys.Right Then

Direction = 3

If Collision = 3 Then

Player.Image = Player_Stand_Right

WalkMovement.Stop()

Else

WalkMovement.Start()

End If

End If

 

If e.KeyCode = Keys.Down Then

Direction = 4

If Collision = 4 Then

Player.Image = Player_Stand_Down

WalkMovement.Stop()

Else

WalkMovement.Start()

End If

End If

 

'2. ===============Controls (Form KeyUp Event) =========

 

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

 

If Direction = 1 Then

Player.Image = Player_Stand_Up

End If

If Direction = 2 Then

Player.Image = Player_Stand_Left

End If

If Direction = 3 Then

Player.Image = Player_Stand_Right

End If

If Direction = 4 Then

Player.Image = Player_Stand_Down

End If

 

'3. ===========Walk Movement (Timer)====================

 

Private Sub WalkMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WalkMovement.Tick

 

WalkAnimationController()

 

If Direction = 1 Then

Player.Location = New Point(Player.Location.X, Player.Location.Y - 2)

WalkUp()

End If

If Direction = 2 Then

Player.Location = New Point(Player.Location.X - 2, Player.Location.Y)

WalkLeft()

End If

If Direction = 3 Then

Player.Location = New Point(Player.Location.X + 2, Player.Location.Y)

WalkRight()

End If

If Direction = 4 Then

Player.Location = New Point(Player.Location.X, Player.Location.Y + 2)

WalkDown()

End If

End Sub

 

'4. =========== Sub That Animates Sprite (Sub)====================

 

Sub WalkAnimationController()

Animation = Animation + 1

If Animation > 8 Then

Animation = 0

End If

End Sub

 

'5. =========== Lots of Animations (Large Amount of Subs)====================

 

Sub WalkUp()

Direction = 1

If Animation = 2 Then

Player.Image = Player_Walk_Up1

End If

If Animation = 4 Then

Player.Image = Player_Stand_Up

End If

If Animation = 6 Then

Player.Image = Player_Walk_Up2

End If

If Animation = 8 Then

Player.Image = Player_Stand_Up

End If

End Sub

 

Sub WalkLeft()

Direction = 2

If Animation = 2 Then

Player.Image = Player_Walk_Left1

End If

If Animation = 4 Then

Player.Image = Player_Stand_Left

End If

If Animation = 6 Then

Player.Image = Player_Walk_Left2

End If

If Animation = 8 Then

Player.Image = Player_Stand_Left

End If

End Sub

 

Sub WalkRight()

Direction = 3

If Animation = 2 Then

Player.Image = Player_Walk_Right1

End If

If Animation = 4 Then

Player.Image = Player_Stand_Right

End If

If Animation = 6 Then

Player.Image = Player_Walk_Right2

End If

If Animation = 8 Then

Player.Image = Player_Stand_Right

End If

End Sub

 

Sub WalkDown()

Direction = 4

If Animation = 2 Then

Player.Image = Player_Walk_Down1

End If

If Animation = 4 Then

Player.Image = Player_Stand_Down

End If

If Animation = 6 Then

Player.Image = Player_Walk_Down2

End If

If Animation = 8 Then

Player.Image = Player_Stand_Down

End If

End Sub

 

I have attached my project.

 

If you have any info you want to get into detail about

You can IM me at : Fabianruss@hotmail.com

Or, Email me at : Fabian.Russ@comcast.net

Edited by Fabian_Russ
Posted

I think I may have got something... TOOK FOREVER.

Sub CheckForCollision()

If Player.Bounds.IntersectsWith(Bike.Bounds) = True Then

Label1.Text = Collision

If Player.Bounds.Top = Bike.Bounds.Bottom - 1 Then

Collision = 1

Label5.Text = "Cannot Move Up"

End If

 

If Player.Bounds.Left = Bike.Bounds.Right - 2 Then

Collision = 2

Label5.Text = "Cannot Move Left"

End If

 

If Player.Bounds.Right = Bike.Bounds.Left + 1 Then

Collision = 3

Label5.Text = "Cannot Move Right"

End If

 

If Player.Bounds.Bottom = Bike.Bounds.Top + 1 Then

Collision = 4

Label5.Text = "Cannot Move Down"

End If

End If

If Player.Bounds.IntersectsWith(Bike.Bounds) = False Then

Collision = 0

End If

End Sub

  • 1 month later...
  • Leaders
Posted

I agree. Checking a specific location of the bike to determine what side its on probably won't work unless you move it in set increments.

Using the .IntersectsWith() is probably the best to determine that there is a collision. To determine which direction is blocked, you can just check what direction the user was moving.

If the user can only move in one direction at a time, then just check which direction is true... it doesn't look like you are doing it that way.

 

You might consider doing the left/right movements, then checking for collision.

Then, doing the up/down movements and then checking for collision.

Your program should not have to move the object left and then right in the same cycle. (Recommend using an If Direction = 2 Then... ElseIf Direction = 3 Then ... End If)

So, now, if you do this, perform the collision check (intersectswith())

if it's true, Direction = 2 means the collision occurs while moving left (so the object was hit on the right side). Direction = 3 means the object was hit on the left side.

Now, do the vertical movements and use intersectswith() again. Direction = 1 means the object was hit on the bottom because the bike was moving towards the top. Etc. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • 5 months later...
Posted

I got it to detect collision, but I can't get it to stop correctly...

 

I have 2 blocks... When one collides with the other, it sometimes gets into the other one.. And it is noticeable, Not only that but I can't walk out of the collision because I don't know how to make the block stop moving on one key and start moving on another... The player just collides with an object and gets stuck in the object.. Any ways of solving this?

Posted
Once you detect a collision you must then set the position of the moving object exactly next to the stationary object.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...