4 sided Collision?

trying to get working

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2769 Views - Last Post: 11 January 2009 - 04:14 PM Rate Topic: -----

#1 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

4 sided Collision?

Posted 04 January 2009 - 01:16 PM

Hi guys im back again....
Another tough question.

I am using IntersecRECT colishions and I am having trouble understanding how to make stuff collide properly like for example mario. Mario jumps on a black and stops mario jumps under a block and he stops (or goes though it). mario jumps and hits the side of the block he falls to the ground.

I have been trying to do with with only 2 boxes.

do i have to make 4 boxes 1 for the top bottom left and right of the box because if i do then i can make the colishions fine i think. but i dont know how I would put it into my program.
Eneyways if you know how to make mario do everything properly with 2 boxes plz post somthing ENEYTHING!!

sorry im desprate

if you want my code i can post it here but its just a basic call of intersect rect with my 2 boxes and then an ifstatment to make marios velocY = 0 not mutch to it.

Thanks ahead of time. :D

Is This A Good Question/Topic? 0
  • +

Replies To: 4 sided Collision?

#2 Tom9729   User is offline

  • Segmentation fault
  • member icon

Reputation: 181
  • View blog
  • Posts: 2,642
  • Joined: 30-December 07

Re: 4 sided Collision?

Posted 04 January 2009 - 04:08 PM

You should check to see which side of the block Mario is on.

If he is on the left or the right then you should set his vx to zero (or more realistically, reverse it and multiply it by same factor to simulate an elastic collision).

If he is on the top or bottom then you should set his vy to zero (or again, reverse and multiply it) and leave his vx alone.

I'm assuming you have some kind of gravity force acting on Mario?

This post has been edited by Tom9729: 04 January 2009 - 04:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: 4 sided Collision?

Posted 05 January 2009 - 08:15 AM

tnx for replieing

yes i have a force pushing down on mario

the problem is i dont know if mario hits the top, botom, left or right of the box all i know is that mario hit it.

iv been trying to use some variables to figure out what way mario is moveing

like when you push a button it will make a boolean true and if i hit the box while the left boolean is true then i know i hit the right side of the box and then know what to do for the colishion.

iv been thinking about it and i might be able to make it so it takes the last position of mario befor moveing him and if he hits a block then he will go back to the last position.

this poses alot of problems because im using the same colishion box for the ground as im using for the blocks and pipes.

And if i use the last position idea the gravity will keep increaseing untill mario will just fly off the screen.

what i dont understand about your post is what do you mean by reversing the movement and multyplyeing it im cofused with that.

tnx for replieing

yes i have a force pushing down on mario

the problem is i dont know if mario hits the top, botom, left or right of the box all i know is that mario hit it.

iv been trying to use some variables to figure out what way mario is moveing

like when you push a button it will make a boolean true and if i hit the box while the left boolean is true then i know i hit the right side of the box and then know what to do for the colishion.

iv been thinking about it and i might be able to make it so it takes the last position of mario befor moveing him and if he hits a block then he will go back to the last position.

this poses alot of problems because im using the same colishion box for the ground as im using for the blocks and pipes.

And if i use the last position idea the gravity will keep increaseing untill mario will just fly off the screen.

what i dont understand about your post is what do you mean by reversing the movement and multyplyeing it im cofused with that.
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4202
  • View blog
  • Posts: 18,444
  • Joined: 24-September 08

Re: 4 sided Collision?

Posted 05 January 2009 - 01:11 PM

I don't know what language you're writing it in, but you could say...

say your box on the screen is at position 500,300, you could draw a box, or the object, and have the graphics library recognise the deminsions and set it as "bad area", and have if statements as to what he does in comparison to his orientation around the box, or draw a rectangle from x,y to x1,y1.
Was This Post Helpful? 0
  • +
  • -

#5 Tom9729   User is offline

  • Segmentation fault
  • member icon

Reputation: 181
  • View blog
  • Posts: 2,642
  • Joined: 30-December 07

Re: 4 sided Collision?

Posted 05 January 2009 - 01:44 PM

I've been giving this some thought and I think using bounding circles would be easier.

A bounding circle works like this...

Each object has a bounding circle around it. The circle should have:
1) A position.
2) A radius.

To test if two objects are "colliding" you find the distance between them, and then you see if the sum of their two circles' radii is greater than or equal to the distance.

When Mario and the box collide, you could find Mario's position relative to the box as an angle (counterclockwise from the origin, see picture).

Attached Image

Then you could do something like..
theta = Mario's position relative to the box (see picture)

if (theta <= 45 or theta > 315)
{
	Mario is on the right of the box
}

else if (theta > 45 and theta <= 135)
{
	Mario is on top of the box
}

else if (theta > 135 and theta <= 225)
{
	Mario is on the left side of the box
}

else
{
	Mario is under the box
}



There are probably more efficient ways to do this...
-------------

