![]() |
Some angle help
I'm working on some fairly basic object physics on Maloria, where hitting an object will make it move. I'm at the part where I need it to kind of bounce off a wall when it collides with it a bit, but I'm having trouble figuring out how I would do that. Here's what I have:
PHP Code:
|
I'll assume that anything this object is going to hit will be a horizontal or vertical line.
If it hits a horizontal line, the angle becomes 180-angle. If it hits a vertical line, the angle becomes -angle. |
It's always become a 180 angle when it hits a horizontal line...? o_O
That doesn't seem right... But also, part of the problem is figuring out if it's hitting a horizontal line or a vertical line...doMove() returns true or false, telling you whether it can move there or no (if it will run into a wall), but the problem is figuring out if it's hitting a horizontal wall or a vertical wall, since the object doesn't just move straight left, right, up, or down, it moves at angles. |
180 minus the angle.
Those work for all angles. |
Apparently I'm doing something wrong, since doMove() isn't returning false unless it's stuck in a corner, most of the time. And when it returns false properly, moving it at the angle 180 - the original angle doesn't work.
|
I hope you're not using 180 - this.angle. It should certainly be pi - this.angle. I was hoping to simplify it a bit in my first post, sorry.
|
I actually used degtorad(180) - this.angle
|
Strange. You should perhaps split up the movement into vertical and horizontal components and check them individually to see if it's a horizontal or vertical wall.
|
Instead of using angles, use a separate velocity for x and y. Then if it hits on the x-axis, invert the x velocity, and same for the y-axis.
|
Quote:
I'm not sure how to use that to go at a certain angle D: |
Quote:
move_dist*sin(angle) is the y component (might need to be negative since (0,0) is the UPPER left corner on a level). |
Alright, thanks, but I think there's something wrong with my onwall checks...
PHP Code:
|
-bump-
Halp D: |
You only need one angle...
|
Well...I was gonna do that...I actually kinda forgot what forced me to do that, actually...
What would you suggest I do? ._. What I did there works, except that the onwall checks are off by 1-2 tiles |
That's probably an issue with the position of the onwall checks. You need to check from the center of the object outwards, or you'll have it hitting walls about 1 tile too soon when headed up/left, and a tile too late the other directions. But this all also depends on the velocity, as is apparent in your script.
More elegant scripts use a for loop to check more elementary distances (e.g. 1 pixel) and determine if a wall is within 1 "velocity's" range. |
This is what I used when I was messing around with a pong thing:
PHP Code:
|
I've tried everything I could think of, and I still can't get the onwall checks to be correct:
PHP Code:
This is really starting to frustrate me. |
Quote:
Here is an example: PHP Code:
|
But shouldn't the first onwall (onwall(temp.dx, temp.dy)) still work when it's first hit, then? Because in that case this.anglex and this.angley are exactly the same.
In any case, I just plain ****ed it up... PHP Code:
|
(Sorry for double posting)
I changed it to this: PHP Code:
|
Sorry to triple post, but I fixed it. :D
|
| All times are GMT +2. The time now is 03:40 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.