Physics help!

BlitzMax Forums/BlitzMax Programming/Physics help!

Ked(Posted 2009) [#1]
NOTE: It's kind of late, and I'm having trouble concentrating so stay with me. Sorry if some of this doesn't make sense.

How can I make particles bounce of walls in my 2D maps? Like a simple explosion. All, say, 20 particles start off at x and y. All particles are given an angle of Rnd(0,360). All particles are given an xspd and yspd of Rnd(1,5) and Rnd(1,5). Move particles with
x:+Sin(angle)*xspd
y:+Cos(angle)*yspd

When a collision is made with the walls of the map, how can I make them "bounce" away?

Sorry, again, for what looks like a bunch of fragment sentences above.


DjBigWorm(Posted 2009) [#2]
HI,
Well I am not a pro at programming but the answer you seek is simple once broke down to theory:) I check for collisions one plane at a time.
Say check for collision at the particles x location + xspd (problem with this if the particle is traveling faster than your blocks are wide it will go right though them).
If this comes back as a collision then make the xspd a negative value.
Same would be true if the xspd was negative, check collision then make a xspd positive value and would give the illusion of bouncing off walls n such.
When I program tile based games, I have a layer of solid colors)
I check pixels for color value then decide what action to take from there. In a mario brother type game you just check to see if the particle is in the sector of a block.
You get the (x+xoffset_of_map)/width_of_block and this returns how many blocks over your x is. same is true for the y location of the map.

I hope this kind of helps you out. If not I can try to be more clear. Happy Programming!! (don't hold me on the math above:)


Nate the Great(Posted 2009) [#3]
what you need to do for the sake of making your physics 100 times easier is do the sin,cos stuff once and store that in a dx and dy value in the type. then you just do

x:+dx
y:+dy


this also allows you to do much simpler collision detection and response as well as gravity if needed

collision detection for left side of screen:

If x <= 0 Then
	x = 0
	dx = -dx
EndIf


see much easier :)


Ked(Posted 2009) [#4]
collision detection for left side of screen:

I have no way of telling which side of the wall (top, left, right, bottom) its hitting.

@DjBigWorm: Sadly, I did not understand any of that. :)

Some other stuff you may need to know about the maps:
1. The map has layers. (TILEMAP_WALL, TILEMAP_FLOOR, etc)
2. Each layer is one TImage. (all of the tiles are drawn on to the map layer[curlayer])


Nate the Great(Posted 2009) [#5]
I have no way of telling which side of the wall (top, left, right, bottom) its hitting.


haha thats why you check for all of them ;) who would have thought


Ked(Posted 2009) [#6]
haha thats why you check for all of them ;) who would have thought

Alright, Nate the Great. I have no clue how to check which side (top, left, right, or bottom) of a wall thats being hit on one single TImage similar to this:

(white is the walls, black is the mask)


DjBigWorm(Posted 2009) [#7]
ked,

hmm, didn't understand. I have blitzplus/blitzbasic code i can email you,
but has comments and what not maybe it will help you to understand what you are asking.
If that doesn't i can explain in more detail later on.
my profile has my email. let me know that way if you are interested!

Later and happy coding.


Nate the Great(Posted 2009) [#8]
oh ok I was thinking you had a tilemap system or similar... in this case it may be best if you just make some sort of type for walls that would have the x,y coords of each end of the wall and simply loop through all of those and do basic collision checking for each one.

(hope that makes more sense)


Ked(Posted 2009) [#9]
My tilemap engine is a little different than others, because I have all of the tiles of each layer being drawn to one image of each layer. Drawing each tile separately will definitely slow the whole program down.

Anyway, I have decided not to do this because it's way too much trouble. I've been working at it all day today and yesterday, and nothing's coming up.

Thanks anyway to anyone that posted to help.


_Skully(Posted 2009) [#10]
How are you managing to perform collisions then? I'm also making a tilemap engine and i have a collision, background and foreground layer that the player can walk behind.


Ked(Posted 2009) [#11]
All of my tiles are on one TImage, so I'm just using ImagesCollide2() for standard collisions. I just didn't know how to make the objects bounce back after they made a collision with the wall layer.


_Skully(Posted 2009) [#12]
If you pass the movement delta of the object to the collision routine you can scroll it back along the same path until it no longer collides. You can pass a vector type back with how far back you had to move it to eliminate the collision, or use the Var (byref) parameter to pass it back through the same variables.

Cheers