When you drop a ball on the ground, it bounces but usually not back up to the height at which you dropped it. The collision between the ground and the ball is mostly elastic, but a little bit of energy is lost (so the ball doesn't bounce as high).

In hindsight, Mario doesn't really bounce in the classic game, so forget what I said about reversing the direction.
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4202
  • View blog
  • Posts: 18,444
  • Joined: 24-September 08

Re: 4 sided Collision?

Posted 05 January 2009 - 02:27 PM

Bounding circles for blocks are unclean and poopy. Bounding rectangles, like I said!
Was This Post Helpful? 0
  • +
  • -

#7 mastersmith98   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 22-December 08

Re: 4 sided Collision?

Posted 05 January 2009 - 04:08 PM

I had a similar problem (see my thread on Direction of Collision; ignore my solution though, I basically just gave up and made some concessions).

the problem with finding the angle of mario to the bounding box is that it's going to be different depending on the dimensions of the bounding box. you could try and find the angles of lines drawn from the origin of the box to the various endpoints, but in my mind that's a bit weak.

There has to be an easy method to see if the player is on top, bottom, right, or left of the bounding box. I've thought about line intersections, but that too is a less than perfect method.

I look forward to seeing what comes out of this thread.
Was This Post Helpful? 0
  • +
  • -

#8 Tom9729   User is offline

  • Segmentation fault
  • member icon

Reputation: 181
  • View blog
  • Posts: 2,642
  • Joined: 30-December 07

Re: 4 sided Collision?

Posted 05 January 2009 - 04:51 PM

View Postmastersmith98, on 5 Jan, 2009 - 06:08 PM, said:

the problem with finding the angle of mario to the bounding box is that it's going to be different depending on the dimensions of the bounding box. you could try and find the angles of lines drawn from the origin of the box to the various endpoints, but in my mind that's a bit weak.

Why? You would only have to calculate the angles (from the center to the corners) once as long as the box didn't change it's dimensions.

View Postmastersmith98, on 5 Jan, 2009 - 06:08 PM, said:

There has to be an easy method to see if the player is on top, bottom, right, or left of the bounding box. I've thought about line intersections, but that too is a less than perfect method.

You could just test the player's position versus the position of each corner of the box, but I think that would be more work than using bounding circles.
Was This Post Helpful? 0
  • +
  • -

#9 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: 4 sided Collision?

Posted 05 January 2009 - 05:55 PM

thank you for all your great ideas .
i am doing this in vb6 NeoTifa and i load a map from a file in the file i have a Boolean that makes it true when its a box Mario should not go in. so i guess that's a bad box and the boxes size will never change its always going be 16 by 16.

I am going to ask my Teacher tomorow about this if i dont get it tonight.
I will try your ideas if i fail.

Thanx ill keep posting what happens :D
Was This Post Helpful? 0
  • +
  • -

#10 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: 4 sided Collision?

Posted 05 January 2009 - 08:50 PM

Aright so i messed around and i got 2 some what working collision handling
first one disables gravity unless you have hit the up key and throws you underneath or over the box depending if you move left or right.

and the other one puts you back to your previous spot witch would be befor you hit the block and sets your y Velocity to 0

this makes you unable to jump over something if you have hit it that's all i got today

I'm going to talk to my teacher tomorrow and work more on the second method.
Was This Post Helpful? 0
  • +
  • -

#11 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: 4 sided Collision?

Posted 06 January 2009 - 10:57 AM

yay i showed my teacher the second colishion thing i made yesterday and he told me that if the spriteX is greater then the last position then it means im moveing right and so on for left up and down so now i know what side i hit of the box now i have to somehow make it so that mario can still jump when he hits a block becuase if i am running right and hit say a pipe it wount let me jump eny more because i have velocY set to 0 when i hit a block.
Was This Post Helpful? 0
  • +
  • -

#12 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4202
  • View blog
  • Posts: 18,444
  • Joined: 24-September 08

Re: 4 sided Collision?

Posted 06 January 2009 - 02:57 PM

I did a game in vb6 and I used the "Bad tile" method. :) Glad things are coming together for ya
Was This Post Helpful? 0
  • +
  • -

#13 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: 4 sided Collision?

Posted 06 January 2009 - 05:07 PM

well its ether me or a brain fart but i give up i tried doing the circle thing and i don't understand the position thing. but the bad tile method is something i haven't see do you have links in your game did you have gravidy forcing your character down ? this would help me a lot tnx
i'm going to search for this bad tile method.
Was This Post Helpful? 0
  • +
  • -

#14 skyhawk133   User is offline

  • Head DIC Head
  • member icon

Reputation: 1960
  • View blog
  • Posts: 20,404
  • Joined: 17-March 01

Re: 4 sided Collision?

Posted 06 January 2009 - 05:14 PM

I hate to be a stickler for spelling. But I'm going to rename the title of this thread to "4 Sided Collision"
Was This Post Helpful? 0
  • +
  • -

#15 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4202
  • View blog
  • Posts: 18,444
  • Joined: 24-September 08

Re: 4 sided Collision?

Posted 06 January 2009 - 06:45 PM

Thanks skyhawk. No I don't have a link. I did it in BASIC on my moms computer at her house. If you're doing a sidescroller, I would suggest having the ground horizontal y position set as a contant. For instance:


if (marioYPos > GROUND)
(marioYPos - 2); //or whatever speed you have your game set

and set that in a loop or something. I don't know. It's been a while. But say

if (marioXPos >= block1Pos || marioXPos <= block1Pos && marioYPos == topOfBox)
//hes standing on the box

this is all assuming that the variable marioXPos and marioYPos are the center of the bottom row of pixels for the sprite. I don't know. It's been a while
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2